CachedMemoryMixin.h
Go to the documentation of this file.
1#pragma once
2
3#include <SimoxUtility/json.h>
4
6
8
10{
11 template <class _CoreSegmentT>
13 {
14 public:
15 CachedMemoryMixin(const MemoryID& id) : cache(std::make_unique<armem::wm::Memory>(id))
16 {
17 }
18
19 protected:
21 getCache() const
22 {
23 std::lock_guard l(this->cache_mutex);
24 return *cache;
25 }
26
27 /// configuration
28 void
29 configureMixin(const nlohmann::json& json)
30 {
31 // TODO: Max cache size
32 //
33 // PROBLEM: The cache grows unbounded via addToCache() which continuously appends
34 // without any eviction mechanism. This leads to unbounded memory growth over time,
35 // only released on system restart.
36 //
37 // RECOMMENDED SOLUTION (Hybrid Approach):
38 // Implement multiple eviction strategies for defense in depth:
39 //
40 // struct CacheConfig {
41 // long maxSnapshots = 50000; // Hard limit on snapshot count
42 // long maxMemoryMB = 200; // Hard limit on memory usage
43 // long maxAgeSeconds = 600; // TTL: remove entries older than 10 minutes
44 // bool useLRU = false; // Optional: LRU eviction (more complex)
45 // };
46 //
47 // Eviction logic should:
48 // 1. Remove entries older than maxAgeSeconds (TTL cleanup)
49 // 2. If still over maxSnapshots OR maxMemoryMB, evict oldest entries (FIFO)
50 // 3. Run cleanup periodically (e.g., every 30s) and on every addToCache() call
51 //
52 // This provides multiple safeguards:
53 // - Time-based: prevents stale data accumulation
54 // - Count-based: prevents unbounded snapshot growth
55 // - Memory-based: prevents OOM scenarios
56 //
57 // NOTE: As of now, addToCache() is commented out in Memory.cpp:472, so this
58 // issue may not be active. Verify cache usage before implementing eviction.
59 }
60
61 void
63 {
64 ARMARX_CHECK_NOT_EMPTY(id.memoryName) << " The full id was: " << id.str();
65
66 cache->id() = id.getMemoryID();
67 }
68
69 void
71 {
72 std::lock_guard l(cache_mutex);
73 cache->append(memory);
74 }
75
76 bool
77 cacheHasCoreSegment(const std::string& n) const
78 {
79 std::lock_guard l(cache_mutex);
80 return cache->hasCoreSegment(n);
81 }
82
83 bool
85 {
86 std::lock_guard l(cache_mutex);
87 return cache->hasCoreSegment(n);
88 }
89
90 bool
92 {
93 std::lock_guard l(cache_mutex);
94 return cache->hasProviderSegment(n);
95 }
96
97 bool
98 cacheHasEntity(const MemoryID& n) const
99 {
100 std::lock_guard l(cache_mutex);
101 return cache->hasEntity(n);
102 }
103
104 bool
106 {
107 std::lock_guard l(cache_mutex);
108 return cache->hasSnapshot(n);
109 }
110
111 bool
113 {
114 std::lock_guard l(cache_mutex);
115 return cache->hasInstance(n);
116 }
117
118
119 protected:
120 std::unique_ptr<armem::wm::Memory> cache;
121
122 private:
123 mutable std::recursive_mutex cache_mutex;
124 };
125} // namespace armarx::armem::server::ltm::detail::mixin
#define ARMARX_CHECK_NOT_EMPTY(c)
A memory storing data on the hard drive and in mongodb (needs 'armarx memory start' to start the mong...
Definition Memory.h:24
void configureMixin(const nlohmann::json &json)
configuration
Client-side working memory.
Brief description of class memory.
Definition memory.h:39