From 22fa32759a96f9d169b78cc403e36b06107dd04e Mon Sep 17 00:00:00 2001 From: haochengxia Date: Mon, 28 Jul 2025 04:29:41 +0000 Subject: [PATCH] Fix plugin example --- docs/src/en/plugin.md | 13 ++++++++++++- examples/plugin_cache.py | 10 +++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/docs/src/en/plugin.md b/docs/src/en/plugin.md index 1d31161..d27b805 100644 --- a/docs/src/en/plugin.md +++ b/docs/src/en/plugin.md @@ -13,4 +13,15 @@ With user-defined sive python hook functions, py::function cache_free_hook; ``` -We can simulate and determine the cache eviction behavior from the python side. \ No newline at end of file +We can simulate and determine the cache eviction behavior from the python side. + +Here is the signature requirement for these hook functions. + +```python +def cache_init_hook(ccparams: CommonCacheParams) -> CustomizedCacheData: ... +def cache_hit_hook(data: CustomizedCacheData, req: Request) -> None: ... +def cache_miss_hook(data: CustomizedCacheData, req: Request) -> None: ... +def cache_eviction_hook(data: CustomizedCacheData, req: Request) -> int | str: ... +def cache_remove_hook(data: CustomizedCacheData, obj_id: int | str) ->: ... +def cache_free_hook(data: CustomizedCacheData) ->: ... +``` diff --git a/examples/plugin_cache.py b/examples/plugin_cache.py index fd144f6..30a1e78 100644 --- a/examples/plugin_cache.py +++ b/examples/plugin_cache.py @@ -25,13 +25,13 @@ def cache_remove(self, obj_id): def cache_init_hook(common_cache_params: CommonCacheParams): return StandaloneLRU() -def cache_hit_hook(cache, obj_id): - cache.cache_hit(obj_id) +def cache_hit_hook(cache, request: Request): + cache.cache_hit(request.obj_id) def cache_miss_hook(cache, request: Request): cache.cache_miss(request.obj_id, request.obj_size) -def cache_eviction_hook(cache): +def cache_eviction_hook(cache, request: Request): return cache.cache_eviction() def cache_remove_hook(cache, obj_id): @@ -41,7 +41,7 @@ def cache_free_hook(cache): cache.cache_data.clear() plugin_lru_cache = PluginCache( - cache_size=1024*1024, + cache_size=1024, cache_init_hook=cache_init_hook, cache_hit_hook=cache_hit_hook, cache_miss_hook=cache_miss_hook, @@ -50,7 +50,7 @@ def cache_free_hook(cache): cache_free_hook=cache_free_hook, cache_name="CustomizedLRU") -ref_lru_cache = LRU(cache_size=1024*1024) +ref_lru_cache = LRU(cache_size=1024) reader = SyntheticReader( num_of_req=100000,