bes  Updated for version 3.17.4
ObjMemCache.h
1 
2 // This file is part of bes, A C++ back-end server implementation framework
3 // for the OPeNDAP Data Access Protocol.
4 
5 // Copyright (c) 2016 OPeNDAP
6 // Author: James Gallagher <jgallagher@opendap.org>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 /*
23  * ObjMemCache.h
24  *
25  * Created on: May 18, 2016
26  * Author: jimg
27  */
28 
29 #ifndef DAP_OBJMEMCACHE_H_
30 #define DAP_OBJMEMCACHE_H_
31 
32 #include <cassert>
33 
34 #include <string>
35 #include <map>
36 
37 namespace libdap {
38  class DapObj;
39 }
40 
41 //namespace bes {
42 
82 class ObjMemCache {
83 private:
84  struct Entry {
85  libdap::DapObj *d_obj; // A weak pointer - we do not manage this storage
86  const std::string d_name;
87 
88  // We need the string so that we can erase the index entry easily
89  Entry(libdap::DapObj *o, const std::string &n): d_obj(o), d_name(n) { }
90  // deleting an Entry deletes the thing it references
91  ~Entry() { delete d_obj; d_obj = 0;}
92  };
93 
94  unsigned long long d_age; // When obj was add or last accessed
95  unsigned int d_entries_threshold; // no more than this num of entries
96  float d_purge_threshold; // free up this fraction of the cache
97 
98  typedef std::pair<unsigned int, Entry*> cache_pair_t; // used by map::insert()
99  typedef std::map<unsigned int, Entry*> cache_t;
100  cache_t cache;
101 
102  typedef std::pair<const std::string, unsigned int> index_pair_t;
103  // efficiency improvement - use an unordered_map when C++-11 is adopted
104  typedef std::map<const std::string, unsigned int> index_t;
105  index_t index;
106 
107  friend class DDSMemCacheTest;
108 
109 public:
118  ObjMemCache(): d_age(0), d_entries_threshold(0), d_purge_threshold(0.2) { }
119 
130  ObjMemCache(unsigned int entries_threshold, float purge_threshold): d_age(0),
131  d_entries_threshold(entries_threshold), d_purge_threshold(purge_threshold) {
132  // d_entries_threshold = entries_threshold >> 1; // * 2
133  }
134 
135  virtual ~ObjMemCache();
136 
137  virtual void add(libdap::DapObj *obj, const std::string &key);
138 
139  virtual void remove(const std::string &key);
140 
141  virtual libdap::DapObj *get(const std::string &key);
142 
147  virtual unsigned int size() const {
148  assert(cache.size() == index.size());
149  return cache.size();
150  }
151 
152  virtual void purge(float fraction);
153 
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;
163  }
164 
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;
168  }
169  }
170 };
171 
172 // } namespace bes
173 
174 #endif /* DAP_OBJMEMCACHE_H_ */
ObjMemCache()
Initialize the DapObj cache This constructor builds a cache that will require the caller manage the p...
Definition: ObjMemCache.h:118
virtual unsigned int size() const
How many items are in the cache.
Definition: ObjMemCache.h:147
virtual void dump(ostream &os)
What is in the cache.
Definition: ObjMemCache.h:158
virtual void purge(float fraction)
Purge the oldest elements.
Definition: ObjMemCache.cc:145
virtual void add(libdap::DapObj *obj, const std::string &key)
Add an object to the cache and associate it with a key.
Definition: ObjMemCache.cc:63
ObjMemCache(unsigned int entries_threshold, float purge_threshold)
Initialize the DapObj cache to use an item count threshold.
Definition: ObjMemCache.h:130
An in-memory cache for DapObj (DAS, DDS, ...) objects.
Definition: ObjMemCache.h:82