proxygen
|
#include <LRUPersistentCache.h>
Public Types | |
using | Ptr = std::shared_ptr< LRUPersistentCache< K, V, MutexT >> |
Public Member Functions | |
LRUPersistentCache (std::size_t cacheCapacity, std::chrono::milliseconds syncInterval=client::persistence::DEFAULT_CACHE_SYNC_INTERVAL, int nSyncRetries=client::persistence::DEFAULT_CACHE_SYNC_RETRIES, std::unique_ptr< CachePersistence< K, V >> persistence=nullptr) | |
LRUPersistentCache (std::shared_ptr< folly::Executor > executor, std::size_t cacheCapacity, std::chrono::milliseconds syncInterval, int nSyncRetries, std::unique_ptr< CachePersistence< K, V >> persistence) | |
~LRUPersistentCache () override | |
bool | hasPendingUpdates () |
folly::Optional< V > | get (const K &key) override |
void | put (const K &key, const V &val) override |
bool | remove (const K &key) override |
void | clear (bool clearPersistence=false) override |
size_t | size () override |
void | setSyncOnDestroy (bool syncOnDestroy) |
void | setPersistence (std::unique_ptr< CachePersistence< K, V >> persistence) |
Public Member Functions inherited from wangle::PersistentCache< K, V > | |
virtual | ~PersistentCache () |
Private Member Functions | |
void | setPersistenceHelper (std::unique_ptr< CachePersistence< K, V >> persistence, bool syncVersion) noexcept |
CacheDataVersion | load (CachePersistence< K, V > &persistence) noexcept |
void | sync () |
void | oneShotSync () |
bool | syncNow (CachePersistence< K, V > &persistence) |
std::shared_ptr< CachePersistence< K, V > > | getPersistence () |
Static Private Member Functions | |
static void * | syncThreadMain (void *arg) |
Private Attributes | |
LRUInMemoryCache< K, V, MutexT > | cache_ |
bool | stopSyncer_ {false} |
std::mutex | stopSyncerMutex_ |
std::condition_variable | stopSyncerCV_ |
std::atomic_flag | executorScheduled_ = ATOMIC_FLAG_INIT |
const std::chrono::milliseconds | syncInterval_ |
const int | nSyncRetries_ {client::persistence::DEFAULT_CACHE_SYNC_RETRIES} |
int | nSyncTries_ {0} |
std::chrono::steady_clock::time_point | lastExecutorScheduleTime_ |
bool | syncOnDestroy_ {false} |
std::shared_ptr< CachePersistence< K, V > > | persistence_ |
MutexT | persistenceLock_ |
std::thread | syncer_ |
std::shared_ptr< folly::Executor > | executor_ |
A PersistentCache implementation that used a CachePersistence for storage. In memory structure fronts the persistence and the cache operations happen on it. Loading from and syncing to persistence are hidden from clients. Sync to persistence happens asynchronously on a separate thread at a configurable interval. Syncs to persistence on destruction as well.
The in memory structure is an EvictingCacheMap which causes this class to evict entries in an LRU fashion.
NOTE NOTE NOTE: Although this class aims to be a cache for arbitrary, it relies heavily on folly::toJson, folly::dynamic and convertTo for serialization and deserialization. So It may not suit your need until true support arbitrary types is written.
Definition at line 119 of file LRUPersistentCache.h.
using wangle::LRUPersistentCache< K, V, MutexT >::Ptr = std::shared_ptr<LRUPersistentCache<K, V, MutexT>> |
Definition at line 124 of file LRUPersistentCache.h.
|
explicit |
LRUPersistentCache constructor
cacheCapacity | max number of elements to hold in the cache. |
syncInterval | how often to sync to the persistence (in ms). |
nSyncRetries | how many times to retry to sync on failure. |
Loads the cache and starts of the syncer thread that periodically syncs the cache to persistence.
If persistence is specified, the cache is initially loaded with the contents from it. If load fails, then cache starts empty.
On write failures, the sync will happen again up to nSyncRetries times. Once failed nSyncRetries amount of time, then it will give up and not attempt to sync again until another update occurs.
On reaching capacity limit, LRU items are evicted.
Definition at line 31 of file LRUPersistentCache-inl.h.
wangle::LRUPersistentCache< K, V, MutexT >::LRUPersistentCache | ( | std::shared_ptr< folly::Executor > | executor, |
std::size_t | cacheCapacity, | ||
std::chrono::milliseconds | syncInterval, | ||
int | nSyncRetries, | ||
std::unique_ptr< CachePersistence< K, V >> | persistence | ||
) |
Definition at line 45 of file LRUPersistentCache-inl.h.
References wangle::LRUPersistentCache< K, V, MutexT >::executor_, folly::gen::move, wangle::LRUPersistentCache< K, V, MutexT >::setPersistenceHelper(), and wangle::LRUPersistentCache< K, V, MutexT >::syncer_.
|
override |
LRUPersistentCache Destructor
Signals the syncer thread to stop, waits for any pending syncs to be done.
Definition at line 69 of file LRUPersistentCache-inl.h.
References wangle::LRUPersistentCache< K, V, MutexT >::executor_, folly::detail::lock(), wangle::LRUPersistentCache< K, V, MutexT >::oneShotSync(), wangle::LRUPersistentCache< K, V, MutexT >::stopSyncer_, wangle::LRUPersistentCache< K, V, MutexT >::stopSyncerCV_, wangle::LRUPersistentCache< K, V, MutexT >::stopSyncerMutex_, wangle::LRUPersistentCache< K, V, MutexT >::syncer_, and wangle::LRUPersistentCache< K, V, MutexT >::syncOnDestroy_.
|
inlineoverridevirtual |
Empty the contents of the cache
Implements wangle::PersistentCache< K, V >.
Definition at line 184 of file LRUPersistentCache.h.
Referenced by wangle::FilePersistentCache< K, V, M >::clear().
|
inlineoverridevirtual |
PersistentCache operations
Implements wangle::PersistentCache< K, V >.
Definition at line 174 of file LRUPersistentCache.h.
Referenced by wangle::FilePersistentCache< K, V, M >::get().
|
private |
Helper to get the persistence layer under lock since it will be called by syncer thread and setters call from any thread.
Definition at line 206 of file LRUPersistentCache-inl.h.
References wangle::LRUPersistentCache< K, V, MutexT >::persistence_, and wangle::LRUPersistentCache< K, V, MutexT >::persistenceLock_.
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::oneShotSync(), and wangle::LRUPersistentCache< K, V, MutexT >::sync().
bool wangle::LRUPersistentCache< K, V, MutexT >::hasPendingUpdates | ( | ) |
Check if there are updates that need to be synced to persistence
Definition at line 112 of file LRUPersistentCache-inl.h.
References wangle::LRUPersistentCache< K, V, MutexT >::cache_, wangle::LRUPersistentCache< K, V, MutexT >::persistence_, and wangle::LRUPersistentCache< K, V, MutexT >::persistenceLock_.
|
privatenoexcept |
Load the contents of the persistence passed to constructor in to the in-memory cache. Failure to read will result in no changes to the in-memory data. That is, if in-memory entries exist, and loading fails, the in-memory data remains and will sync down to the underlying persistence layer on the next sync.
Failure to read inclues IO errors and deserialization errors.
Definition at line 236 of file LRUPersistentCache-inl.h.
References wangle::LRUPersistentCache< K, V, MutexT >::cache_.
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::setPersistenceHelper().
|
private |
Definition at line 130 of file LRUPersistentCache-inl.h.
References wangle::LRUPersistentCache< K, V, MutexT >::cache_, wangle::LRUPersistentCache< K, V, MutexT >::executorScheduled_, wangle::LRUPersistentCache< K, V, MutexT >::getPersistence(), wangle::LRUPersistentCache< K, V, MutexT >::nSyncRetries_, wangle::LRUPersistentCache< K, V, MutexT >::nSyncTries_, and wangle::LRUPersistentCache< K, V, MutexT >::syncNow().
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::~LRUPersistentCache().
|
overridevirtual |
Set a value corresponding to a key
key | string, the key to set |
val | string, the value to set |
overwrites value if key has a value associated in the cache
Implements wangle::PersistentCache< K, V >.
Definition at line 90 of file LRUPersistentCache-inl.h.
References wangle::LRUPersistentCache< K, V, MutexT >::cache_, wangle::LRUPersistentCache< K, V, MutexT >::executor_, wangle::LRUPersistentCache< K, V, MutexT >::executorScheduled_, wangle::LRUPersistentCache< K, V, MutexT >::lastExecutorScheduleTime_, folly::detail::lock(), folly::gen::move, now(), and wangle::LRUPersistentCache< K, V, MutexT >::syncInterval_.
Referenced by wangle::FilePersistentCache< K, V, M >::put().
|
inlineoverridevirtual |
Clear a cache entry associated with a key
key | string, the key to lookup and clear |
Implements wangle::PersistentCache< K, V >.
Definition at line 180 of file LRUPersistentCache.h.
Referenced by wangle::FilePersistentCache< K, V, M >::remove().
void wangle::LRUPersistentCache< K, V, MutexT >::setPersistence | ( | std::unique_ptr< CachePersistence< K, V >> | persistence | ) |
Set a new persistence layer on this cache. This call blocks while the new persistence layer is loaded into the cache. The load is also done under a lock so multiple calls to this will not stomp on each other.
Definition at line 227 of file LRUPersistentCache-inl.h.
References folly::gen::move, and wangle::LRUPersistentCache< K, V, MutexT >::setPersistenceHelper().
|
privatenoexcept |
Helper to set persistence that will load the persistence data into memory and optionally sync versions
Definition at line 212 of file LRUPersistentCache-inl.h.
References wangle::LRUPersistentCache< K, V, MutexT >::load(), folly::gen::move, wangle::LRUPersistentCache< K, V, MutexT >::persistence_, wangle::LRUPersistentCache< K, V, MutexT >::persistenceLock_, and version.
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::LRUPersistentCache(), and wangle::LRUPersistentCache< K, V, MutexT >::setPersistence().
|
inline |
Definition at line 198 of file LRUPersistentCache.h.
Referenced by wangle::FilePersistentCache< K, V, M >::setSyncOnDestroy().
|
inlineoverridevirtual |
return the size of the cache
Implements wangle::PersistentCache< K, V >.
Definition at line 194 of file LRUPersistentCache.h.
Referenced by wangle::FilePersistentCache< K, V, M >::size().
|
private |
The syncer thread's function. Syncs to the persistence, if necessary, after every syncInterval_ seconds.
Definition at line 146 of file LRUPersistentCache-inl.h.
References wangle::LRUPersistentCache< K, V, MutexT >::cache_, wangle::LRUPersistentCache< K, V, MutexT >::getPersistence(), wangle::LRUPersistentCache< K, V, MutexT >::nSyncRetries_, wangle::LRUPersistentCache< K, V, MutexT >::stopSyncer_, wangle::LRUPersistentCache< K, V, MutexT >::stopSyncerCV_, wangle::LRUPersistentCache< K, V, MutexT >::stopSyncerMutex_, wangle::LRUPersistentCache< K, V, MutexT >::syncInterval_, and wangle::LRUPersistentCache< K, V, MutexT >::syncNow().
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::syncThreadMain().
|
private |
Helper to sync routine above that actualy does the serialization and writes to persistence.
Definition at line 178 of file LRUPersistentCache-inl.h.
References wangle::LRUPersistentCache< K, V, MutexT >::cache_, wangle::CachePersistence< K, V >::getLastPersistedVersion(), folly::gen::move, wangle::CachePersistence< K, V >::persistVersionedData(), and version.
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::oneShotSync(), and wangle::LRUPersistentCache< K, V, MutexT >::sync().
|
staticprivate |
Definition at line 121 of file LRUPersistentCache-inl.h.
References folly::setThreadName(), and wangle::LRUPersistentCache< K, V, MutexT >::sync().
|
private |
Definition at line 257 of file LRUPersistentCache.h.
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::hasPendingUpdates(), wangle::LRUPersistentCache< K, V, MutexT >::load(), wangle::LRUPersistentCache< K, V, MutexT >::oneShotSync(), wangle::LRUPersistentCache< K, V, MutexT >::put(), wangle::LRUPersistentCache< K, V, MutexT >::sync(), and wangle::LRUPersistentCache< K, V, MutexT >::syncNow().
|
private |
Definition at line 292 of file LRUPersistentCache.h.
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::LRUPersistentCache(), wangle::LRUPersistentCache< K, V, MutexT >::put(), and wangle::LRUPersistentCache< K, V, MutexT >::~LRUPersistentCache().
|
private |
Definition at line 267 of file LRUPersistentCache.h.
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::oneShotSync(), and wangle::LRUPersistentCache< K, V, MutexT >::put().
|
private |
Definition at line 276 of file LRUPersistentCache.h.
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::put().
|
private |
Definition at line 273 of file LRUPersistentCache.h.
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::oneShotSync(), and wangle::LRUPersistentCache< K, V, MutexT >::sync().
|
private |
Definition at line 275 of file LRUPersistentCache.h.
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::oneShotSync().
|
private |
Definition at line 284 of file LRUPersistentCache.h.
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::getPersistence(), wangle::LRUPersistentCache< K, V, MutexT >::hasPendingUpdates(), and wangle::LRUPersistentCache< K, V, MutexT >::setPersistenceHelper().
|
private |
Definition at line 286 of file LRUPersistentCache.h.
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::getPersistence(), wangle::LRUPersistentCache< K, V, MutexT >::hasPendingUpdates(), and wangle::LRUPersistentCache< K, V, MutexT >::setPersistenceHelper().
|
private |
Definition at line 260 of file LRUPersistentCache.h.
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::sync(), and wangle::LRUPersistentCache< K, V, MutexT >::~LRUPersistentCache().
|
private |
Definition at line 264 of file LRUPersistentCache.h.
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::sync(), and wangle::LRUPersistentCache< K, V, MutexT >::~LRUPersistentCache().
|
private |
Definition at line 262 of file LRUPersistentCache.h.
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::sync(), and wangle::LRUPersistentCache< K, V, MutexT >::~LRUPersistentCache().
|
private |
Definition at line 289 of file LRUPersistentCache.h.
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::LRUPersistentCache(), and wangle::LRUPersistentCache< K, V, MutexT >::~LRUPersistentCache().
|
private |
Definition at line 270 of file LRUPersistentCache.h.
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::put(), and wangle::LRUPersistentCache< K, V, MutexT >::sync().
|
private |
Definition at line 279 of file LRUPersistentCache.h.
Referenced by wangle::LRUPersistentCache< K, V, MutexT >::~LRUPersistentCache().