Skip to content

Commit

Permalink
cache key does not include prefix (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-luna authored Apr 23, 2021
1 parent 2895439 commit e1b94b4
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/fastapi_redis_cache/key_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,33 @@
ALWAYS_IGNORE_ARG_TYPES = [Response, Request]


def get_cache_key(func: Callable, ignore_arg_types: List[ArgType], *args: List, **kwargs: Dict) -> str:
"""Ganerate a string that uniquely identifies the function and values of all arguments."""
def get_cache_key(func: Callable, prefix: str, ignore_arg_types: List[ArgType], *args: List, **kwargs: Dict) -> str:
"""Ganerate a string that uniquely identifies the function and values of all arguments.
Args:
func (`Callable`): Path operation function for an API endpoint.
prefix (`str`): Customizable namespace value that will prefix all cache keys.
ignore_arg_types (`List[ArgType]`): Each argument to the API endpoint function is
used to compose the cache key by calling `str(arg)`. If there are any keys that
should not be used in this way (i.e., because their value has no effect on the
response, such as a `Request` or `Response` object) you can remove them from
the cache key by including their type as a list item in ignore_key_types.
Returns:
`str`: Unique identifier for `func`, `*args` and `**kwargs` that can be used as a
Redis key to retrieve cached API response data.
"""

if not ignore_arg_types:
ignore_arg_types = []
ignore_arg_types.extend(ALWAYS_IGNORE_ARG_TYPES)
prefix = f"{prefix}:" if prefix else ""

sig = signature(func)
sig_params = sig.parameters
func_args = get_func_args(sig, *args, **kwargs)
args_str = get_args_str(sig_params, func_args, ignore_arg_types)
return f"{func.__module__}.{func.__name__}({args_str})"
return f"{prefix}{func.__module__}.{func.__name__}({args_str})"


def get_func_args(sig: Signature, *args: List, **kwargs: Dict) -> "OrderedDict[str, Any]":
Expand Down

0 comments on commit e1b94b4

Please sign in to comment.