FileCache.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. time_t mtime = fc->st.st_mtime;
  54. stat(filepath, &fc->st);
  55. fc->stat_time = tt;
  56. fc->stat_cnt++;
  57. if (mtime != fc->st.st_mtime) {
  58. filechanged = true;
  59. fc->stat_cnt = 1;
  60. }
  61. }
  62. }
  63. if (fc == NULL || filechanged) {
  64. int fd = open(filepath, O_RDONLY);
  65. if (fd < 0) {
  66. return NULL;
  67. }
  68. if (fc == NULL) {
  69. fc = new file_cache_t;
  70. //fc->filepath = filepath;
  71. fstat(fd, &fc->st);
  72. time(&fc->open_time);
  73. fc->stat_time = fc->open_time;
  74. fc->stat_cnt = 1;
  75. cached_files[filepath] = fc;
  76. }
  77. fc->filebuf.resize(fc->st.st_size);
  78. read(fd, fc->filebuf.base, fc->filebuf.len);
  79. close(fd);
  80. time_t tt = fc->st.st_mtime;
  81. strftime(fc->last_modified, sizeof(fc->last_modified), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&tt));
  82. MD5_CTX md5_ctx;
  83. MD5Init(&md5_ctx);
  84. MD5Update(&md5_ctx, fc->filebuf.base, fc->filebuf.len);
  85. unsigned char digital[16];
  86. MD5Final(digital, &md5_ctx);
  87. char* md5 = fc->etag;
  88. for (int i = 0; i < 16; ++i) {
  89. sprintf(md5, "%02x", digital[i]);
  90. md5 += 2;
  91. }
  92. fc->etag[32] = '\0';
  93. const char* suffix = strrchr(filepath, '.');
  94. if (suffix) {
  95. ++suffix;
  96. fc->content_type = http_content_type_str_by_suffix(suffix);
  97. }
  98. }
  99. return fc;
  100. }
  101. int Close(const char* filepath) {
  102. auto iter = cached_files.find(filepath);
  103. if (iter != cached_files.end()) {
  104. delete iter->second;
  105. iter = cached_files.erase(iter);
  106. return 0;
  107. }
  108. return -1;
  109. }
  110. protected:
  111. file_cache_t* Get(const char* filepath) {
  112. auto iter = cached_files.find(filepath);
  113. if (iter != cached_files.end()) {
  114. return iter->second;
  115. }
  116. return NULL;
  117. }
  118. };
  119. #endif // HW_FILE_CACHE_H_