Mantid
Loading...
Searching...
No Matches
Cache.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2007 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
9//----------------------------------------------------------------------
10// Includes
11//----------------------------------------------------------------------
12#include "MantidKernel/DllConfig.h"
14#include <map>
15
16#include <mutex>
17
18namespace Mantid {
19namespace Kernel {
27template <class KEYTYPE, class VALUETYPE> class DLLExport Cache {
28public:
30 Cache() : m_cacheHit(0), m_cacheMiss(0), m_cacheMap(), m_mutex() {}
31
37 : m_cacheHit(src.m_cacheHit), m_cacheMiss(src.m_cacheMiss), m_cacheMap(src.m_cacheMap),
38 m_mutex() // New mutex which is unlocked
39 {}
40
46 if (this == &rhs)
47 return *this; // handle self-assignment
48 m_cacheHit = rhs.m_cacheHit;
49 m_cacheMiss = rhs.m_cacheMiss;
50 m_cacheMap = rhs.m_cacheMap;
51 // mutex is untouched
52 return *this;
53 }
54
56 void clear() {
57 std::lock_guard<std::mutex> lock(m_mutex);
58 m_cacheHit = 0;
59 m_cacheMiss = 0;
60 m_cacheMap.clear();
61 }
62
64 int size() { return static_cast<int>(m_cacheMap.size()); }
65
67 int hitCount() { return m_cacheHit; }
69 int missCount() { return m_cacheMiss; }
72 double hitRatio() {
73 double hitRatio = 0.0;
74 if ((m_cacheHit + m_cacheMiss) > 0) {
75 hitRatio = 100.0 * (m_cacheHit * 1.0) / (m_cacheHit + m_cacheMiss);
76 }
77 return hitRatio;
78 }
79
85 void setCache(const KEYTYPE &key, const VALUETYPE &value) {
86 std::lock_guard<std::mutex> lock(m_mutex);
87 m_cacheMap[key] = value;
88 }
89
98 bool getCache(const KEYTYPE &key, VALUETYPE &value) const {
99#ifdef USE_CACHE_STATS
100 bool found = getCacheNoStats(key, value);
101 if (found) {
103 m_cacheHit++;
104 } else {
106 m_cacheMiss++;
107 }
108 return found;
109#else
110 return getCacheNoStats(key, value);
111#endif
112 }
113
119 void removeCache(const KEYTYPE &key) {
120 std::lock_guard<std::mutex> lock(m_mutex);
121 m_cacheMap.erase(key);
122 }
123
124private:
132 bool getCacheNoStats(const KEYTYPE key, VALUETYPE &value) const {
133 std::lock_guard<std::mutex> lock(m_mutex);
134 auto it_found = m_cacheMap.find(key);
135 bool isValid = it_found != m_cacheMap.end();
136
137 if (isValid) {
138 value = it_found->second;
139 }
140
141 return isValid;
142 }
143
145 mutable int m_cacheHit;
148 mutable int m_cacheMiss;
150 std::map<KEYTYPE, VALUETYPE> m_cacheMap;
152 mutable std::mutex m_mutex;
154 using CacheMapIterator = typename std::map<KEYTYPE, VALUETYPE>::iterator;
156 using CacheMapConstIterator = typename std::map<KEYTYPE, VALUETYPE>::const_iterator;
157};
158
159} // namespace Kernel
160} // namespace Mantid
const std::vector< double > & rhs
double value
The value of the point.
Definition: FitMW.cpp:51
#define PARALLEL_ATOMIC
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
Definition: System.h:53
Cache is a generic caching storage class.
Definition: Cache.h:27
Cache()
No-arg Constructor.
Definition: Cache.h:30
void setCache(const KEYTYPE &key, const VALUETYPE &value)
Inserts/updates a cached value with the given key.
Definition: Cache.h:85
void clear()
Clears the cache.
Definition: Cache.h:56
int m_cacheMiss
total number of times the cache has not contained the requested information
Definition: Cache.h:148
void removeCache(const KEYTYPE &key)
Attempts to remove a value from the cache.
Definition: Cache.h:119
std::map< KEYTYPE, VALUETYPE > m_cacheMap
internal cache map
Definition: Cache.h:150
double hitRatio()
total number of times the cache has contained the requested information/the total number of requests
Definition: Cache.h:72
int missCount()
total number of times the cache has contained the requested information
Definition: Cache.h:69
std::mutex m_mutex
internal mutex
Definition: Cache.h:152
Cache< KEYTYPE, VALUETYPE > & operator=(const Cache< KEYTYPE, VALUETYPE > &rhs)
Copy-assignment operator as we have a non-default copy constructor.
Definition: Cache.h:45
Cache(const Cache< KEYTYPE, VALUETYPE > &src)
Copy constructor (mutex cannot be copied)
Definition: Cache.h:36
bool getCacheNoStats(const KEYTYPE key, VALUETYPE &value) const
Attempts to retrieve a value from the cache.
Definition: Cache.h:132
int size()
The number of cache entries.
Definition: Cache.h:64
typename std::map< KEYTYPE, VALUETYPE >::const_iterator CacheMapConstIterator
const_iterator typedef
Definition: Cache.h:156
int hitCount()
total number of times the cache has contained the requested information
Definition: Cache.h:67
bool getCache(const KEYTYPE &key, VALUETYPE &value) const
Attempts to retrieve a value from the cache, with optional cache stats tracking.
Definition: Cache.h:98
typename std::map< KEYTYPE, VALUETYPE >::iterator CacheMapIterator
iterator typedef
Definition: Cache.h:154
int m_cacheHit
total number of times the cache has contained the requested information
Definition: Cache.h:145
Helper class which provides the Collimation Length for SANS instruments.