Skip to content

Commit fd9d435

Browse files
committed
Readme update
1 parent ac175c8 commit fd9d435

1 file changed

Lines changed: 39 additions & 7 deletions

File tree

README.md

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,49 @@ async def get_user_profile(user_id: str) -> dict:
170170

171171
### Working with Complex Arguments
172172

173-
Real functions have complex arguments. Use `id_arg` tuples and `arg_adapters` to handle them:
173+
Options for mapping function arguments to cache keys.
174+
175+
#### `id_arg`
176+
177+
Specifies which argument contains the entity ID for the cache key.
178+
179+
**String form** — use when the argument itself is the ID:
180+
```python
181+
id_arg="user_id" # user_id argument is the ID
182+
```
183+
184+
**Tuple form** — use when the ID needs to be extracted from an object:
185+
```python
186+
id_arg=("user", lambda u: u.id) # Extract ID from User object
187+
```
188+
189+
#### `arg_adapters`
190+
191+
Converts complex arguments to strings for the cache key. Only needed for non-primitive types.
192+
193+
```python
194+
arg_adapters={
195+
"filters": lambda f: f.to_cache_key(), # Complex object
196+
"page": str, # Simple conversion
197+
}
198+
```
199+
200+
#### `ignore_args`
201+
202+
Excludes arguments that don't affect the cached result.
203+
204+
```python
205+
ignore_args=["db_session", "logger"]
206+
```
207+
208+
#### Example
174209

175210
```python
176211
@gcache.cached(
177212
key_type="user_id",
178-
id_arg=("user", lambda u: u.id), # Extract ID from User object
179-
arg_adapters={
180-
"filters": lambda f: f.to_cache_key(), # Convert complex objects
181-
"page": str, # Simple conversion
182-
},
183-
ignore_args=["db_session", "logger"], # Don't include these in cache key
213+
id_arg=("user", lambda u: u.id),
214+
arg_adapters={"filters": lambda f: f.to_cache_key()},
215+
ignore_args=["db_session", "logger"],
184216
)
185217
async def search_user_posts(
186218
user: User,

0 commit comments

Comments
 (0)