proxygen
wangle::LRUInMemoryCache< K, V, MutexT > Class Template Reference

#include <LRUInMemoryCache.h>

Public Member Functions

 LRUInMemoryCache (size_t capacity)
 
 ~LRUInMemoryCache ()=default
 
folly::Optional< V > get (const K &key)
 
void put (const K &key, const V &val)
 
bool remove (const K &key)
 
size_t size () const
 
void clear ()
 
CacheDataVersion getVersion () const
 
CacheDataVersion loadData (const folly::dynamic &kvPairs) noexcept
 
folly::Optional< std::pair< folly::dynamic, CacheDataVersion > > convertToKeyValuePairs () noexcept
 
bool hasChangedSince (CacheDataVersion version) const
 

Private Member Functions

void incrementVersion ()
 

Private Attributes

folly::EvictingCacheMap< K, V > cache_
 
CacheDataVersion version_ {1}
 
MutexT cacheLock_
 

Detailed Description

template<typename K, typename V, typename MutexT>
class wangle::LRUInMemoryCache< K, V, MutexT >

A threadsafe cache map that delegates to an EvictingCacheMap and maintains a version of the data.

Definition at line 32 of file LRUInMemoryCache.h.

Constructor & Destructor Documentation

template<typename K, typename V, typename MutexT>
wangle::LRUInMemoryCache< K, V, MutexT >::LRUInMemoryCache ( size_t  capacity)
inlineexplicit

Create with the specified capacity.

Definition at line 37 of file LRUInMemoryCache.h.

37 : cache_(capacity) {}
folly::EvictingCacheMap< K, V > cache_
template<typename K, typename V, typename MutexT>
wangle::LRUInMemoryCache< K, V, MutexT >::~LRUInMemoryCache ( )
default

Member Function Documentation

template<typename K , typename V , typename M >
void wangle::LRUInMemoryCache< K, V, M >::clear ( )

Definition at line 59 of file LRUInMemoryCache-inl.h.

Referenced by wangle::LRUInMemoryCache< K, V, M >::LRUInMemoryCache().

59  {
60  typename wangle::CacheLockGuard<M>::Write writeLock(cacheLock_);
61  if (cache_.empty()) {
62  return;
63  }
64  cache_.clear();
66 }
folly::EvictingCacheMap< K, V > cache_
void clear(PruneHookCall pruneHook=nullptr)
template<typename K , typename V , typename M >
folly::Optional< std::pair< folly::dynamic, CacheDataVersion > > wangle::LRUInMemoryCache< K, V, M >::convertToKeyValuePairs ( )
noexcept

Get the cache data as a list of kv pairs along with the version

Definition at line 105 of file LRUInMemoryCache-inl.h.

References folly::dynamic::array(), folly::gen::move, folly::none, folly::dynamic::push_back(), and folly::toDynamic().

Referenced by wangle::LRUInMemoryCache< K, V, M >::LRUInMemoryCache().

105  {
106  typename wangle::CacheLockGuard<M>::Read readLock(cacheLock_);
107  try {
109  for (const auto& kv : cache_) {
110  dynObj.push_back(folly::toDynamic(std::make_pair(kv.first, kv.second)));
111  }
112  return std::make_pair(std::move(dynObj), version_);
113  } catch (const std::exception& err) {
114  LOG(ERROR) << "Converting cache to folly::dynamic failed with error: "
115  << err.what();
116  }
117  return folly::none;
118 }
folly::EvictingCacheMap< K, V > cache_
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
CacheDataVersion version_
void push_back(dynamic const &)
Definition: dynamic-inl.h:969
static void array(EmptyArrayTag)
Definition: dynamic-inl.h:233
dynamic toDynamic(const T &)
constexpr None none
Definition: Optional.h:87
template<typename K, typename V , typename M >
folly::Optional< V > wangle::LRUInMemoryCache< K, V, M >::get ( const K &  key)

Definition at line 24 of file LRUInMemoryCache-inl.h.

References folly::none.

24  {
25  // need to take a write lock since get modifies the LRU
26  typename wangle::CacheLockGuard<M>::Write writeLock(cacheLock_);
27  auto itr = cache_.find(key);
28  if (itr != cache_.end()) {
29  return folly::Optional<V>(itr->second);
30  }
31  return folly::none;
32 }
folly::EvictingCacheMap< K, V > cache_
iterator find(const TKey &key)
constexpr None none
Definition: Optional.h:87
template<typename K , typename V , typename M >
CacheDataVersion wangle::LRUInMemoryCache< K, V, M >::getVersion ( ) const
template<typename K, typename V, typename MutexT>
bool wangle::LRUInMemoryCache< K, V, MutexT >::hasChangedSince ( CacheDataVersion  version) const
inline

Determine if the cache has changed since the specified version

Definition at line 63 of file LRUInMemoryCache.h.

63  {
64  return getVersion() != version;
65  }
CacheDataVersion getVersion() const
ProtocolVersion version
template<typename K, typename V, typename MutexT>
void wangle::LRUInMemoryCache< K, V, MutexT >::incrementVersion ( )
inlineprivate

Definition at line 70 of file LRUInMemoryCache.h.

70  {
71  ++version_;
72  // if a uint64_t is incremented a billion times a second, it will still take
73  // 585 years to wrap around, so don't bother
74  }
CacheDataVersion version_
template<typename K , typename V , typename M >
CacheDataVersion wangle::LRUInMemoryCache< K, V, M >::loadData ( const folly::dynamic kvPairs)
noexcept

Loads the list of kv pairs into the cache and bumps version. Returns the new cache version.

Definition at line 76 of file LRUInMemoryCache-inl.h.

References data.

Referenced by wangle::LRUInMemoryCache< K, V, M >::LRUInMemoryCache().

76  {
77  bool updated = false;
78  typename wangle::CacheLockGuard<M>::Write writeLock(cacheLock_);
79  try {
80  for (const auto& kv : data) {
81  cache_.set(
82  folly::convertTo<K>(kv[0]),
83  folly::convertTo<V>(kv[1]));
84  updated = true;
85  }
86  } catch (const folly::TypeError& err) {
87  LOG(ERROR) << "Load cache failed with type error: "
88  << err.what();
89  } catch (const std::out_of_range& err) {
90  LOG(ERROR) << "Load cache failed with key error: "
91  << err.what();
92  } catch (const std::exception& err) {
93  LOG(ERROR) << "Load cache failed with error: "
94  << err.what();
95  }
96  if (updated) {
97  // we still need to increment the version
99  }
100  return version_;
101 }
folly::EvictingCacheMap< K, V > cache_
void set(const TKey &key, TValue value, bool promote=true, PruneHookCall pruneHook=nullptr)
CacheDataVersion version_
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
template<typename K, typename V, typename M >
void wangle::LRUInMemoryCache< K, V, M >::put ( const K &  key,
const V &  val 
)

Definition at line 35 of file LRUInMemoryCache-inl.h.

Referenced by wangle::LRUInMemoryCache< K, V, M >::LRUInMemoryCache().

35  {
36  typename wangle::CacheLockGuard<M>::Write writeLock(cacheLock_);
37  cache_.set(key, val);
39 }
folly::EvictingCacheMap< K, V > cache_
void set(const TKey &key, TValue value, bool promote=true, PruneHookCall pruneHook=nullptr)
double val
Definition: String.cpp:273
template<typename K, typename V , typename M >
bool wangle::LRUInMemoryCache< K, V, M >::remove ( const K &  key)

Definition at line 42 of file LRUInMemoryCache-inl.h.

42  {
43  typename wangle::CacheLockGuard<M>::Write writeLock(cacheLock_);
44  size_t nErased = cache_.erase(key);
45  if (nErased > 0) {
47  return true;
48  }
49  return false;
50 }
folly::EvictingCacheMap< K, V > cache_
bool erase(const TKey &key)
template<typename K , typename V , typename M >
size_t wangle::LRUInMemoryCache< K, V, M >::size ( ) const

Definition at line 53 of file LRUInMemoryCache-inl.h.

Referenced by wangle::LRUInMemoryCache< K, V, M >::LRUInMemoryCache().

53  {
55  return cache_.size();
56 }
std::size_t size() const
folly::EvictingCacheMap< K, V > cache_

Member Data Documentation

template<typename K, typename V, typename MutexT>
folly::EvictingCacheMap<K, V> wangle::LRUInMemoryCache< K, V, MutexT >::cache_
private

Definition at line 76 of file LRUInMemoryCache.h.

template<typename K, typename V, typename MutexT>
MutexT wangle::LRUInMemoryCache< K, V, MutexT >::cacheLock_
mutableprivate

Definition at line 80 of file LRUInMemoryCache.h.

template<typename K, typename V, typename MutexT>
CacheDataVersion wangle::LRUInMemoryCache< K, V, MutexT >::version_ {1}
private

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