|
| 1 | +# FileCacheEx — Enhanced File Cache |
| 2 | + |
| 3 | +`FileCacheEx` is a drop-in replacement for `FileCache` that fixes thread-safety issues and provides runtime-configurable parameters. |
| 4 | + |
| 5 | +## Header |
| 6 | + |
| 7 | +```c++ |
| 8 | +#include "FileCacheEx.h" |
| 9 | +``` |
| 10 | + |
| 11 | +## Improvements over FileCache |
| 12 | + |
| 13 | +| Feature | FileCache | FileCacheEx | |
| 14 | +|---------|-----------|-------------| |
| 15 | +| HTTP header reserve | Compile-time 1024 bytes | Runtime configurable, default 4096 | |
| 16 | +| `prepend_header()` | Returns `void`, silently drops overflow | Returns `bool`, `false` on overflow | |
| 17 | +| Per-entry thread safety | No locking | Per-entry `std::mutex` | |
| 18 | +| `resize_buf()` httpbuf | May leave dangling reference | Explicitly invalidated (UAF prevention) | |
| 19 | +| File read | Single `read()`, may short-read | Loop with `EINTR` handling | |
| 20 | +| Cache insertion | Before initialization completes | Deferred until fully initialized | |
| 21 | +| Max file size | Compile-time `FILE_CACHE_MAX_SIZE` | Runtime `SetMaxFileSize()` | |
| 22 | +| DLL export | None | `HV_EXPORT` | |
| 23 | + |
| 24 | +## Data Structures |
| 25 | + |
| 26 | +```c++ |
| 27 | +typedef struct file_cache_ex_s { |
| 28 | + mutable std::mutex mutex; // per-entry lock |
| 29 | + std::string filepath; |
| 30 | + struct stat st; |
| 31 | + time_t open_time; |
| 32 | + time_t stat_time; |
| 33 | + uint32_t stat_cnt; |
| 34 | + HBuf buf; // header_reserve + file_content |
| 35 | + hbuf_t filebuf; // points into buf: file content |
| 36 | + hbuf_t httpbuf; // points into buf: header + content |
| 37 | + char last_modified[64]; |
| 38 | + char etag[64]; |
| 39 | + std::string content_type; |
| 40 | + int header_reserve; // bytes reserved before content |
| 41 | + int header_used; // bytes actually used by header |
| 42 | + |
| 43 | + bool is_modified(); // caller must hold mutex |
| 44 | + bool is_complete(); // caller must hold mutex |
| 45 | + void resize_buf(size_t filesize, int reserved); // caller must hold mutex |
| 46 | + void resize_buf(size_t filesize); |
| 47 | + bool prepend_header(const char* header, int len); // thread-safe |
| 48 | + |
| 49 | + // Thread-safe accessors |
| 50 | + int get_header_reserve() const; |
| 51 | + int get_header_used() const; |
| 52 | + int get_header_remaining() const; |
| 53 | + bool header_fits(int len) const; |
| 54 | +} file_cache_ex_t; |
| 55 | + |
| 56 | +typedef std::shared_ptr<file_cache_ex_t> file_cache_ex_ptr; |
| 57 | +``` |
| 58 | +
|
| 59 | +## FileCacheEx Class |
| 60 | +
|
| 61 | +```c++ |
| 62 | +class HV_EXPORT FileCacheEx : public hv::LRUCache<std::string, file_cache_ex_ptr> { |
| 63 | +public: |
| 64 | + int stat_interval; // seconds between stat() checks, default 10 |
| 65 | + int expired_time; // seconds before expiry, default 60 |
| 66 | + int max_header_length; // header reserve per entry, default 4096 |
| 67 | + int max_file_size; // max cached file size, default 4MB |
| 68 | +
|
| 69 | + explicit FileCacheEx(size_t capacity = 100); |
| 70 | +
|
| 71 | + file_cache_ex_ptr Open(const char* filepath, OpenParam* param); |
| 72 | + bool Exists(const char* filepath) const; |
| 73 | + bool Close(const char* filepath); |
| 74 | + void RemoveExpiredFileCache(); |
| 75 | +
|
| 76 | + int GetMaxHeaderLength() const; |
| 77 | + int GetMaxFileSize() const; |
| 78 | + int GetStatInterval() const; |
| 79 | + int GetExpiredTime() const; |
| 80 | +
|
| 81 | + void SetMaxHeaderLength(int len); |
| 82 | + void SetMaxFileSize(int size); |
| 83 | +}; |
| 84 | +``` |
| 85 | + |
| 86 | +## Usage |
| 87 | + |
| 88 | +```c++ |
| 89 | +FileCacheEx filecache(200); // LRU capacity = 200 |
| 90 | +filecache.SetMaxHeaderLength(8192); // 8K header reserve |
| 91 | +filecache.SetMaxFileSize(1 << 24); // 16MB max file |
| 92 | +filecache.stat_interval = 5; |
| 93 | +filecache.expired_time = 120; |
| 94 | + |
| 95 | +FileCacheEx::OpenParam param; |
| 96 | +file_cache_ex_ptr fc = filecache.Open("/var/www/index.html", ¶m); |
| 97 | +if (fc) { |
| 98 | + std::string header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"; |
| 99 | + if (fc->prepend_header(header.c_str(), header.size())) { |
| 100 | + // send fc->httpbuf (header + content) |
| 101 | + } else { |
| 102 | + // header exceeds reserve, fall back to separate send |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | +
|
| 107 | +## Migration from FileCache |
| 108 | +
|
| 109 | +1. Replace `#include "FileCache.h"` with `#include "FileCacheEx.h"` |
| 110 | +2. Change types: `FileCache` → `FileCacheEx`, `file_cache_ptr` → `file_cache_ex_ptr` |
| 111 | +3. Handle `bool` return from `prepend_header()` |
| 112 | +4. Optional: configure via `SetMaxHeaderLength()` / `SetMaxFileSize()` |
| 113 | +
|
| 114 | +## Thread Safety |
| 115 | +
|
| 116 | +- `FileCacheEx` inherits `hv::LRUCache` global mutex for LRU operations |
| 117 | +- Each `file_cache_ex_s` entry has its own `std::mutex` for entry-level protection |
| 118 | +- `prepend_header()` locks automatically; callers need no external synchronization |
| 119 | +- `is_modified()`, `is_complete()`, `resize_buf()` require caller to hold the mutex |
0 commit comments