Skip to content

Commit 63602f0

Browse files
committed
Fix pluginCache
1 parent d745ad6 commit 63602f0

File tree

8 files changed

+95
-33
lines changed

8 files changed

+95
-33
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Implement custom cache replacement algorithms using pure Python functions - **no
100100

101101
### Python Hook Cache Overview
102102

103-
The `PythonHookCachePolicy` allows you to define custom caching behavior through Python callback functions. This is perfect for:
103+
The `PluginCache` allows you to define custom caching behavior through Python callback functions. This is perfect for:
104104
- Prototyping new cache algorithms
105105
- Educational purposes and learning
106106
- Research and experimentation
@@ -124,7 +124,7 @@ import libcachesim as lcs
124124
from collections import OrderedDict
125125

126126
# Create a Python hook-based cache
127-
cache = lcs.PythonHookCachePolicy(cache_size=1024*1024, cache_name="MyLRU")
127+
cache = lcs.PluginCache(cache_size=1024*1024, cache_name="MyLRU")
128128

129129
# Define LRU policy hooks
130130
def init_hook(cache_size):
@@ -160,7 +160,7 @@ import libcachesim as lcs
160160
from collections import deque
161161
from contextlib import suppress
162162

163-
cache = lcs.PythonHookCachePolicy(cache_size=1024, cache_name="CustomFIFO")
163+
cache = lcs.PluginCache(cache_size=1024, cache_name="CustomFIFO")
164164

165165
def init_hook(cache_size):
166166
return deque() # Use deque for FIFO order

examples/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ caches = {
5959
}
6060

6161
# Create Python hook cache
62-
python_cache = lcs.PythonHookCachePolicy(cache_size, "CustomLRU")
62+
python_cache = lcs.PluginCache(cache_size, "CustomLRU")
6363
# Set hook functions...
6464
caches["Custom Python LRU"] = python_cache
6565

@@ -101,7 +101,7 @@ class LRUPolicy:
101101
return next(iter(self.access_order))
102102

103103
def create_lru_cache(cache_size):
104-
cache = lcs.PythonHookCachePolicy(cache_size, "PythonLRU")
104+
cache = lcs.PluginCache(cache_size, "PythonLRU")
105105

106106
def init_hook(cache_size):
107107
return LRUPolicy(cache_size)
@@ -166,7 +166,7 @@ for alpha in alphas:
166166
### Cache Algorithms
167167
- **Classic algorithms**: `LRU()`, `FIFO()`, `ARC()`, `Clock()`
168168
- **Modern algorithms**: `S3FIFO()`, `Sieve()`, `TinyLFU()`
169-
- **Custom policies**: `PythonHookCachePolicy()`
169+
- **Custom policies**: `PluginCache()`
170170

171171
### Trace Processing
172172
- `open_trace()`: Open real trace files

examples/demo_unified_interface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def demo_unified_interface():
7272
}
7373

7474
# Create Python hook-based LRU
75-
python_cache = lcs.PythonHookCachePolicy(cache_size, "CustomLRU")
75+
python_cache = lcs.PluginCache(cache_size, "CustomLRU")
7676
init_hook, hit_hook, miss_hook, eviction_hook, remove_hook = create_demo_lru_hooks()
7777
python_cache.set_hooks(init_hook, hit_hook, miss_hook, eviction_hook, remove_hook)
7878
caches["Custom Python LRU"] = python_cache

examples/plugin_cache.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from collections import OrderedDict
2+
from libcachesim import PluginCache, CommonCacheParams, Request, SyntheticReader, LRU
3+
4+
class StandaloneLRU:
5+
def __init__(self):
6+
self.cache_data = OrderedDict()
7+
8+
def cache_hit(self, obj_id):
9+
if obj_id in self.cache_data:
10+
obj_size = self.cache_data.pop(obj_id)
11+
self.cache_data[obj_id] = obj_size
12+
13+
def cache_miss(self, obj_id, obj_size):
14+
self.cache_data[obj_id] = obj_size
15+
16+
def cache_eviction(self):
17+
evicted_id, _ = self.cache_data.popitem(last=False)
18+
return evicted_id
19+
20+
def cache_remove(self, obj_id):
21+
if obj_id in self.cache_data:
22+
del self.cache_data[obj_id]
23+
24+
25+
def cache_init_hook(common_cache_params: CommonCacheParams):
26+
return StandaloneLRU()
27+
28+
def cache_hit_hook(cache, obj_id):
29+
cache.cache_hit(obj_id)
30+
31+
def cache_miss_hook(cache, request: Request):
32+
cache.cache_miss(request.obj_id, request.obj_size)
33+
34+
def cache_eviction_hook(cache):
35+
return cache.cache_eviction()
36+
37+
def cache_remove_hook(cache, obj_id):
38+
cache.cache_remove(obj_id)
39+
40+
def cache_free_hook(cache):
41+
cache.cache_data.clear()
42+
43+
plugin_lru_cache = PluginCache(
44+
cache_size=1024*1024,
45+
cache_init_hook=cache_init_hook,
46+
cache_hit_hook=cache_hit_hook,
47+
cache_miss_hook=cache_miss_hook,
48+
cache_eviction_hook=cache_eviction_hook,
49+
cache_remove_hook=cache_remove_hook,
50+
cache_free_hook=cache_free_hook,
51+
cache_name="CustomizedLRU")
52+
53+
ref_lru_cache = LRU(cache_size=1024*1024)
54+
55+
reader = SyntheticReader(
56+
num_of_req=100000,
57+
num_objects=100,
58+
obj_size=100,
59+
seed=42,
60+
alpha=0.8,
61+
dist="zipf",
62+
)
63+
64+
for req in reader:
65+
plugin_hit = plugin_lru_cache.get(req)
66+
ref_hit = ref_lru_cache.get(req)
67+
assert plugin_hit == ref_hit, f"Cache hit mismatch: {plugin_hit} != {ref_hit}"
68+
69+
print("All requests processed successfully. Plugin cache matches reference LRU cache.")
70+
71+
72+

examples/python_hook_cache_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Example demonstrating how to create custom cache policies using Python hooks.
44
55
This example shows how to implement LRU and FIFO cache policies using the
6-
PythonHookCachePolicy class, which allows users to define cache behavior using
6+
PluginCache class, which allows users to define cache behavior using
77
pure Python functions instead of C/C++ plugins.
88
"""
99

@@ -72,7 +72,7 @@ def on_remove(self, obj_id):
7272

7373
def create_lru_cache(cache_size):
7474
"""Create an LRU cache using Python hooks."""
75-
cache = lcs.PythonHookCachePolicy(cache_size, "PythonLRU")
75+
cache = lcs.PluginCache(cache_size, "PythonLRU")
7676

7777
def init_hook(cache_size):
7878
return LRUPolicy(cache_size)
@@ -99,7 +99,7 @@ def free_hook(policy):
9999

100100
def create_fifo_cache(cache_size):
101101
"""Create a FIFO cache using Python hooks."""
102-
cache = lcs.PythonHookCachePolicy(cache_size, "PythonFIFO")
102+
cache = lcs.PluginCache(cache_size, "PythonFIFO")
103103

104104
def init_hook(cache_size):
105105
return FIFOPolicy(cache_size)

libcachesim/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
SamplerType,
1111
AnalysisParam,
1212
AnalysisOption,
13+
CommonCacheParams,
1314
__doc__,
1415
__version__,
1516
)
@@ -38,7 +39,7 @@
3839
Belady,
3940
BeladySize,
4041
# Plugin cache
41-
PythonHookCachePolicy,
42+
PluginCache,
4243
)
4344

4445
from .trace_reader import TraceReader
@@ -56,6 +57,7 @@
5657
"SamplerType",
5758
"AnalysisParam",
5859
"AnalysisOption",
60+
"CommonCacheParams",
5961
# Cache base class
6062
"CacheBase",
6163
# Core cache algorithms
@@ -80,7 +82,7 @@
8082
"Belady",
8183
"BeladySize",
8284
# Plugin cache
83-
"PythonHookCachePolicy",
85+
"PluginCache",
8486
# Readers and analyzers
8587
"TraceReader",
8688
"TraceAnalyzer",

libcachesim/__init__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class BeladySize(CacheBase):
173173
): ...
174174

175175
# Plugin cache
176-
class PythonHookCachePolicy(CacheBase):
176+
class PluginCache(CacheBase):
177177
def __init__(
178178
self,
179179
cache_size: int,

libcachesim/cache.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from abc import ABC
2-
from typing import Protocol
2+
from typing import Protocol, Callable, Optional
33
from .libcachesim_python import (
44
CommonCacheParams,
55
Request,
@@ -351,29 +351,23 @@ def __init__(
351351

352352

353353
# Plugin cache for custom Python implementations
354-
def nop_method(*args, **kwargs):
355-
"""No-operation method for default hooks"""
356-
pass
357-
358-
359-
class PythonHookCachePolicy(CacheBase):
354+
class PluginCache(CacheBase):
360355
"""Python plugin cache for custom implementations"""
361356

362357
def __init__(
363358
self,
364359
cache_size: int,
360+
cache_init_hook: Callable,
361+
cache_hit_hook: Callable,
362+
cache_miss_hook: Callable,
363+
cache_eviction_hook: Callable,
364+
cache_remove_hook: Callable,
365+
cache_free_hook: Optional[Callable] = None,
365366
cache_name: str = "PythonHookCache",
366367
default_ttl: int = 86400 * 300,
367368
hashpower: int = 24,
368369
consider_obj_metadata: bool = False,
369-
cache_init_hook=nop_method,
370-
cache_hit_hook=nop_method,
371-
cache_miss_hook=nop_method,
372-
cache_eviction_hook=nop_method,
373-
cache_remove_hook=nop_method,
374-
cache_free_hook=nop_method,
375370
):
376-
self.cache_name = cache_name
377371
self.common_cache_params = _create_common_params(cache_size, default_ttl, hashpower, consider_obj_metadata)
378372

379373
super().__init__(
@@ -388,9 +382,3 @@ def __init__(
388382
cache_free_hook,
389383
)
390384
)
391-
392-
def set_hooks(self, init_hook, hit_hook, miss_hook, eviction_hook, remove_hook, free_hook=nop_method):
393-
"""Set the cache hooks after initialization"""
394-
# Note: This would require C++ side support to change hooks after creation
395-
# For now, hooks should be set during initialization
396-
pass

0 commit comments

Comments
 (0)