olp-cpp-sdk  1.21.0
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>
34 #include <boost/any.hpp>
35 
36 namespace olp {
37 namespace cache {
38 
39 using Encoder = std::function<std::string()>;
40 using Decoder = std::function<boost::any(const std::string&)>;
41 
42 template <typename Result>
43 using OperationOutcome = client::ApiResponse<Result, client::ApiError>;
44 using OperationOutcomeEmpty = OperationOutcome<client::ApiNoResult>;
45 
47 class 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 boost::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 boost::any Get(const std::string& key, const Decoder& encoder) = 0;
101 
109  virtual ValueTypePtr Get(const std::string& key) = 0;
110 
118  virtual bool Remove(const std::string& key) = 0;
119 
128  virtual bool RemoveKeysWithPrefix(const std::string& prefix) = 0;
129 
137  virtual bool Contains(const std::string& key) const {
138  OLP_SDK_CORE_UNUSED(key);
139  return false;
140  }
141 
152  virtual bool Protect(const KeyListType& keys) {
153  OLP_SDK_CORE_UNUSED(keys);
154  return false;
155  }
156 
167  virtual bool Release(const KeyListType& keys) {
168  OLP_SDK_CORE_UNUSED(keys);
169  return false;
170  }
171 
180  virtual bool IsProtected(const std::string& key) const {
181  OLP_SDK_CORE_UNUSED(key);
182  return false;
183  }
184 
190  virtual void Promote(const std::string& key) { OLP_SDK_CORE_UNUSED(key); }
191 
200  virtual OperationOutcome<ValueTypePtr> Read(const std::string& key) {
201  OLP_SDK_CORE_UNUSED(key);
202  return client::ApiError(client::ErrorCode::Unknown, "Not implemented");
203  }
204 
214  virtual OperationOutcomeEmpty Write(const std::string& key,
215  const ValueTypePtr& value,
216  time_t expiry = kDefaultExpiry) {
217  OLP_SDK_CORE_UNUSED(key);
218  OLP_SDK_CORE_UNUSED(value);
219  OLP_SDK_CORE_UNUSED(expiry);
220  return client::ApiError(client::ErrorCode::Unknown, "Not implemented");
221  }
222 
230  virtual OperationOutcomeEmpty Delete(const std::string& key) {
231  OLP_SDK_CORE_UNUSED(key);
232  return client::ApiError(client::ErrorCode::Unknown, "Not implemented");
233  }
234 
243  virtual OperationOutcomeEmpty DeleteByPrefix(const std::string& prefix) {
244  OLP_SDK_CORE_UNUSED(prefix);
245  return client::ApiError(client::ErrorCode::Unknown, "Not implemented");
246  }
247 };
248 
249 } // namespace cache
250 } // 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.
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 boost::any &value, const Encoder &encoder, time_t expiry=kDefaultExpiry)=0
Stores the key-value pair in the cache.
virtual boost::any Get(const std::string &key, const Decoder &encoder)=0
Gets the key-value pair from the cache.
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:190
virtual bool Release(const KeyListType &keys)
Removes a list of keys from protection.
Definition: KeyValueCache.h:167
virtual OperationOutcomeEmpty Delete(const std::string &key)
Removes the key-value pair from the cache.
Definition: KeyValueCache.h:230
virtual OperationOutcome< ValueTypePtr > Read(const std::string &key)
Gets the binary data from the cache.
Definition: KeyValueCache.h:200
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:214
virtual OperationOutcomeEmpty DeleteByPrefix(const std::string &prefix)
Removes the values with the keys that match the given prefix from the cache.
Definition: KeyValueCache.h:243
virtual bool Contains(const std::string &key) const
Checks if the key is in the cache.
Definition: KeyValueCache.h:137
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:180
virtual bool Protect(const KeyListType &keys)
Protects keys from eviction.
Definition: KeyValueCache.h:152
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