Skip to content

Commit 16786ad

Browse files
committed
add a skeleton 'expires' decorator
Signed-off-by: Grant Ramsay <seapagan@gmail.com>
1 parent b28b391 commit 16786ad

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

fastapi_redis_cache/cache.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,37 @@ async def inner_wrapper(
127127
return outer_wrapper
128128

129129

130+
def expires(tag: str | None = None) -> Callable[..., Any]:
131+
"""Invalidate all cached responses with the same tag.
132+
133+
Args:
134+
tag (str, optional): A tag to associate with the cached response. This
135+
can later be used to invalidate all cached responses with the same
136+
tag, or for further fine-grained cache expiry. Defaults to None.
137+
"""
138+
139+
def outer_wrapper(func: Callable[..., Any]) -> Callable[..., Any]:
140+
@wraps(func)
141+
async def inner_wrapper(
142+
*args: Any, # noqa: ANN401
143+
**kwargs: Any, # noqa: ANN401
144+
) -> Any: # noqa: ANN401
145+
"""Invalidate all cached responses with the same tag."""
146+
redis_cache = FastApiRedisCache()
147+
if redis_cache.not_connected:
148+
return await get_api_response_async(func, *args, **kwargs)
149+
if tag:
150+
# expire all keys with the same tag. This is a test we will
151+
# later only expire keys that have the search argument in the
152+
# key.
153+
pass
154+
return await get_api_response_async(func, *args, **kwargs)
155+
156+
return inner_wrapper
157+
158+
return outer_wrapper
159+
160+
130161
async def get_api_response_async(
131162
func: Callable[..., Any],
132163
*args: Any, # noqa: ANN401

0 commit comments

Comments
 (0)