olp-cpp-sdk 1.24.0
Loading...
Searching...
No Matches
DefaultCache.h
1/*
2 * Copyright (C) 2019-2024 HERE Europe B.V.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * SPDX-License-Identifier: Apache-2.0
17 * License-Filename: LICENSE
18 */
19
20#pragma once
21
22#include <memory>
23#include <mutex>
24#include <string>
25
26#include <olp/core/Config.h>
27#include "CacheSettings.h"
28#include "KeyValueCache.h"
29
30#ifdef OLP_SDK_ENABLE_DEFAULT_CACHE
31
32namespace olp {
33namespace cache {
34
35class DefaultCacheImpl;
36
57class CORE_API DefaultCache : public KeyValueCache {
58 public:
68
72 enum class CacheType {
73 kMutable,
74 kProtected
75 };
76
82 explicit DefaultCache(CacheSettings settings = {});
83 ~DefaultCache() override;
84
94
107
111 void Close();
112
121 bool Close(CacheType type);
122
128 bool Clear();
129
149 void Compact();
150
161 bool Put(const std::string& key, const olp::porting::any& value,
162 const Encoder& encoder, time_t expiry) override;
163
173 bool Put(const std::string& key, const KeyValueCache::ValueTypePtr value,
174 time_t expiry) override;
175
184 olp::porting::any Get(const std::string& key,
185 const Decoder& decoder) override;
186
194 KeyValueCache::ValueTypePtr Get(const std::string& key) override;
195
203 bool Remove(const std::string& key) override;
204
213 bool RemoveKeysWithPrefix(const std::string& prefix) override;
214
222 bool Contains(const std::string& key) const override;
223
234 bool Protect(const KeyValueCache::KeyListType& keys) override;
235
246 bool Release(const KeyValueCache::KeyListType& keys) override;
247
256 bool IsProtected(const std::string& key) const override;
257
263 void Promote(const std::string& key) override;
264
273 OperationOutcome<ValueTypePtr> Read(const std::string& key) override;
274
284 OperationOutcomeEmpty Write(const std::string& key, const ValueTypePtr& value,
285 time_t expiry) override;
286
294 OperationOutcomeEmpty Delete(const std::string& key) override;
295
304 OperationOutcomeEmpty DeleteByPrefix(const std::string& prefix) override;
305
313 uint64_t Size(CacheType cache_type) const;
314
325 uint64_t Size(uint64_t new_size);
326
327 private:
328 std::shared_ptr<DefaultCacheImpl> impl_;
329};
330
331} // namespace cache
332} // namespace olp
333
334#endif // OLP_SDK_ENABLE_DEFAULT_CACHE
A default cache that provides a memory LRU cache and persistence of cached key-value pairs.
Definition DefaultCache.h:57
StorageOpenResult Open(CacheType type)
Creates a new cache of the corresponding type.
CacheType
The cache type.
Definition DefaultCache.h:72
bool Protect(const KeyValueCache::KeyListType &keys) override
Protects keys from eviction.
uint64_t Size(CacheType cache_type) const
Gets size of the corresponding cache.
void Close()
Closes the cache.
bool RemoveKeysWithPrefix(const std::string &prefix) override
Removes the values with the keys that match the given prefix from the cache.
uint64_t Size(uint64_t new_size)
Sets maximum size for the mutable cache. Evict data that exceeds new maximum size.
bool Release(const KeyValueCache::KeyListType &keys) override
Removes a list of keys from protection.
bool Close(CacheType type)
Closes the cache internally so that it is not keept open and thus blocking others from accessing it.
OperationOutcome< ValueTypePtr > Read(const std::string &key) override
Gets the binary data from the cache.
StorageOpenResult
The storage open result type.
Definition DefaultCache.h:62
@ ProtectedCacheCorrupted
Definition DefaultCache.h:65
@ OpenDiskPathFailure
Definition DefaultCache.h:64
@ Success
Definition DefaultCache.h:63
void Promote(const std::string &key) override
Promotes a key in the cache LRU when applicable.
bool Put(const std::string &key, const olp::porting::any &value, const Encoder &encoder, time_t expiry) override
Stores the key-value pair in the cache.
DefaultCache(CacheSettings settings={})
Creates the DefaultCache instance.
bool Remove(const std::string &key) override
Removes the key-value pair from the cache.
OperationOutcomeEmpty DeleteByPrefix(const std::string &prefix) override
Removes the values with the keys that match the given prefix from the cache.
bool IsProtected(const std::string &key) const override
Checks if key is protected.
OperationOutcomeEmpty Delete(const std::string &key) override
Removes the key-value pair from the cache.
OperationOutcomeEmpty Write(const std::string &key, const ValueTypePtr &value, time_t expiry) override
Stores the raw binary data as a value in the cache.
bool Clear()
Clears the cache content.
olp::porting::any Get(const std::string &key, const Decoder &decoder) override
Gets the key-value pair from the cache.
bool Contains(const std::string &key) const override
Check if key is in the cache.
void Compact()
Compacts the underlying mutable cache storage.
KeyValueCache::ValueTypePtr Get(const std::string &key) override
Gets the key and binary data from the cache.
StorageOpenResult Open()
Opens the cache to start read and write operations.
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.
An interface for a cache that expects a key-value pair.
Definition KeyValueCache.h:47
std::shared_ptr< ValueType > ValueTypePtr
The shared pointer type of the DB entry.
Definition KeyValueCache.h:60
std::vector< std::string > KeyListType
An alias for the list of keys to be protected or released.
Definition KeyValueCache.h:63
Represents a request outcome.
Definition ApiResponse.h:65
Rules all the other namespaces.
Definition AppleSignInProperties.h:24
Settings for memory and disk caching.
Definition CacheSettings.h:63