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.
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.
template<typename Key , typename Value , typename KeyHash , typename KeyPred >
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; }
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.
282 return result.first->second;
284 Value value = func(
key);