34#include <libdap/DapObj.h>
35#include <libdap/InternalErr.h>
37#include "ObjMemCache.h"
44ObjMemCache::~ObjMemCache()
46 for (cache_t::iterator i = cache.begin(), e = cache.end(); i != e; ++i) {
73 if (d_entries_threshold && (cache.size() > d_entries_threshold))
74 purge(d_purge_threshold);
76 index.insert(index_pair_t(key, d_age));
78 cache.insert(cache_pair_t(d_age,
new Entry(obj, key)));
87 index_t::iterator i = index.find(key);
89 if (i != index.end()) {
90 unsigned int count = i->second;
92 cache_t::iterator c = cache.find(count);
93 assert(c != cache.end());
107 DapObj *cached_obj = 0;
109 index_t::iterator i = index.find(key);
110 if (i != index.end()) {
111 cache_t::iterator c = cache.find(i->second);
112 assert(c != cache.end());
115 if (c != cache.end()) {
118 Entry *e = c->second;
119 cached_obj = e->d_obj;
123 cache.insert(cache_pair_t(++d_age, e));
130 throw InternalErr(__FILE__, __LINE__,
"Memory cache consistency error.");
135 index.insert(index_pair_t(key, d_age));
148 size_t num_remove = cache.size() * fraction;
150 cache_t::iterator c = cache.begin(), e = cache.end();
151 for (
unsigned int i = 0; i < num_remove && c != e; ++i) {
152 const string name = c->second->d_name;
157 index_t::iterator pos = index.find(name);
158 assert(pos != index.end());
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.
virtual void remove(const std::string &key)
Remove the object associated with a key.
virtual void purge(float fraction)
Purge the oldest elements.