LSST Applications  21.0.0-142-gef555c1e+42c9bccae2,22.0.0+052faf71bd,22.0.0+1c4650f311,22.0.0+40ce427c77,22.0.0+5b6c068b1a,22.0.0+7589c3a021,22.0.0+81ed51be6d,22.0.1-1-g7d6de66+6cae67f2c6,22.0.1-1-g87000a6+314cd8b7ea,22.0.1-1-g8760c09+052faf71bd,22.0.1-1-g8e32f31+5b6c068b1a,22.0.1-10-g779eefa+a163f08322,22.0.1-12-g3bd7ecb+bbeacc25a9,22.0.1-15-g63cc0c1+2a7037787d,22.0.1-17-ge5a99e88+3d2c1afe2e,22.0.1-19-g88addfe+6cae67f2c6,22.0.1-2-g1cb3e5b+84de06d286,22.0.1-2-g8ef0a89+6cae67f2c6,22.0.1-2-g92698f7+1c4650f311,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gb66926d+5b6c068b1a,22.0.1-2-gcb770ba+0723a13595,22.0.1-2-ge470956+ff9f1dc8d5,22.0.1-22-g608e23ac+2ac85e833c,22.0.1-29-g184b6e44e+8b185d4e2d,22.0.1-3-g59f966b+11ba4df19d,22.0.1-3-g8c1d971+f90df4c6d0,22.0.1-3-g997b569+d69a7aa2f8,22.0.1-3-gaaec9c0+4d194bf81c,22.0.1-4-g1930a60+283d9d2f1a,22.0.1-4-g5b7b756+c1283a92b8,22.0.1-4-g8623105+6cae67f2c6,22.0.1-7-gba73697+283d9d2f1a,22.0.1-8-g47d23f5+43acea82f3,master-g5f2689bdc5+40ce427c77,w.2021.38
LSST Data Management Base Package
Classes | Public Member Functions | List of all members
lsst::utils::Cache< Key, Value, KeyHash, KeyPred > Class Template Reference

Cache of most recently used values. More...

#include <Cache.h>

Public Member Functions

 Cache (std::size_t maxElements=0)
 Ctor. More...
 
 Cache (Cache const &)=default
 
 Cache (Cache &&)=default
 
Cacheoperator= (Cache const &)=default
 
Cacheoperator= (Cache &&)=default
 
 ~Cache ()=default
 Dtor. More...
 
template<typename Generator >
Value operator() (Key const &key, Generator func)
 Lookup or generate a value. More...
 
Value operator[] (Key const &key)
 
void add (Key const &key, Value const &value)
 Add a value to the cache. More...
 
std::size_t size () const
 Return the number of values in the cache. More...
 
std::vector< Key > keys () const
 Return all keys in the cache, most recent first. More...
 
bool contains (Key const &key)
 Does the cache contain the key? More...
 
std::size_t capacity () const
 Return the capacity of the cache. More...
 
void reserve (std::size_t maxElements)
 Change the capacity of the cache. More...
 
void flush ()
 Empty the cache. More...
 

Detailed Description

template<typename Key, typename Value, typename KeyHash, typename KeyPred>
class lsst::utils::Cache< Key, Value, KeyHash, KeyPred >

Cache of most recently used values.

This object stores the most recent maxElements values, where maxElements is set in the constructor. Objects (of type Value) are stored by a key (of type Key; hence the need to provide a KeyHash and KeyPred), and the class presents a dict-like interface. Objects may be added to (add) and retrieved from (operator[]) the cache. For ease of use, an interface (operator()) is also provided that will check the cache for an existing key, and if the key is not present, generate it with a function provided by the user.

Note
Value and Key must be copyable.
This header (Cache.h) should generally only be included in source files, not other header files, because you probably don't want all of the boost::multi_index includes in your header. We suggest you se the CacheFwd.h file in your header instead, and hold the Cache as a std::unique_ptr.
Python bindings (for pybind11) are available in lsst/utils/python/Cache.h.

Definition at line 74 of file Cache.h.

Constructor & Destructor Documentation

◆ Cache() [1/3]

template<typename Key , typename Value , typename KeyHash , typename KeyPred >
lsst::utils::Cache< Key, Value, KeyHash, KeyPred >::Cache ( std::size_t  maxElements = 0)
inline

Ctor.

The maximum number of elements may be zero (default), in which case the cache is permitted to grow without limit.

Exception Safety
Strong exception safety: exceptions will return the system to previous state.

Definition at line 84 of file Cache.h.

84  : _maxElements(maxElements) {
85  _container.template get<Hash>().reserve(maxElements);
86 #ifdef LSST_CACHE_DEBUG
87  _debuggingEnabled = false;
88  _hits = 0;
89  _total = 0;
90  _requests.reserve(maxElements);
91 #endif
92  }

◆ Cache() [2/3]

template<typename Key , typename Value , typename KeyHash , typename KeyPred >
lsst::utils::Cache< Key, Value, KeyHash, KeyPred >::Cache ( Cache< Key, Value, KeyHash, KeyPred > const &  )
default

◆ Cache() [3/3]

template<typename Key , typename Value , typename KeyHash , typename KeyPred >
lsst::utils::Cache< Key, Value, KeyHash, KeyPred >::Cache ( Cache< Key, Value, KeyHash, KeyPred > &&  )
default

◆ ~Cache()

template<typename Key , typename Value , typename KeyHash , typename KeyPred >
lsst::utils::Cache< Key, Value, KeyHash, KeyPred >::~Cache ( )
default

Dtor.

Member Function Documentation

◆ add()

template<typename Key , typename Value , typename KeyHash , typename KeyPred >
void lsst::utils::Cache< Key, Value, KeyHash, KeyPred >::add ( Key const &  key,
Value const &  value 
)

Add a value to the cache.

If the key is already in the cache, the existing value will be promoted to the most recently used value.

Exception Safety
Basic exception safety: exceptions will leave the system in a valid but unpredictable state.

Definition at line 300 of file Cache.h.

300  {
301  auto result = _lookup(key);
302  if (!result.second) {
303  _addNew(key, value);
304  }
305 }
py::object result
Definition: _schema.cc:429

◆ capacity()

template<typename Key , typename Value , typename KeyHash , typename KeyPred >
std::size_t lsst::utils::Cache< Key, Value, KeyHash, KeyPred >::capacity ( ) const
inline

Return the capacity of the cache.

Exception Safety
No exceptions can be thrown.

Definition at line 183 of file Cache.h.

183 { return _maxElements; }

◆ contains()

template<typename Key , typename Value , typename KeyHash , typename KeyPred >
bool lsst::utils::Cache< Key, Value, KeyHash, KeyPred >::contains ( Key const &  key)
inline

Does the cache contain the key?

If the key is in the cache, it will be promoted to the most recently used value.

Exception Safety
Basic exception safety: exceptions will leave the system in a valid but unpredictable state.

Definition at line 177 of file Cache.h.

177 { return _lookup(key).second; }

◆ flush()

template<typename Key , typename Value , typename KeyHash , typename KeyPred >
void lsst::utils::Cache< Key, Value, KeyHash, KeyPred >::flush

Empty the cache.

Exception Safety
Basic exception safety: exceptions will leave the system in a valid but unpredictable state.

Definition at line 318 of file Cache.h.

318  {
319  while (size() > 0) {
320  _container.template get<Sequence>().pop_back();
321  }
322 }
std::size_t size() const
Return the number of values in the cache.
Definition: Cache.h:160

◆ keys()

template<typename Key , typename Value , typename KeyHash , typename KeyPred >
std::vector< Key > lsst::utils::Cache< Key, Value, KeyHash, KeyPred >::keys

Return all keys in the cache, most recent first.

Exception Safety
Strong exception safety: exceptions will return the system to previous state.

Definition at line 308 of file Cache.h.

308  {
310  result.reserve(size());
311  for (auto & keyValue : _container.template get<Sequence>()) {
312  result.push_back(keyValue.first);
313  }
314  return result;
315 }

◆ operator()()

template<typename Key , typename Value , typename KeyHash , typename KeyPred >
template<typename Generator >
Value lsst::utils::Cache< Key, Value, KeyHash, KeyPred >::operator() ( Key const &  key,
Generator  func 
)

Lookup or generate a value.

If the key is in the cache, the corresponding value is returned. Otherwise, a value is generated by the provided function which is cached and returned. Thus, the (expensive) Generator function only fires if the corresponding value is not already cached.

The Generator function signature should be:

Value func(Key const& key);

Given the possibility of lambdas, we could have made the required function signature so that it took no arguments. However, it's possible the user has some function that produces a value when given a key, so chose to adopt that signature; any other signature would likely require use of a lambda always.

Exception Safety
Basic exception safety: exceptions will leave the system in a valid but unpredictable state.

Definition at line 276 of file Cache.h.

279  {
280  auto result = _lookup(key);
281  if (result.second) {
282  return result.first->second;
283  }
284  Value value = func(key);
285  _addNew(key, value);
286  return value;
287 }

◆ operator=() [1/2]

template<typename Key , typename Value , typename KeyHash , typename KeyPred >
Cache& lsst::utils::Cache< Key, Value, KeyHash, KeyPred >::operator= ( Cache< Key, Value, KeyHash, KeyPred > &&  )
default

◆ operator=() [2/2]

template<typename Key , typename Value , typename KeyHash , typename KeyPred >
Cache& lsst::utils::Cache< Key, Value, KeyHash, KeyPred >::operator= ( Cache< Key, Value, KeyHash, KeyPred > const &  )
default

◆ operator[]()

template<typename Key , typename Value , typename KeyHash , typename KeyPred >
Value lsst::utils::Cache< Key, Value, KeyHash, KeyPred >::operator[] ( Key const &  key)

Definition at line 290 of file Cache.h.

290  {
291  auto result = _lookup(key);
292  if (result.second) {
293  return result.first->second;
294  }
295  throw LSST_EXCEPT(pex::exceptions::NotFoundError,
296  (boost::format("Unable to find key: %s") % key).str());
297 }
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174

◆ reserve()

template<typename Key , typename Value , typename KeyHash , typename KeyPred >
void lsst::utils::Cache< Key, Value, KeyHash, KeyPred >::reserve ( std::size_t  maxElements)
inline

Change the capacity of the cache.

Exception Safety
Basic exception safety: exceptions will leave the system in a valid but unpredictable state.

Definition at line 190 of file Cache.h.

190 { _maxElements = maxElements; _trim(); }

◆ size()

template<typename Key , typename Value , typename KeyHash , typename KeyPred >
std::size_t lsst::utils::Cache< Key, Value, KeyHash, KeyPred >::size ( ) const
inline

Return the number of values in the cache.

Exception Safety
Strong exception safety: exceptions will return the system to previous state.

Definition at line 160 of file Cache.h.

160 { return _container.size(); }

The documentation for this class was generated from the following file: