Key Fields¶
The cache-key field types used inside a KeyConstructor. See the
Key Constructors guide
for an overview and worked examples.
CacheKeyField ¶
Bases: ABC
Base class for the pieces that go into a cache key.
Each subclass pulls a piece of data out of a function call and turns it into a stable string.
get_key_payload
abstractmethod
¶
Return a name-to-value payload extracted from the call.
stringify ¶
Turn value into the string the cache key uses, hashing the result when hash_value is set.
get_cache_key_part ¶
Return the string this field contributes to the cache key for the given call.
extract_request_object ¶
extract_request_object(
func,
args,
kwargs,
request_arg="request",
normalize=False,
view_self_request_fallback: bool = True,
)
Find the request object on the call, falling back to the DRF viewset convention when enabled.
ConstantKeyField ¶
ConstantKeyField(
key: str,
value: Any,
*args,
partition=False,
hash_value=False,
sort_lists: bool = False,
**kwargs,
)
Bases: CacheKeyField
Cache-key field that contributes a fixed key-value pair on every call.
Example
::
class UserKey(KeyConstructor):
env = ConstantKeyField("env", "production")
user = ArgsKeyField("user_id", partition=True)
ArgsKeyField ¶
ArgsKeyField(
arguments: str | list[str] = "*",
path: str | None = None,
normalizer: Callable[[Any], Any] | None = None,
*args,
partition=False,
hash_value=False,
sort_lists: bool = False,
**kwargs,
)
Bases: CacheKeyField
Cache-key field that captures function arguments by name.
Example
::
class UserKey(KeyConstructor):
user = ArgsKeyField("user_id", partition=True)
lang = ArgsKeyField("lang")
RequestValueKeyField ¶
RequestValueKeyField(
path: str,
request_arg: str = "request",
*args,
partition=False,
hash_value=False,
sort_lists: bool = False,
view_self_request_fallback: bool = True,
**kwargs,
)
Bases: CacheKeyField
Cache-key field that reads a value off the request object.
Example
::
class UserKey(KeyConstructor):
user = RequestValueKeyField("user.id", partition=True)
QueryParamsKeyField ¶
QueryParamsKeyField(
params: str | list[str] = "*",
request_arg: str = "request",
*args,
partition=False,
hash_value=False,
sort_lists: bool = False,
view_self_request_fallback: bool = True,
**kwargs,
)
Bases: CacheKeyField
Cache-key field that captures values from the request's query string.
Example
::
class ListUsersKey(KeyConstructor):
filters = QueryParamsKeyField(["status", "role"])
DjangoModelKeyField ¶
Bases: CacheKeyField
Cache-key field that fingerprints a Django model's schema.
Records the model's name, module, and concrete fields. Migrations that change the schema invalidate the cache automatically. The payload is always hashed.
Example
::
class UserKey(KeyConstructor):
shape = DjangoModelKeyField(User)
DrfSerializerKeyField ¶
Bases: CacheKeyField
Cache-key field that fingerprints a DRF serializer's shape.
Walks declared fields (including nested and list serializers) and records their classes and modules. Adding or removing a field invalidates the cache automatically. The payload is always hashed.
Example
::
class ListUsersKey(KeyConstructor):
shape = DrfSerializerKeyField(UserSerializer)