25#ifndef _http_cache_table_h
26#define _http_cache_table_h
38#define NO_LM_EXPIRATION (24 * 3600)
39#define MAX_LM_EXPIRATION (48 * 3600)
44#define LM_EXPIRATION(t) (min((MAX_LM_EXPIRATION), static_cast<int>((t) / 10)))
84 std::string cachename;
93 unsigned long size = 0;
96 time_t freshness_lifetime = 0;
97 time_t response_time = 0;
98 time_t corrected_initial_age = 0;
100 bool must_revalidate =
false;
101 bool no_cache =
false;
104 std::mutex d_readers_lock;
105 std::mutex d_response_read_lock;
106 std::mutex d_response_write_lock;
144 if (d_response_read_lock.try_lock()) {
145 d_response_write_lock.lock();
146 d_response_write_lock.unlock();
148 std::lock_guard<std::mutex> lock(d_readers_lock);
153 std::lock_guard<std::mutex> lock(d_readers_lock);
156 d_response_read_lock.unlock();
163 d_response_read_lock.unlock();
164 d_response_write_lock.unlock();
186 std::string d_cache_root;
187 unsigned int d_block_size;
188 unsigned long d_current_size = 0;
190 std::string d_cache_index;
191 int d_new_entries = 0;
193 std::map<FILE *, HTTPCacheTable::CacheEntry *> d_locked_entries;
195 CacheEntry *get_read_locked_entry_from_cache_table(
int hash,
const std::string &url);
196 bool cache_index_delete();
197 bool cache_index_read();
198 CacheEntry *cache_index_parse_line(
const char *line);
199 std::string create_hash_directory(
int hash);
238 CacheEntry *get_read_locked_entry_from_cache_table(
const std::string &url);
243 const std::vector<std::string> &headers);
262class cache_entry_guard {
264 enum class operation { read, write };
268 HTTPCacheTable::CacheEntry *d_entry;
269 bool d_managed =
true;
270 bool d_delete_after_unlock =
false;
272 cache_entry_guard() =
delete;
274 cache_entry_guard(
const cache_entry_guard &) =
delete;
276 cache_entry_guard &operator=(
const cache_entry_guard &) =
delete;
278 HTTPCacheTable::CacheEntry* &operator->() {
283 cache_entry_guard(operation op, HTTPCacheTable::CacheEntry *entry) : d_op(op), d_entry(entry) {
286 ~cache_entry_guard() {
288 if (d_op == operation::read)
289 d_entry->unlock_read_response();
291 d_entry->unlock_write_response();
293 if (d_delete_after_unlock)
299 void release() { d_managed =
false; }
302 void delete_after() { d_delete_after_unlock =
true; }
const string CACHE_EMPTY_ETAG
void create_location(CacheEntry *entry)
void calculate_time(HTTPCacheTable::CacheEntry *entry, int default_expiration, time_t request_time)
void delete_expired_entries(time_t time=0)
Delete all the expired entries in the cache.
void bind_entry_to_data(CacheEntry *entry, FILE *body)
std::vector< CacheEntry * > CacheEntries
void set_cache_root(const std::string &cr)
unsigned int get_block_size() const
void set_block_size(unsigned int sz)
void remove_entry_from_cache_table(const std::string &url)
void delete_by_size(unsigned long size)
Delete all the entries in the cache that are larger than size bytes.
void delete_by_hits(int hits)
Delete all the entries in the cache that have fewer than hits hits.
friend class HTTPCacheTest
virtual ~HTTPCacheTable()
HTTPCacheTable & operator=(const HTTPCacheTable &)=delete
std::string get_cache_root() const
void add_entry_to_cache_table(CacheEntry *entry)
int get_new_entries() const
HTTPCacheTable(const std::string &cache_root, int block_size)
void set_current_size(unsigned long sz)
std::vector< CacheEntries > CacheTable
HTTPCacheTable(const HTTPCacheTable &)=delete
unsigned long get_current_size() const
CacheEntry * get_write_locked_entry_from_cache_table(const std::string &url)
void delete_all_entries()
void uncouple_entry_from_data(FILE *body)
void parse_headers(HTTPCacheTable::CacheEntry *entry, unsigned long max_entry_size, const std::vector< std::string > &headers)
void increment_new_entries()
bool is_locked_read_responses() const
top level DAP object to house generic methods
int get_hash(const string &url)
void unlock_write_response()
friend class HTTPCacheTable
void set_no_cache(bool state)
void lock_write_response()
std::string get_etag() const
bool get_must_revalidate() const
void set_size(unsigned long sz)
friend class HTTPCacheTest
time_t get_response_time() const
void lock_read_response()
time_t get_max_age() const
CacheEntry(std::string u)
time_t get_corrected_initial_age() const
friend class WriteOneCacheEntry
void unlock_read_response()
std::string get_cachename() const
time_t get_freshness_lifetime() const
time_t get_expires() const