FileCache.h 3.4 KB

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