activemq-cpp-3.9.5
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
26namespace decaf {
27namespace 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
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_ */
Definition IllegalArgumentException.h:31
virtual int size() const
Definition HashMap.h:953
float loadFactor
Definition HashMap.h:760
LRUCache(int initialCapacity, int maximumCacheSize, float loadFactor, bool accessOrder)
Constructs an empty LRUCache instance with the specified initial capacity, maximumCacheSize,...
Definition LRUCache.h:87
virtual ~LRUCache()
Definition LRUCache.h:96
int maxCacheSize
Definition LRUCache.h:46
void setMaxCacheSize(int size)
Sets the maximum size allowed for this LRU Cache.
Definition LRUCache.h:115
LRUCache()
Default constructor for an LRU Cache The default capacity is 10000.
Definition LRUCache.h:54
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
LRUCache(int maximumCacheSize)
Constructs a LRUCache with a maximum capacity.
Definition LRUCache.h:62
int getMaxCacheSize() const
Gets the currently configured Max Cache Size setting.
Definition LRUCache.h:103
LinkedHashMap()
Constructs an empty insertion-ordered LinkedHashMap instance with the default initial capacity (16) a...
Definition LinkedHashMap.h:609
Definition MapEntry.h:27
Definition AbstractCollection.h:33
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
Definition AprPool.h:25