Cache.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #pragma once
  2. #include "HashMap.h"
  3. #ifdef WIN32
  4. # include "Time.h"
  5. #else
  6. # include <sys/time.h>
  7. #endif
  8. namespace Framework
  9. {
  10. enum class CacheCleanupStrategy
  11. {
  12. LONGEST_NOT_USED,
  13. OLDEST,
  14. RANDOM
  15. };
  16. template<typename T> struct CacheEntry
  17. {
  18. __int64 created;
  19. __int64 lastUsed;
  20. T value;
  21. };
  22. template<typename K, typename T> class CacheIterator
  23. {
  24. private:
  25. MapIterator<K, CacheEntry<T>> delegate;
  26. public:
  27. CacheIterator(MapIterator<K, CacheEntry<T>> delegate)
  28. : delegate(delegate)
  29. {}
  30. CacheIterator(const CacheIterator<K, T>& it)
  31. {
  32. delegate = it.delegate;
  33. }
  34. CacheIterator<K, T>& operator=(CacheIterator<K, T> r)
  35. {
  36. delegate = r.delegate;
  37. return *this;
  38. }
  39. bool hasNext()
  40. {
  41. return delegate.hasNext();
  42. }
  43. MapEntry<K, T> operator*()
  44. {
  45. return MapEntry<K, T>(delegate.operator->().getKey(),
  46. delegate.operator->().getValue().value);
  47. }
  48. CacheIterator<K, T> next()
  49. {
  50. return CacheIterator(delegate.next());
  51. }
  52. operator bool()
  53. {
  54. return delegate;
  55. }
  56. operator MapEntry<K, T>()
  57. {
  58. return MapEntry<K, T>(
  59. delegate->getKey(), delegate->getValue().value);
  60. }
  61. CacheIterator<K, T>& operator++() //! prefix
  62. {
  63. ++delegate;
  64. return *this;
  65. }
  66. CacheIterator<K, T> operator++(int) //! postfix
  67. {
  68. CacheIterator<K, T> temp(*this);
  69. ++delegate;
  70. return temp;
  71. }
  72. MapEntry<K, T> operator->()
  73. {
  74. return MapEntry<K, T>(
  75. delegate->getKey(), delegate->getValue().value);
  76. }
  77. void set(MapEntry<K, T> val)
  78. {
  79. time_t zeit = time(0);
  80. delegate.set(MapEntry<K, CacheEntry<T>>(
  81. val.getKey(), CacheEntry<T>{zeit, zeit, val.getValue()}));
  82. }
  83. bool operator!=(CacheIterator<K, T>& r)
  84. {
  85. return delegate != r.delegate;
  86. }
  87. };
  88. template<typename K, typename T> class Cache
  89. : public virtual ReferenceCounter
  90. {
  91. private:
  92. HashMap<K, CacheEntry<T>>* cache;
  93. CacheCleanupStrategy cleanup;
  94. int maxSize;
  95. void removeOneElement()
  96. {
  97. switch (cleanup)
  98. {
  99. case CacheCleanupStrategy::LONGEST_NOT_USED:
  100. {
  101. K key;
  102. __int64 t = -1;
  103. for (MapEntry<K, CacheEntry<T>> e : *cache)
  104. {
  105. if (t < 0 || t > e.getValue().lastUsed)
  106. {
  107. t = e.getValue().lastUsed;
  108. key = e.getKey();
  109. }
  110. }
  111. if (t >= 0) cache->remove(key);
  112. break;
  113. }
  114. case CacheCleanupStrategy::OLDEST:
  115. {
  116. K key;
  117. __int64 t = -1;
  118. for (MapEntry<K, CacheEntry<T>> e : *cache)
  119. {
  120. if (t < 0 || t > e.getValue().created)
  121. {
  122. t = e.getValue().created;
  123. key = e.getKey();
  124. }
  125. }
  126. if (t >= 0) cache->remove(key);
  127. break;
  128. }
  129. case CacheCleanupStrategy::RANDOM:
  130. {
  131. int size = cache->getSize();
  132. int index = (int)(((double)rand() / RAND_MAX) * size);
  133. auto it = cache->begin();
  134. for (int i = 0; i < index; i++)
  135. ++it;
  136. cache->remove(it.operator->().getKey());
  137. break;
  138. }
  139. }
  140. }
  141. public:
  142. Cache(
  143. int size, std::function<int(K)> hash, CacheCleanupStrategy cleanup)
  144. : ReferenceCounter(),
  145. cleanup(cleanup),
  146. maxSize(size)
  147. {
  148. cache = new HashMap<K, CacheEntry<T>>(size, hash);
  149. }
  150. ~Cache()
  151. {
  152. cache->release();
  153. }
  154. void put(K key, T value)
  155. {
  156. time_t zeit = time(0);
  157. if (cache->getSize() >= maxSize && !cache->has(key))
  158. removeOneElement();
  159. cache->put(key, {zeit, zeit, value});
  160. }
  161. void remove(K key)
  162. {
  163. cache->remove(key);
  164. }
  165. T get(K key) const
  166. {
  167. CacheEntry<T> entry = cache->get(key);
  168. entry.lastUsed = time(0);
  169. cache->put(key, entry);
  170. return entry.value;
  171. }
  172. bool has(K key) const
  173. {
  174. return cache->has(key);
  175. }
  176. int getCurrentSize() const
  177. {
  178. return cache->getSize();
  179. }
  180. int getMaxSize() const
  181. {
  182. return maxSize;
  183. }
  184. void reset() const
  185. {
  186. cache->clear();
  187. }
  188. CacheIterator<K, T> begin()
  189. {
  190. return CacheIterator<K, T>(cache->begin());
  191. }
  192. CacheIterator<K, T> end()
  193. {
  194. return CacheIterator<K, T>(cache->end());
  195. }
  196. };
  197. } // namespace Framework