29#ifndef DAP_OBJMEMCACHE_H_
30#define DAP_OBJMEMCACHE_H_
88 libdap::DapObj *d_obj;
89 const std::string d_name;
92 Entry(libdap::DapObj *o,
const std::string &n): d_obj(o), d_name(n) { }
94 ~Entry() {
delete d_obj; d_obj = 0;}
101 unsigned long long d_age;
102 unsigned int d_entries_threshold;
103 float d_purge_threshold;
105 typedef std::pair<unsigned int, Entry*> cache_pair_t;
106 typedef std::map<unsigned int, Entry*> cache_t;
109 typedef std::pair<const std::string, unsigned int> index_pair_t;
111 typedef std::map<const std::string, unsigned int> index_t;
114 friend class DDSMemCacheTest;
125 ObjMemCache(): d_age(0), d_entries_threshold(0), d_purge_threshold(0.2) { }
137 ObjMemCache(
unsigned int entries_threshold,
float purge_threshold): d_age(0),
138 d_entries_threshold(entries_threshold), d_purge_threshold(purge_threshold) {
144 virtual void add(libdap::DapObj *obj,
const std::string &key);
146 virtual void remove(
const std::string &key);
148 virtual libdap::DapObj *
get(
const std::string &key);
154 virtual unsigned int size()
const {
155 assert(cache.size() == index.size());
159 virtual void purge(
float fraction);
166 os <<
"ObjMemCache" << std::endl;
167 os <<
"Length of index: " << index.size() << std::endl;
168 for(index_t::const_iterator it = index.begin(); it != index.end(); ++it) {
169 os << it->first <<
" --> " << it->second << std::endl;
172 os <<
"Length of cache: " << cache.size() << std::endl;
173 for(cache_t::const_iterator it = cache.begin(); it != cache.end(); ++it) {
174 os << it->first <<
" --> " << it->second->d_name << std::endl;
An in-memory cache for DapObj (DAS, DDS, ...) objects.
virtual unsigned int size() const
How many items are in the cache.
ObjMemCache()
Initialize the DapObj cache This constructor builds a cache that will require the caller manage the p...
virtual void add(libdap::DapObj *obj, const std::string &key)
Add an object to the cache and associate it with a key.
virtual libdap::DapObj * get(const std::string &key)
Get the cached pointer.
ObjMemCache(unsigned int entries_threshold, float purge_threshold)
Initialize the DapObj cache to use an item count threshold.
virtual void remove(const std::string &key)
Remove the object associated with a key.
virtual void dump(ostream &os)
What is in the cache.
virtual void purge(float fraction)
Purge the oldest elements.