activemq-cpp-3.8.2
LRUCache.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef _DECAF_UTIL_LRUCACHE_H_
19 #define _DECAF_UTIL_LRUCACHE_H_
20 
21 #include <decaf/util/Config.h>
22 
25 
26 namespace decaf {
27 namespace util {
28 
42  template<typename K, typename V, typename HASHCODE = HashCode<K> >
43  class LRUCache : public LinkedHashMap<K, V, HASHCODE> {
44  protected:
45 
47 
48  public:
49 
50 
54  LRUCache() : LinkedHashMap<K, V, HASHCODE>(0, 0.75f, true), maxCacheSize(10000) {}
55 
62  LRUCache(int maximumCacheSize) :
63  LinkedHashMap<K, V, HASHCODE>(0, 0.75f, true), maxCacheSize(maximumCacheSize) {
64 
65  if (maximumCacheSize <= 0) {
67  __FILE__, __LINE__, "Cache size must be greater than zero.");
68  }
69  }
70 
87  LRUCache(int initialCapacity, int maximumCacheSize, float loadFactor, bool accessOrder) :
88  LinkedHashMap<K, V, HASHCODE>(initialCapacity, loadFactor, accessOrder), maxCacheSize(maximumCacheSize) {
89 
90  if (maximumCacheSize <= 0) {
92  __FILE__, __LINE__, "Cache size must be greater than zero.");
93  }
94  }
95 
96  virtual ~LRUCache() {}
97 
103  int getMaxCacheSize() const {
104  return maxCacheSize;
105  }
106 
115  void setMaxCacheSize(int size) {
116  if (size <= 0) {
118  __FILE__, __LINE__, "Cache size must be greater than zero.");
119  }
120 
121  this->maxCacheSize = size;
122  }
123 
124  protected:
125 
126  virtual bool removeEldestEntry(const MapEntry<K, V>& eldest DECAF_UNUSED) {
127  if (this->size() > maxCacheSize) {
128  return true;
129  }
130  return false;
131  }
132 
133  };
134 
135 }}
136 
137 #endif /* _DECAF_UTIL_LRUCACHE_H_ */
virtual ~LRUCache()
Definition: LRUCache.h:96
LRUCache(int maximumCacheSize)
Constructs a LRUCache with a maximum capacity.
Definition: LRUCache.h:62
int maxCacheSize
Definition: LRUCache.h:46
LRUCache(int initialCapacity, int maximumCacheSize, float loadFactor, bool accessOrder)
Constructs an empty LRUCache instance with the specified initial capacity, maximumCacheSize, load factor and ordering mode.
Definition: LRUCache.h:87
LRUCache()
Default constructor for an LRU Cache The default capacity is 10000.
Definition: LRUCache.h:54
A Basic Least Recently Used (LRU) Cache Map.
Definition: LRUCache.h:43
int getMaxCacheSize() const
Gets the currently configured Max Cache Size setting.
Definition: LRUCache.h:103
Hashed and linked list implementation of the Map interface, with predictable iteration order...
Definition: LinkedHashMap.h:111
virtual int size() const
Definition: HashMap.h:943
Definition: IllegalArgumentException.h:31
Definition: MapEntry.h:27
virtual bool removeEldestEntry(const MapEntry< K, V > &eldest DECAF_UNUSED)
This method is queried from the put and putAll methods to check if the eldest member of the map shoul...
Definition: LRUCache.h:126
#define DECAF_UNUSED
Definition: Config.h:160
float loadFactor
Definition: HashMap.h:760
void setMaxCacheSize(int size)
Sets the maximum size allowed for this LRU Cache.
Definition: LRUCache.h:115
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: AprPool.h:25