FileCache.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #define HTTP_HEADER_MAX_LENGTH 1024 // 1k
  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 buf; // http_header + file_content
  20. hbuf_t filebuf;
  21. hbuf_t httpbuf;
  22. char last_modified[64];
  23. char etag[64];
  24. const char* content_type;
  25. file_cache_s() {
  26. stat_cnt = 0;
  27. content_type = NULL;
  28. }
  29. void resize_buf(int filesize) {
  30. buf.resize(HTTP_HEADER_MAX_LENGTH + filesize);
  31. filebuf.base = buf.base + HTTP_HEADER_MAX_LENGTH;
  32. filebuf.len = filesize;
  33. }
  34. void prepend_header(const char* header, int len) {
  35. if (len > HTTP_HEADER_MAX_LENGTH) return;
  36. httpbuf.base = filebuf.base - len;
  37. httpbuf.len = len + filebuf.len;
  38. memcpy(httpbuf.base, header, len);
  39. }
  40. } file_cache_t;
  41. // filepath => file_cache_t
  42. typedef std::map<std::string, file_cache_t*> FileCacheMap;
  43. #define DEFAULT_FILE_STAT_INTERVAL 10 // s
  44. #define DEFAULT_FILE_CACHED_TIME 60 // s
  45. class FileCache {
  46. public:
  47. int file_stat_interval;
  48. int file_cached_time;
  49. FileCacheMap cached_files;
  50. FileCache() {
  51. file_stat_interval = DEFAULT_FILE_STAT_INTERVAL;
  52. file_cached_time = DEFAULT_FILE_CACHED_TIME;
  53. }
  54. ~FileCache() {
  55. for (auto& pair : cached_files) {
  56. delete pair.second;
  57. }
  58. cached_files.clear();
  59. }
  60. file_cache_t* Open(const char* filepath) {
  61. file_cache_t* fc = Get(filepath);
  62. bool filechanged = false;
  63. if (fc) {
  64. time_t tt;
  65. time(&tt);
  66. if (tt - fc->stat_time > file_stat_interval) {
  67. time_t mtime = fc->st.st_mtime;
  68. stat(filepath, &fc->st);
  69. fc->stat_time = tt;
  70. fc->stat_cnt++;
  71. if (mtime != fc->st.st_mtime) {
  72. filechanged = true;
  73. fc->stat_cnt = 1;
  74. }
  75. }
  76. }
  77. if (fc == NULL || filechanged) {
  78. int fd = open(filepath, O_RDONLY);
  79. if (fd < 0) {
  80. return NULL;
  81. }
  82. if (fc == NULL) {
  83. fc = new file_cache_t;
  84. //fc->filepath = filepath;
  85. fstat(fd, &fc->st);
  86. time(&fc->open_time);
  87. fc->stat_time = fc->open_time;
  88. fc->stat_cnt = 1;
  89. cached_files[filepath] = fc;
  90. }
  91. fc->resize_buf(fc->st.st_size);
  92. read(fd, fc->filebuf.base, fc->filebuf.len);
  93. close(fd);
  94. time_t tt = fc->st.st_mtime;
  95. strftime(fc->last_modified, sizeof(fc->last_modified), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&tt));
  96. MD5_CTX md5_ctx;
  97. MD5Init(&md5_ctx);
  98. MD5Update(&md5_ctx, (unsigned char*)fc->filebuf.base, fc->filebuf.len);
  99. unsigned char digital[16];
  100. MD5Final(digital, &md5_ctx);
  101. char* md5 = fc->etag;
  102. for (int i = 0; i < 16; ++i) {
  103. sprintf(md5, "%02x", digital[i]);
  104. md5 += 2;
  105. }
  106. fc->etag[32] = '\0';
  107. const char* suffix = strrchr(filepath, '.');
  108. if (suffix) {
  109. fc->content_type = http_content_type_str_by_suffix(++suffix);
  110. }
  111. }
  112. return fc;
  113. }
  114. int Close(const char* filepath) {
  115. auto iter = cached_files.find(filepath);
  116. if (iter != cached_files.end()) {
  117. delete iter->second;
  118. iter = cached_files.erase(iter);
  119. return 0;
  120. }
  121. return -1;
  122. }
  123. protected:
  124. file_cache_t* Get(const char* filepath) {
  125. auto iter = cached_files.find(filepath);
  126. if (iter != cached_files.end()) {
  127. return iter->second;
  128. }
  129. return NULL;
  130. }
  131. };
  132. #endif // HW_FILE_CACHE_H_