29 #ifndef DAP_OBJMEMCACHE_H_ 30 #define DAP_OBJMEMCACHE_H_ 85 libdap::DapObj *d_obj;
86 const std::string d_name;
89 Entry(libdap::DapObj *o,
const std::string &n): d_obj(o), d_name(n) { }
91 ~Entry() {
delete d_obj; d_obj = 0;}
94 unsigned long long d_age;
95 unsigned int d_entries_threshold;
96 float d_purge_threshold;
98 typedef std::pair<unsigned int, Entry*> cache_pair_t;
99 typedef std::map<unsigned int, Entry*> cache_t;
102 typedef std::pair<const std::string, unsigned int> index_pair_t;
104 typedef std::map<const std::string, unsigned int> index_t;
107 friend class DDSMemCacheTest;
118 ObjMemCache(): d_age(0), d_entries_threshold(0), d_purge_threshold(0.2) { }
130 ObjMemCache(
unsigned int entries_threshold,
float purge_threshold): d_age(0),
131 d_entries_threshold(entries_threshold), d_purge_threshold(purge_threshold) {
137 virtual void add(libdap::DapObj *obj,
const std::string &key);
139 virtual void remove(
const std::string &key);
141 virtual libdap::DapObj *
get(
const std::string &key);
147 virtual unsigned int size()
const {
148 assert(cache.size() == index.size());
152 virtual void purge(
float fraction);
158 virtual void dump(ostream &os) {
159 os <<
"ObjMemCache" << endl;
160 os <<
"Length of index: " << index.size() << endl;
161 for(index_t::const_iterator it = index.begin(); it != index.end(); ++it) {
162 os << it->first <<
" --> " << it->second << endl;
165 os <<
"Length of cache: " << cache.size() << endl;
166 for(cache_t::const_iterator it = cache.begin(); it != cache.end(); ++it) {
167 os << it->first <<
" --> " << it->second->d_name << endl;
ObjMemCache()
Initialize the DapObj cache This constructor builds a cache that will require the caller manage the p...
virtual unsigned int size() const
How many items are in the cache.
virtual libdap::DapObj * get(const std::string &key)
Get the cached pointer.
virtual void dump(ostream &os)
What is in the cache.
virtual void purge(float fraction)
Purge the oldest elements.
virtual void add(libdap::DapObj *obj, const std::string &key)
Add an object to the cache and associate it with a key.
virtual void remove(const std::string &key)
Remove the object associated with a key.
ObjMemCache(unsigned int entries_threshold, float purge_threshold)
Initialize the DapObj cache to use an item count threshold.
An in-memory cache for DapObj (DAS, DDS, ...) objects.