FileCache.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #ifndef HW_FILE_CACHE_H_
  2. #define HW_FILE_CACHE_H_
  3. #include <map>
  4. #include <string>
  5. #include "hbuf.h"
  6. #include "hfile.h"
  7. #include "md5.h"
  8. #include "HttpRequest.h" // for get_content_type_str_by_suffix
  9. #ifndef INVALID_FD
  10. #define INVALID_FD -1
  11. #endif
  12. typedef struct file_cache_s {
  13. //std::string filepath;
  14. struct stat st;
  15. time_t open_time;
  16. time_t stat_time;
  17. uint32_t stat_cnt;
  18. HBuf filebuf;
  19. char last_modified[64];
  20. char etag[64];
  21. const char* content_type;
  22. file_cache_s() {
  23. stat_cnt = 0;
  24. content_type = NULL;
  25. }
  26. } file_cache_t;
  27. // filepath => file_cache_t
  28. typedef std::map<std::string, file_cache_t*> FileCacheMap;
  29. #define DEFAULT_FILE_STAT_INTERVAL 10 // s
  30. #define DEFAULT_FILE_CACHED_TIME 60 // s
  31. class FileCache {
  32. public:
  33. int file_stat_interval;
  34. int file_cached_time;
  35. FileCacheMap cached_files;
  36. FileCache() {
  37. file_stat_interval = DEFAULT_FILE_STAT_INTERVAL;
  38. file_cached_time = DEFAULT_FILE_CACHED_TIME;
  39. }
  40. ~FileCache() {
  41. for (auto& pair : cached_files) {
  42. delete pair.second;
  43. }
  44. cached_files.clear();
  45. }
  46. file_cache_t* Open(const char* filepath) {
  47. file_cache_t* fc = Get(filepath);
  48. bool filechanged = false;
  49. if (fc) {
  50. time_t tt;
  51. time(&tt);
  52. if (tt - fc->stat_time > file_stat_interval) {
  53. struct timespec mtime = fc->st.st_mtim;
  54. stat(filepath, &fc->st);
  55. fc->stat_time = tt;
  56. fc->stat_cnt++;
  57. if (mtime.tv_sec != fc->st.st_mtim.tv_sec ||
  58. mtime.tv_nsec != fc->st.st_mtim.tv_nsec) {
  59. filechanged = true;
  60. fc->stat_cnt = 1;
  61. }
  62. }
  63. }
  64. if (fc == NULL || filechanged) {
  65. int fd = open(filepath, O_RDONLY);
  66. if (fd < 0) {
  67. return NULL;
  68. }
  69. if (fc == NULL) {
  70. fc = new file_cache_t;
  71. //fc->filepath = filepath;
  72. fstat(fd, &fc->st);
  73. time(&fc->open_time);
  74. fc->stat_time = fc->open_time;
  75. fc->stat_cnt = 1;
  76. cached_files[filepath] = fc;
  77. }
  78. fc->filebuf.resize(fc->st.st_size);
  79. read(fd, fc->filebuf.base, fc->filebuf.len);
  80. close(fd);
  81. time_t tt = fc->st.st_mtim.tv_sec;
  82. strftime(fc->last_modified, sizeof(fc->last_modified), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&tt));
  83. MD5_CTX md5_ctx;
  84. MD5Init(&md5_ctx);
  85. MD5Update(&md5_ctx, fc->filebuf.base, fc->filebuf.len);
  86. unsigned char digital[16];
  87. MD5Final(digital, &md5_ctx);
  88. char* md5 = fc->etag;
  89. for (int i = 0; i < 16; ++i) {
  90. sprintf(md5, "%02x", digital[i]);
  91. md5 += 2;
  92. }
  93. fc->etag[32] = '\0';
  94. const char* suffix = strrchr(filepath, '.');
  95. if (suffix) {
  96. ++suffix;
  97. fc->content_type = http_content_type_str_by_suffix(suffix);
  98. }
  99. }
  100. return fc;
  101. }
  102. int Close(const char* filepath) {
  103. auto iter = cached_files.find(filepath);
  104. if (iter != cached_files.end()) {
  105. delete iter->second;
  106. iter = cached_files.erase(iter);
  107. return 0;
  108. }
  109. return -1;
  110. }
  111. protected:
  112. file_cache_t* Get(const char* filepath) {
  113. auto iter = cached_files.find(filepath);
  114. if (iter != cached_files.end()) {
  115. return iter->second;
  116. }
  117. return NULL;
  118. }
  119. };
  120. #endif // HW_FILE_CACHE_H_