26 #ifndef _http_cache_table_h
27 #define _http_cache_table_h
49 #ifndef _internalerr_h
61 #define LOCK(m) do { \
62 int code = pthread_mutex_lock((m)); \
64 throw InternalErr(__FILE__, __LINE__, "Mutex lock: " + long_to_string(code)); \
67 #define UNLOCK(m) do { \
68 int code = pthread_mutex_unlock((m)); \
70 throw InternalErr(__FILE__, __LINE__, "Mutex unlock: " + long_to_string(code)); \
73 #define TRYLOCK(m) pthread_mutex_trylock((m))
74 #define INIT(m) pthread_mutex_init((m), 0)
75 #define DESTROY(m) pthread_mutex_destroy((m))
130 time_t freshness_lifetime;
131 time_t response_time;
132 time_t corrected_initial_age;
134 bool must_revalidate;
138 pthread_mutex_t d_response_lock;
139 pthread_mutex_t d_response_write_lock;
143 friend class HTTPCacheTest;
146 friend class DeleteCacheEntry;
147 friend class WriteOneCacheEntry;
148 friend class DeleteExpired;
149 friend class DeleteByHits;
150 friend class DeleteBySize;
153 string get_cachename()
173 void set_size(
unsigned long sz)
177 time_t get_freshness_lifetime()
179 return freshness_lifetime;
181 time_t get_response_time()
183 return response_time;
185 time_t get_corrected_initial_age()
187 return corrected_initial_age;
189 bool get_must_revalidate()
191 return must_revalidate;
193 void set_no_cache(
bool state)
202 void lock_read_response()
204 DBG(cerr <<
"Try locking read response... (" << hex << &d_response_lock << dec <<
") ");
205 int status =
TRYLOCK(&d_response_lock);
208 LOCK(&d_response_write_lock);
209 UNLOCK(&d_response_write_lock);
211 DBGN(cerr <<
"Done" << endl);
215 void unlock_read_response()
219 DBG(cerr <<
"Unlocking read response... (" << hex << &d_response_lock << dec <<
") ");
221 DBGN(cerr <<
"Done" << endl);
225 void lock_write_response()
227 DBG(cerr <<
"locking write response... (" << hex << &d_response_lock << dec <<
") ");
228 LOCK(&d_response_lock);
229 LOCK(&d_response_write_lock);
230 DBGN(cerr <<
"Done" << endl);
233 void unlock_write_response()
235 DBG(cerr <<
"Unlocking write response... (" << hex << &d_response_lock << dec <<
") ");
236 UNLOCK(&d_response_write_lock);
238 DBGN(cerr <<
"Done" << endl);
242 url(
""), hash(-1), hits(0), cachename(
""), etag(
""), lm(-1), expires(-1), date(-1), age(-1), max_age(-1),
243 size(0), range(false), freshness_lifetime(0), response_time(0), corrected_initial_age(0),
244 must_revalidate(false), no_cache(false), readers(0)
246 INIT(&d_response_lock);
247 INIT(&d_response_write_lock);
250 url(u), hash(-1), hits(0), cachename(
""), etag(
""), lm(-1), expires(-1), date(-1), age(-1), max_age(-1),
251 size(0), range(false), freshness_lifetime(0), response_time(0), corrected_initial_age(0),
252 must_revalidate(false), no_cache(false), readers(0)
254 INIT(&d_response_lock);
255 INIT(&d_response_write_lock);
270 friend class HTTPCacheTest;
276 unsigned int d_block_size;
277 unsigned long d_current_size;
279 string d_cache_index;
282 map<FILE *, HTTPCacheTable::CacheEntry *> d_locked_entries;
287 throw InternalErr(__FILE__, __LINE__,
"unimplemented");
292 throw InternalErr(__FILE__, __LINE__,
"unimplemented");
297 throw InternalErr(__FILE__, __LINE__,
"unimplemented");
300 CacheTable &get_cache_table()
302 return d_cache_table;
304 CacheEntry *get_locked_entry_from_cache_table(
int hash,
const string &url);
307 HTTPCacheTable(
const string &cache_root,
int block_size);
311 unsigned long get_current_size()
const
313 return d_current_size;
315 void set_current_size(
unsigned long sz)
320 unsigned int get_block_size()
const
324 void set_block_size(
unsigned int sz)
329 int get_new_entries()
const
331 return d_new_entries;
333 void increment_new_entries()
338 string get_cache_root()
342 void set_cache_root(
const string &cr)
348 void delete_expired_entries(time_t time = 0);
349 void delete_by_hits(
int hits);
350 void delete_by_size(
unsigned int size);
351 void delete_all_entries();
353 bool cache_index_delete();
354 bool cache_index_read();
355 CacheEntry *cache_index_parse_line(
const char *line);
356 void cache_index_write();
358 string create_hash_directory(
int hash);
359 void create_location(CacheEntry *entry);
361 void add_entry_to_cache_table(CacheEntry *entry);
364 void remove_entry_from_cache_table(
const string &url);
365 CacheEntry *get_locked_entry_from_cache_table(
const string &url);
366 CacheEntry *get_write_locked_entry_from_cache_table(
const string &url);
372 void bind_entry_to_data(CacheEntry *entry, FILE *body);
373 void uncouple_entry_from_data(FILE *body);
374 bool is_locked_read_responses();