olp-cpp-sdk 1.24.0
Loading...
Searching...
No Matches
KeyValueCache.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 <ctime>
23#include <functional>
24#include <limits>
25#include <memory>
26#include <string>
27#include <vector>
28
29#include <olp/core/CoreApi.h>
30#include <olp/core/client/ApiError.h>
31#include <olp/core/client/ApiNoResult.h>
32#include <olp/core/client/ApiResponse.h>
33#include <olp/core/porting/any.h>
35
36namespace olp {
37namespace cache {
38
39using Encoder = std::function<std::string()>;
40using Decoder = std::function<olp::porting::any(const std::string&)>;
41
42template <typename Result>
43using OperationOutcome = client::ApiResponse<Result, client::ApiError>;
44using OperationOutcomeEmpty = OperationOutcome<client::ApiNoResult>;
45
47class CORE_API KeyValueCache {
48 public:
54 static constexpr time_t kDefaultExpiry = std::numeric_limits<time_t>::max();
55
57 using ValueType = std::vector<unsigned char>;
58
60 using ValueTypePtr = std::shared_ptr<ValueType>;
61
63 using KeyListType = std::vector<std::string>;
64
65 virtual ~KeyValueCache() = default;
66
77 virtual bool Put(const std::string& key, const olp::porting::any& value,
78 const Encoder& encoder, time_t expiry = kDefaultExpiry) = 0;
79
89 virtual bool Put(const std::string& key, const ValueTypePtr value,
90 time_t expiry = kDefaultExpiry) = 0;
91
100 virtual olp::porting::any Get(const std::string& key,
101 const Decoder& encoder) = 0;
102
110 virtual ValueTypePtr Get(const std::string& key) = 0;
111
119 virtual bool Remove(const std::string& key) = 0;
120
129 virtual bool RemoveKeysWithPrefix(const std::string& prefix) = 0;
130
138 virtual bool Contains(const std::string& key) const {
140 return false;
141 }
142
153 virtual bool Protect(const KeyListType& keys) {
155 return false;
156 }
157
168 virtual bool Release(const KeyListType& keys) {
170 return false;
171 }
172
181 virtual bool IsProtected(const std::string& key) const {
183 return false;
184 }
185
191 virtual void Promote(const std::string& key) { OLP_SDK_CORE_UNUSED(key); }
192
201 virtual OperationOutcome<ValueTypePtr> Read(const std::string& key) {
203 return client::ApiError(client::ErrorCode::Unknown, "Not implemented");
204 }
205
215 virtual OperationOutcomeEmpty Write(const std::string& key,
216 const ValueTypePtr& value,
217 time_t expiry = kDefaultExpiry) {
219 OLP_SDK_CORE_UNUSED(value);
220 OLP_SDK_CORE_UNUSED(expiry);
221 return client::ApiError(client::ErrorCode::Unknown, "Not implemented");
222 }
223
231 virtual OperationOutcomeEmpty Delete(const std::string& key) {
233 return client::ApiError(client::ErrorCode::Unknown, "Not implemented");
234 }
235
244 virtual OperationOutcomeEmpty DeleteByPrefix(const std::string& prefix) {
245 OLP_SDK_CORE_UNUSED(prefix);
246 return client::ApiError(client::ErrorCode::Unknown, "Not implemented");
247 }
248
258 const std::string& prefix) {
259 OLP_SDK_CORE_UNUSED(prefix);
260 return client::ApiError(client::ErrorCode::Unknown, "Not implemented");
261 }
262};
263
264} // namespace cache
265} // namespace olp
Contains utilities used to work around compiler warnings.
#define OLP_SDK_CORE_UNUSED(...)
Arbitrarily marks many variables as unused to avoid compiler warnings.
Definition WarningWorkarounds.h:28
An interface for a cache that expects a key-value pair.
Definition KeyValueCache.h:47
std::vector< unsigned char > ValueType
The value type that is stored in the DB.
Definition KeyValueCache.h:57
virtual ValueTypePtr Get(const std::string &key)=0
Gets the key and binary data from the cache.
virtual OperationOutcome< ValueTypePtr > Read(const std::string &key)
Gets the binary data from the cache.
Definition KeyValueCache.h:201
std::shared_ptr< ValueType > ValueTypePtr
The shared pointer type of the DB entry.
Definition KeyValueCache.h:60
virtual bool Put(const std::string &key, const ValueTypePtr value, time_t expiry=kDefaultExpiry)=0
Stores the raw binary data as a value in the cache.
virtual bool Remove(const std::string &key)=0
Removes the key-value pair from the cache.
virtual void Promote(const std::string &key)
Promotes a key in the cache LRU when applicable.
Definition KeyValueCache.h:191
virtual bool Release(const KeyListType &keys)
Removes a list of keys from protection.
Definition KeyValueCache.h:168
virtual OperationOutcomeEmpty Delete(const std::string &key)
Removes the key-value pair from the cache.
Definition KeyValueCache.h:231
virtual bool Put(const std::string &key, const olp::porting::any &value, const Encoder &encoder, time_t expiry=kDefaultExpiry)=0
Stores the key-value pair in the cache.
virtual OperationOutcomeEmpty Write(const std::string &key, const ValueTypePtr &value, time_t expiry=kDefaultExpiry)
Stores the raw binary data as a value in the cache.
Definition KeyValueCache.h:215
virtual OperationOutcomeEmpty DeleteByPrefix(const std::string &prefix)
Removes the values with the keys that match the given prefix from the cache.
Definition KeyValueCache.h:244
virtual bool Contains(const std::string &key) const
Checks if the key is in the cache.
Definition KeyValueCache.h:138
virtual OperationOutcome< KeyListType > ListKeysWithPrefix(const std::string &prefix)
Lists the keys that match the given prefix.
Definition KeyValueCache.h:257
virtual bool RemoveKeysWithPrefix(const std::string &prefix)=0
Removes the values with the keys that match the given prefix from the cache.
virtual bool IsProtected(const std::string &key) const
Checks if key is protected.
Definition KeyValueCache.h:181
virtual olp::porting::any Get(const std::string &key, const Decoder &encoder)=0
Gets the key-value pair from the cache.
virtual bool Protect(const KeyListType &keys)
Protects keys from eviction.
Definition KeyValueCache.h:153
std::vector< std::string > KeyListType
An alias for the list of keys to be protected or released.
Definition KeyValueCache.h:63
A wrapper around an internal error or HTTP status code.
Definition ApiError.h:37
Represents a request outcome.
Definition ApiResponse.h:65
Rules all the other namespaces.
Definition AppleSignInProperties.h:24