olp-cpp-sdk
1.21.0
|
A default cache that provides a memory LRU cache and persistence of cached key-value pairs. More...
#include <DefaultCache.h>
Public Types | |
enum | StorageOpenResult { Success , OpenDiskPathFailure , ProtectedCacheCorrupted , NotReady } |
The storage open result type. More... | |
enum class | CacheType { kMutable , kProtected } |
The cache type. More... | |
Public Types inherited from olp::cache::KeyValueCache | |
using | ValueType = std::vector< unsigned char > |
The value type that is stored in the DB. | |
using | ValueTypePtr = std::shared_ptr< ValueType > |
The shared pointer type of the DB entry. | |
using | KeyListType = std::vector< std::string > |
An alias for the list of keys to be protected or released. | |
Public Member Functions | |
DefaultCache (CacheSettings settings={}) | |
Creates the DefaultCache instance. More... | |
StorageOpenResult | Open () |
Opens the cache to start read and write operations. More... | |
StorageOpenResult | Open (CacheType type) |
Creates a new cache of the corresponding type. More... | |
void | Close () |
Closes the cache. | |
bool | Close (CacheType type) |
Closes the cache internally so that it is not keept open and thus blocking others from accessing it. More... | |
bool | Clear () |
Clears the cache content. More... | |
void | Compact () |
Compacts the underlying mutable cache storage. More... | |
bool | Put (const std::string &key, const boost::any &value, const Encoder &encoder, time_t expiry) override |
Stores the key-value pair in the cache. More... | |
bool | Put (const std::string &key, const KeyValueCache::ValueTypePtr value, time_t expiry) override |
Stores the raw binary data as a value in the cache. More... | |
boost::any | Get (const std::string &key, const Decoder &decoder) override |
Gets the key-value pair from the cache. More... | |
KeyValueCache::ValueTypePtr | Get (const std::string &key) override |
Gets the key and binary data from the cache. More... | |
bool | Remove (const std::string &key) override |
Removes the key-value pair from the cache. More... | |
bool | RemoveKeysWithPrefix (const std::string &prefix) override |
Removes the values with the keys that match the given prefix from the cache. More... | |
bool | Contains (const std::string &key) const override |
Check if key is in the cache. More... | |
bool | Protect (const KeyValueCache::KeyListType &keys) override |
Protects keys from eviction. More... | |
bool | Release (const KeyValueCache::KeyListType &keys) override |
Removes a list of keys from protection. More... | |
bool | IsProtected (const std::string &key) const override |
Checks if key is protected. More... | |
void | Promote (const std::string &key) override |
Promotes a key in the cache LRU when applicable. More... | |
OperationOutcome< ValueTypePtr > | Read (const std::string &key) override |
Gets the binary data from the cache. More... | |
OperationOutcomeEmpty | Write (const std::string &key, const ValueTypePtr &value, time_t expiry) override |
Stores the raw binary data as a value in the cache. More... | |
OperationOutcomeEmpty | Delete (const std::string &key) override |
Removes the key-value pair from the cache. More... | |
OperationOutcomeEmpty | DeleteByPrefix (const std::string &prefix) override |
Removes the values with the keys that match the given prefix from the cache. More... | |
uint64_t | Size (CacheType cache_type) const |
Gets size of the corresponding cache. More... | |
uint64_t | Size (uint64_t new_size) |
Sets maximum size for the mutable cache. Evict data that exceeds new maximum size. More... | |
Additional Inherited Members | |
Static Public Attributes inherited from olp::cache::KeyValueCache | |
static constexpr time_t | kDefaultExpiry = std::numeric_limits<time_t>::max() |
The expiry time of the key-value pair. More... | |
A default cache that provides a memory LRU cache and persistence of cached key-value pairs.
olp::cache::CacheSettings::disk_path
. On iOS, the path is relative to the application data folder.The default maximum size of the persistent cache is 32 MB. If the entire available disk space should be used, set olp::cache::CacheSettings::max_disk_storage
to -1. The implementation of the default persistent cache is based on LevelDB. Due to the known LevelDB limitation, the default cache can be accessed only by one process exclusively.
By default, the maximum size of the memory cache is 1MB. To change it, set olp::cache::CacheSettings::max_memory_cache_size
to the desired value.
|
strong |
The storage open result type.
Enumerator | |
---|---|
Success | The operation succeeded. |
OpenDiskPathFailure | The disk cache failure. |
ProtectedCacheCorrupted | The protected disk cache is corrupted. |
NotReady | The DefaultCache is closed. |
|
explicit |
Creates the DefaultCache
instance.
settings | The cache settings. |
bool olp::cache::DefaultCache::Clear | ( | ) |
Clears the cache content.
bool olp::cache::DefaultCache::Close | ( | CacheType | type | ) |
Closes the cache internally so that it is not keept open and thus blocking others from accessing it.
type | The type of cache to close. |
void olp::cache::DefaultCache::Compact | ( | ) |
Compacts the underlying mutable cache storage.
In particular, deleted and overwritten versions are discarded, and the data is rearranged to reduce the cost of operations needed to access the data. In some cases this operation might take a very long time, so use with care. You generally don't have to call this, but it can be useful to optimize preloaded databases or compact before you shut down the system to ensure quick startup time.
|
overridevirtual |
Check if key is in the cache.
key | The key for the value. |
Reimplemented from olp::cache::KeyValueCache.
|
overridevirtual |
Removes the key-value pair from the cache.
key | The key for the value. |
Reimplemented from olp::cache::KeyValueCache.
|
overridevirtual |
Removes the values with the keys that match the given prefix from the cache.
prefix | The prefix that matches the keys. |
Reimplemented from olp::cache::KeyValueCache.
|
overridevirtual |
Gets the key and binary data from the cache.
key | The key that is used to look for the binary data. |
Implements olp::cache::KeyValueCache.
|
overridevirtual |
Gets the key-value pair from the cache.
key | The key that is used to look for the key-value pair. |
decoder | Decodes the value from a string. |
Implements olp::cache::KeyValueCache.
|
overridevirtual |
Checks if key is protected.
key | The key or prefix. |
Reimplemented from olp::cache::KeyValueCache.
StorageOpenResult olp::cache::DefaultCache::Open | ( | ) |
Opens the cache to start read and write operations.
StorageOpenResult
if there are problems opening any of the provided paths on the disk.StorageOpenResult olp::cache::DefaultCache::Open | ( | CacheType | type | ) |
Creates a new cache of the corresponding type.
type | The type of cache to open. |
Success
if the cache is created or already open, NotReady
if DefaultCache
is closed, and OpenDiskPathFailure
if there are problems opening the provided path on the disk.
|
overridevirtual |
Promotes a key in the cache LRU when applicable.
key | The key to promote in the cache LRU. |
Reimplemented from olp::cache::KeyValueCache.
|
overridevirtual |
Protects keys from eviction.
You can use keys or prefixes to protect single keys or entire catalogs, layers, or version.
keys | The list of keys or prefixes. |
Reimplemented from olp::cache::KeyValueCache.
|
overridevirtual |
Stores the key-value pair in the cache.
key | The key for this value. |
value | The value of any type. |
encoder | Encodes the specified value into a string. |
expiry | The expiry time (in seconds) of the key-value pair. |
Implements olp::cache::KeyValueCache.
|
overridevirtual |
Stores the raw binary data as a value in the cache.
key | The key for this value. |
value | The binary data that should be stored. |
expiry | The expiry time (in seconds) of the key-value pair. |
Implements olp::cache::KeyValueCache.
|
overridevirtual |
Gets the binary data from the cache.
key | The key that is used to look for the binary data. |
Reimplemented from olp::cache::KeyValueCache.
|
overridevirtual |
Removes a list of keys from protection.
The provided keys can be full keys or prefixes only.
keys | The list of keys or prefixes. |
Reimplemented from olp::cache::KeyValueCache.
|
overridevirtual |
Removes the key-value pair from the cache.
key | The key for the value. |
Implements olp::cache::KeyValueCache.
|
overridevirtual |
Removes the values with the keys that match the given prefix from the cache.
prefix | The prefix that matches the keys. |
Implements olp::cache::KeyValueCache.
uint64_t olp::cache::DefaultCache::Size | ( | CacheType | cache_type | ) | const |
Gets size of the corresponding cache.
cache_type | The type of a cache. |
uint64_t olp::cache::DefaultCache::Size | ( | uint64_t | new_size | ) |
Sets maximum size for the mutable cache. Evict data that exceeds new maximum size.
new_size | The new maximum size. |
|
overridevirtual |
Stores the raw binary data as a value in the cache.
key | The key for this value. |
value | The binary data that should be stored. |
expiry | The expiry time (in seconds) of the key-value pair. |
Reimplemented from olp::cache::KeyValueCache.