37#define DUMP_FREQUENCY (10)
39#define NO_LM_EXPIRATION (24 * 3600)
41#define MEGA (0x100000L)
42#define CACHE_TOTAL_SIZE (20)
43#define CACHE_FOLDER_PCT (10)
44#define CACHE_GC_PCT (10)
45#define MIN_CACHE_TOTAL_SIZE (5)
46#define MAX_CACHE_ENTRY_SIZE (3)
108 std::string d_cache_root;
109 int d_cache_lock_fd = -1;
110 std::string d_cache_lock_file;
112 bool d_cache_enabled =
false;
113 bool d_cache_protected =
false;
115 bool d_expire_ignored =
false;
116 bool d_always_validate =
false;
124 std::vector<std::string> d_cache_control;
129 time_t d_max_age = -1;
130 time_t d_max_stale = -1;
131 time_t d_min_fresh = -1;
134 std::mutex d_cache_mutex;
139 std::vector<std::string> d_open_files;
141 void set_cache_root(
const std::string &root =
"");
143 void create_cache_root(
const std::string &cache_root)
const;
145 int m_initialize_cache_lock(
const std::string &cache_lock)
const;
147 static void m_lock_cache_write(
int fd);
149 static void m_lock_cache_read(
int fd);
151 static void m_unlock_cache(
int fd);
153 static void m_exclusive_to_shared_lock(
int fd);
155 bool is_url_in_cache(
const std::string &url);
159 void write_metadata(
const std::string &cachename,
const std::vector<std::string> &headers);
161 void read_metadata(
const std::string &cachename, std::vector<std::string> &headers)
const;
163 int write_body(
const std::string &cachename,
const FILE *src);
165 FILE *open_body(
const std::string &cachename);
169 bool startGC()
const;
171 void perform_garbage_collection();
179 explicit HTTPCache(
const std::string &cache_root);
193 class mp_lock_guard {
195 enum class operation { read, write };
199 bool d_locked =
false;
200 bool d_released =
false;
212 if (d_op == operation::write)
213 HTTPCache::m_lock_cache_write(d_fd);
215 HTTPCache::m_lock_cache_read(d_fd);
217 }
catch (
const std::exception &e) {
220 std::cerr <<
"mp_lock_guard::mp_lock_guard() - Failed to lock the cache (" << e.what() <<
")."
227 if (!d_released && d_locked) {
228 HTTPCache::m_unlock_cache(d_fd);
230 }
catch (
const std::exception &e) {
232 std::cerr <<
"mp_lock_guard::~mp_lock_guard() - Failed to release the cache lock (" << e.what() <<
")."
237 void release() { d_released =
true; }
269 static HTTPCache *
instance(
const std::string &cache_root =
"") {
271 static HTTPCache
instance(cache_root);
310 bool cache_response(
const std::string &url, time_t request_time,
const std::vector<std::string> &headers,
313 void update_response(
const std::string &url, time_t request_time,
const std::vector<std::string> &headers);
323 FILE *
get_cached_response(
const std::string &url, std::vector<std::string> &headers, std::string &cacheName);
#define MAX_CACHE_ENTRY_SIZE
CacheDisconnectedMode get_cache_disconnected() const
void set_expire_ignored(bool mode)
void set_default_expiration(int exp_time)
bool is_url_valid(const std::string &url)
std::vector< std::string > get_conditional_request_headers(const std::string &url)
std::string get_cache_root() const
void set_cache_disconnected(CacheDisconnectedMode mode)
void release_cached_response(FILE *response)
HTTPCache & operator=(const HTTPCache &)=delete
static HTTPCache * instance(const std::string &cache_root="")
HTTPCache(const HTTPCache &)=delete
unsigned long get_max_entry_size() const
void set_cache_enabled(bool mode)
FILE * get_cached_response(const std::string &url, std::vector< std::string > &headers, std::string &cacheName)
friend class HTTPCacheTest
unsigned long get_max_size() const
bool cache_response(const std::string &url, time_t request_time, const std::vector< std::string > &headers, const FILE *body)
std::vector< std::string > get_cache_control() const
void set_max_entry_size(unsigned long size)
bool get_always_validate() const
friend class HTTPConnectTest
int get_default_expiration() const
void set_always_validate(bool validate)
void update_response(const std::string &url, time_t request_time, const std::vector< std::string > &headers)
void set_max_size(unsigned long size)
bool is_expire_ignored() const
void set_cache_control(const std::vector< std::string > &cc)
bool is_cache_enabled() const
Lock the cache for writing. Implements RAII for the multi-process write lock for the cache.
top level DAP object to house generic methods
bool is_hop_by_hop_header(const string &header)