PydanticRedisStorage#
- class safir.redis.PydanticRedisStorage(*, datatype, redis, key_prefix='')#
-
JSON-serialized encrypted storage in Redis.
- Parameters:
datatype (
type
[TypeVar
(S
, bound= BaseModel)]) – The class of object being stored (a Pydantic model).redis (
Redis
) – A Redis client configured to talk to the backend store.key_prefix (
str
, default:''
) – A prefix to prepend to all Redis keys. Setting a prefix ensures that all objects stored through this class share the same redis key prefix. This is useful if multiple storage classes share the same Redis database. The keys that you use to store objects (e.g. thestore
method) and the key you get back from thescan
method do not include this prefix.
Methods Summary
delete
(key)Delete a stored object.
delete_all
(pattern)Delete all stored objects.
get
(key)Retrieve a stored object.
scan
(pattern)Scan Redis for a given key pattern, returning each key.
store
(key, obj[, lifetime])Store an object.
Methods Documentation
- async delete(key)#
Delete a stored object.
- async delete_all(pattern)#
Delete all stored objects.
- async get(key)#
Retrieve a stored object.
- Parameters:
key (
str
) – The key for the object.- Returns:
The deserialized object or
None
if no such object could be found.- Return type:
Any or None
- Raises:
DeserializeError – Raised if the stored object could not be decrypted or deserialized.
- async scan(pattern)#
Scan Redis for a given key pattern, returning each key.
- Parameters:
pattern (
str
) – Key pattern to scan for.- Yields:
str – Each key matching that pattern.
- Return type:
- async store(key, obj, lifetime=None)#
Store an object.
- Parameters:
key (
str
) – The key for the object.obj (
TypeVar
(S
, bound= BaseModel)) – The object to store.lifetime (
int
|None
, default:None
) – The object lifetime in seconds. The object should expire from the data store after that many seconds after the current time. PassNone
if the object should not expire.
- Return type: