-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Christian Parpart <[email protected]>
- Loading branch information
1 parent
f0f7c1c
commit 0d013d2
Showing
5 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
#pragma once | ||
|
||
#include "../Api.hpp" | ||
#include "Record.hpp" | ||
|
||
#include <reflection-cpp/reflection.hpp> | ||
|
||
#include <unordered_map> | ||
|
||
/// Manages the cache of the records of the given type. | ||
template <typename Record> | ||
class RecordCache | ||
{ | ||
public: | ||
using PrimaryKey = RecordPrimaryKeyType<Record>; | ||
|
||
explicit RecordCache(size_t capacity): | ||
_cache() | ||
{ | ||
std::ignore = capacity; // TODO: used when moving to LRU cache | ||
} | ||
|
||
LIGHTWEIGHT_API static RecordCache& Instance() | ||
{ | ||
static RecordCache instance; | ||
return instance; | ||
} | ||
|
||
/// Clears the cache. | ||
void Clear(); | ||
|
||
/// Clears the record with the given primary key from the cache. | ||
void Clear(PrimaryKey const& key); | ||
|
||
/// Emplaces the record in the cache and returns a reference to the cached record. | ||
Record& Emplace(Record&& record); | ||
|
||
/// Looks up the record in the cache. | ||
Record* Lookup(PrimaryKey const& key); | ||
|
||
private: | ||
// TODO: Use an LRU-based internal cache store here later | ||
std::unordered_map<PrimaryKey, Record> _cache {}; | ||
}; | ||
|
||
template <typename Record> | ||
inline void RecordCache<Record>::Clear() | ||
{ | ||
_cache.clear(); | ||
} | ||
|
||
template <typename Record> | ||
inline void RecordCache<Record>::Clear(PrimaryKey const& key) | ||
{ | ||
_cache.erase(key); | ||
} | ||
|
||
template <typename Record> | ||
inline Record& RecordCache<Record>::Emplace(Record&& record) | ||
{ | ||
auto const& primaryKey = RecordPrimaryKeyOf(record); | ||
auto [it, inserted] = _cache.try_emplace(primaryKey, std::move(record)); | ||
return it->second; | ||
} | ||
|
||
template <typename Record> | ||
inline Record* RecordCache<Record>::Lookup(PrimaryKey const& key) | ||
{ | ||
if (auto it = _cache.find(key); it != _cache.end()) | ||
return &it->second; | ||
return nullptr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters