proxygen
wangle::FilePersistentCache< K, V, M > Class Template Reference

#include <FilePersistentCache.h>

Inheritance diagram for wangle::FilePersistentCache< K, V, M >:
wangle::PersistentCache< K, V >

Public Member Functions

 FilePersistentCache (const std::string &file, std::size_t cacheCapacity, std::chrono::seconds syncInterval=std::chrono::duration_cast< std::chrono::seconds >(client::persistence::DEFAULT_CACHE_SYNC_INTERVAL), int nSyncRetries=client::persistence::DEFAULT_CACHE_SYNC_RETRIES)
 
 FilePersistentCache (std::shared_ptr< folly::Executor > executor, const std::string &file, std::size_t cacheCapacity, std::chrono::seconds syncInterval, int nSyncRetries)
 
 ~FilePersistentCache () override
 
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)
 
- Public Member Functions inherited from wangle::PersistentCache< K, V >
virtual ~PersistentCache ()
 

Private Attributes

LRUPersistentCache< K, V, M >::Ptr cache_
 

Detailed Description

template<typename K, typename V, typename M = std::mutex>
class wangle::FilePersistentCache< K, V, M >

A PersistentCache implementation that used a regular file for storage. In memory structure fronts the file and the cache operations happen on it. Loading from and syncing to file are hidden from clients. Sync to file happens asynchronously on a separate thread at a configurable interval. Syncs to file on destruction as well.

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 40 of file FilePersistentCache.h.

Constructor & Destructor Documentation

template<typename K , typename V , typename M >
wangle::FilePersistentCache< K, V, M >::FilePersistentCache ( const std::string file,
std::size_t  cacheCapacity,
std::chrono::seconds  syncInterval = std::chrono::duration_cast<std::chrono::seconds>(              client::persistence::DEFAULT_CACHE_SYNC_INTERVAL),
int  nSyncRetries = client::persistence::DEFAULT_CACHE_SYNC_RETRIES 
)

Definition at line 114 of file FilePersistentCache-inl.h.

119  : cache_(std::make_shared<LRUPersistentCache<K, V, M>>(
120  cacheCapacity,
121  std::chrono::duration_cast<std::chrono::milliseconds>(syncInterval),
122  nSyncRetries,
123  std::make_unique<FilePersistenceLayer<K, V>>(file))) {}
LRUPersistentCache< K, V, M >::Ptr cache_
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&...args)
Definition: Memory.h:259
template<typename K , typename V , typename M >
wangle::FilePersistentCache< K, V, M >::FilePersistentCache ( std::shared_ptr< folly::Executor executor,
const std::string file,
std::size_t  cacheCapacity,
std::chrono::seconds  syncInterval,
int  nSyncRetries 
)

Definition at line 126 of file FilePersistentCache-inl.h.

132  : cache_(std::make_shared<LRUPersistentCache<K, V, M>>(
134  cacheCapacity,
135  std::chrono::duration_cast<std::chrono::milliseconds>(syncInterval),
136  nSyncRetries,
137  std::make_unique<FilePersistenceLayer<K, V>>(file))) {}
LRUPersistentCache< K, V, M >::Ptr cache_
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
PUSHMI_INLINE_VAR constexpr __adl::get_executor_fn executor
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&...args)
Definition: Memory.h:259
template<typename K, typename V, typename M = std::mutex>
wangle::FilePersistentCache< K, V, M >::~FilePersistentCache ( )
inlineoverride

Definition at line 58 of file FilePersistentCache.h.

58 {}

Member Function Documentation

template<typename K, typename V, typename M = std::mutex>
void wangle::FilePersistentCache< K, V, M >::clear ( bool  clearPersistence = false)
inlineoverridevirtual

Empty the contents of the cache

Implements wangle::PersistentCache< K, V >.

Definition at line 75 of file FilePersistentCache.h.

References wangle::FilePersistentCache< K, V, M >::cache_, and wangle::LRUPersistentCache< K, V, MutexT >::clear().

75  {
76  cache_->clear(clearPersistence);
77  }
LRUPersistentCache< K, V, M >::Ptr cache_
void clear(bool clearPersistence=false) override
template<typename K, typename V, typename M = std::mutex>
folly::Optional<V> wangle::FilePersistentCache< K, V, M >::get ( const K &  key)
inlineoverridevirtual

PersistentCache operations

Implements wangle::PersistentCache< K, V >.

Definition at line 63 of file FilePersistentCache.h.

References wangle::FilePersistentCache< K, V, M >::cache_, and wangle::LRUPersistentCache< K, V, MutexT >::get().

Referenced by TYPED_TEST().

63  {
64  return cache_->get(key);
65  }
LRUPersistentCache< K, V, M >::Ptr cache_
folly::Optional< V > get(const K &key) override
template<typename K, typename V, typename M = std::mutex>
void wangle::FilePersistentCache< K, V, M >::put ( const K &  key,
const V &  val 
)
inlineoverridevirtual

Set a value corresponding to a key

Parameters
keystring, the key to set
valstring, the value to set

overwrites value if key has a value associated in the cache

Implements wangle::PersistentCache< K, V >.

Definition at line 67 of file FilePersistentCache.h.

References wangle::FilePersistentCache< K, V, M >::cache_, and wangle::LRUPersistentCache< K, V, MutexT >::put().

Referenced by TYPED_TEST().

67  {
68  cache_->put(key, val);
69  }
void put(const K &key, const V &val) override
LRUPersistentCache< K, V, M >::Ptr cache_
double val
Definition: String.cpp:273
template<typename K, typename V, typename M = std::mutex>
bool wangle::FilePersistentCache< K, V, M >::remove ( const K &  key)
inlineoverridevirtual

Clear a cache entry associated with a key

Parameters
keystring, the key to lookup and clear
Returns
boolean true if any elements are removed, else false

Implements wangle::PersistentCache< K, V >.

Definition at line 71 of file FilePersistentCache.h.

References wangle::FilePersistentCache< K, V, M >::cache_, and wangle::LRUPersistentCache< K, V, MutexT >::remove().

71  {
72  return cache_->remove(key);
73  }
LRUPersistentCache< K, V, M >::Ptr cache_
bool remove(const K &key) override
template<typename K, typename V, typename M = std::mutex>
void wangle::FilePersistentCache< K, V, M >::setSyncOnDestroy ( bool  syncOnDestroy)
inline

Definition at line 83 of file FilePersistentCache.h.

References wangle::FilePersistentCache< K, V, M >::cache_, and wangle::LRUPersistentCache< K, V, MutexT >::setSyncOnDestroy().

83  {
84  cache_->setSyncOnDestroy(syncOnDestroy);
85  }
LRUPersistentCache< K, V, M >::Ptr cache_
void setSyncOnDestroy(bool syncOnDestroy)
template<typename K, typename V, typename M = std::mutex>
size_t wangle::FilePersistentCache< K, V, M >::size ( )
inlineoverridevirtual

return the size of the cache

Returns
size_t, the size of the cache

Implements wangle::PersistentCache< K, V >.

Definition at line 79 of file FilePersistentCache.h.

References wangle::FilePersistentCache< K, V, M >::cache_, and wangle::LRUPersistentCache< K, V, MutexT >::size().

Referenced by TYPED_TEST().

79  {
80  return cache_->size();
81  }
LRUPersistentCache< K, V, M >::Ptr cache_

Member Data Documentation


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