bes Updated for version 3.20.13
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#include "BESIndent.h"
38
39namespace libdap {
40 class DapObj;
41}
42
43//namespace bes {
44
85private:
86 struct Entry {
87 libdap::DapObj *d_obj; // A weak pointer - we do not manage this storage
88 const std::string d_name;
89
90 // We need the string so that we can erase the index entry easily
91 Entry(libdap::DapObj *o, const std::string &n): d_obj(o), d_name(n) { }
92 // deleting an Entry deletes the thing it references
93 ~Entry() { delete d_obj; d_obj = 0;}
94 };
95
96 unsigned long long d_age; // When obj was add or last accessed
97 unsigned int d_entries_threshold; // no more than this num of entries
98 float d_purge_threshold; // free up this fraction of the cache
99
100 typedef std::pair<unsigned int, Entry*> cache_pair_t; // used by map::insert()
101 typedef std::map<unsigned int, Entry*> cache_t;
102 cache_t cache;
103
104 typedef std::pair<const std::string, unsigned int> index_pair_t;
105 // efficiency improvement - use an unordered_map when C++-11 is adopted
106 typedef std::map<const std::string, unsigned int> index_t;
107 index_t index;
108
109 friend class DDSMemCacheTest;
110
111public:
120 ObjMemCache(): d_age(0), d_entries_threshold(0), d_purge_threshold(0.2) { }
121
132 ObjMemCache(unsigned int entries_threshold, float purge_threshold): d_age(0),
133 d_entries_threshold(entries_threshold), d_purge_threshold(purge_threshold) {
134 // d_entries_threshold = entries_threshold >> 1; // * 2
135 }
136
137 virtual ~ObjMemCache();
138
139 virtual void add(libdap::DapObj *obj, const std::string &key);
140
141 virtual void remove(const std::string &key);
142
143 virtual libdap::DapObj *get(const std::string &key);
144
149 virtual unsigned int size() const {
150 assert(cache.size() == index.size());
151 return cache.size();
152 }
153
154 virtual void purge(float fraction);
155
160 virtual void dump(ostream &os) {
161 os << "ObjMemCache" << std::endl;
162 os << "Length of index: " << index.size() << std::endl;
163 for(index_t::const_iterator it = index.begin(); it != index.end(); ++it) {
164 os << it->first << " --> " << it->second << std::endl;
165 }
166
167 os << "Length of cache: " << cache.size() << std::endl;
168 for(cache_t::const_iterator it = cache.begin(); it != cache.end(); ++it) {
169 os << it->first << " --> " << it->second->d_name << std::endl;
170 }
171 }
172};
173
174// } namespace bes
175
176#endif /* DAP_OBJMEMCACHE_H_ */
An in-memory cache for DapObj (DAS, DDS, ...) objects.
Definition: ObjMemCache.h:84
virtual unsigned int size() const
How many items are in the cache.
Definition: ObjMemCache.h:149
ObjMemCache()
Initialize the DapObj cache This constructor builds a cache that will require the caller manage the p...
Definition: ObjMemCache.h:120
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
virtual libdap::DapObj * get(const std::string &key)
Get the cached pointer.
Definition: ObjMemCache.cc:105
ObjMemCache(unsigned int entries_threshold, float purge_threshold)
Initialize the DapObj cache to use an item count threshold.
Definition: ObjMemCache.h:132
virtual void remove(const std::string &key)
Remove the object associated with a key.
Definition: ObjMemCache.cc:85
virtual void dump(ostream &os)
What is in the cache.
Definition: ObjMemCache.h:160
virtual void purge(float fraction)
Purge the oldest elements.
Definition: ObjMemCache.cc:145