olp-cpp-sdk  1.21.0
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 
32 namespace olp {
33 namespace cache {
34 
35 class DefaultCacheImpl;
36 
57 class CORE_API DefaultCache : public KeyValueCache {
58  public:
66  NotReady
67  };
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 boost::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  boost::any Get(const std::string& key, const Decoder& decoder) override;
185 
193  KeyValueCache::ValueTypePtr Get(const std::string& key) override;
194 
202  bool Remove(const std::string& key) override;
203 
212  bool RemoveKeysWithPrefix(const std::string& prefix) override;
213 
221  bool Contains(const std::string& key) const override;
222 
233  bool Protect(const KeyValueCache::KeyListType& keys) override;
234 
245  bool Release(const KeyValueCache::KeyListType& keys) override;
246 
255  bool IsProtected(const std::string& key) const override;
256 
262  void Promote(const std::string& key) override;
263 
272  OperationOutcome<ValueTypePtr> Read(const std::string& key) override;
273 
283  OperationOutcomeEmpty Write(const std::string& key, const ValueTypePtr& value,
284  time_t expiry) override;
285 
293  OperationOutcomeEmpty Delete(const std::string& key) override;
294 
303  OperationOutcomeEmpty DeleteByPrefix(const std::string& prefix) override;
304 
312  uint64_t Size(CacheType cache_type) const;
313 
324  uint64_t Size(uint64_t new_size);
325 
326  private:
327  std::shared_ptr<DefaultCacheImpl> impl_;
328 };
329 
330 } // namespace cache
331 } // namespace olp
332 
333 #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 boost::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.
boost::any Get(const std::string &key, const Decoder &decoder) override
Gets 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.
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:65