Skip to content

Fix: signature error in plugin example #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion docs/src/en/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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) ->: ...
Comment on lines +25 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There are syntax errors in the return type annotations for cache_remove_hook and cache_free_hook. The trailing colon ->: is invalid. Based on the example implementation, these functions do not return a value, so the return type should be None.

Suggested change
def cache_remove_hook(data: CustomizedCacheData, obj_id: int | str) ->: ...
def cache_free_hook(data: CustomizedCacheData) ->: ...
def cache_remove_hook(data: CustomizedCacheData, obj_id: int | str) -> None: ...
def cache_free_hook(data: CustomizedCacheData) -> None: ...

def cache_free_hook(data: CustomizedCacheData) ->: ...
```
10 changes: 5 additions & 5 deletions examples/plugin_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The request parameter is unused in this function. To improve code clarity and signal that this is intentional, it's a good practice in Python to prefix unused parameters with an underscore.

Suggested change
def cache_eviction_hook(cache, request: Request):
def cache_eviction_hook(cache, _request: Request):

return cache.cache_eviction()

def cache_remove_hook(cache, obj_id):
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Loading