Skip to content

Commit

Permalink
add a skeleton 'expires' decorator
Browse files Browse the repository at this point in the history
Signed-off-by: Grant Ramsay <[email protected]>
  • Loading branch information
seapagan committed Mar 31, 2024
1 parent 1a6ffa3 commit 8b81d84
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions fastapi_redis_cache/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,37 @@ async def inner_wrapper(
return outer_wrapper


def expires(tag: str | None = None) -> Callable[..., Any]:
"""Invalidate all cached responses with the same tag.
Args:
tag (str, optional): A tag to associate with the cached response. This
can later be used to invalidate all cached responses with the same
tag, or for further fine-grained cache expiry. Defaults to None.
"""

def outer_wrapper(func: Callable[..., Any]) -> Callable[..., Any]:
@wraps(func)
async def inner_wrapper(
*args: Any, # noqa: ANN401
**kwargs: Any, # noqa: ANN401
) -> Any: # noqa: ANN401
"""Invalidate all cached responses with the same tag."""
redis_cache = FastApiRedisCache()
if redis_cache.not_connected:
return await get_api_response_async(func, *args, **kwargs)
if tag:
# expire all keys with the same tag. This is a test we will
# later only expire keys that have the search argument in the
# key.
pass
return await get_api_response_async(func, *args, **kwargs)

return inner_wrapper

return outer_wrapper


async def get_api_response_async(
func: Callable[..., Any],
*args: Any, # noqa: ANN401
Expand Down

0 comments on commit 8b81d84

Please sign in to comment.