Skip to content

Commit 722f765

Browse files
author
juncheng
committed
[bug] pluginLRU needs to reject objects larger than cache size
1 parent 5bf890e commit 722f765

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

example/plugin_v2/plugin_lru.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ class StandaloneLRU {
2626
std::unordered_map<obj_id_t, Node *> cache_map;
2727
Node *head;
2828
Node *tail;
29+
uint64_t max_size;
2930

30-
StandaloneLRU() {
31+
StandaloneLRU(uint64_t cache_size = 1024 * 1024 * 1024) // default 1GB
32+
: max_size(cache_size) {
3133
head = new Node();
3234
tail = new Node();
3335
head->next = tail;
@@ -40,6 +42,7 @@ class StandaloneLRU {
4042
head = head->next;
4143
delete temp;
4244
}
45+
cache_map.clear();
4346
}
4447

4548
void add_to_head(Node *node) {
@@ -71,9 +74,11 @@ class StandaloneLRU {
7174
}
7275

7376
void cache_miss(obj_id_t obj_id, uint64_t obj_size) {
74-
Node *new_node = new Node(obj_id, obj_size);
75-
cache_map[obj_id] = new_node;
76-
add_to_head(new_node);
77+
if (obj_size < max_size) {
78+
Node *new_node = new Node(obj_id, obj_size);
79+
cache_map[obj_id] = new_node;
80+
add_to_head(new_node);
81+
}
7782
}
7883

7984
obj_id_t cache_eviction() {
@@ -102,7 +107,7 @@ extern "C" {
102107
// implement the cache init hook
103108
void *cache_init_hook(const common_cache_params_t ccache_params) {
104109
// initialize the LRU cache
105-
StandaloneLRU *lru_cache = new StandaloneLRU();
110+
StandaloneLRU *lru_cache = new StandaloneLRU(ccache_params.cache_size);
106111
return lru_cache;
107112
}
108113

0 commit comments

Comments
 (0)