Caches¶
Synchronous cache¶
Synchronous cache.
Source code in cachetory/caches/sync.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
__init__ ¶
__init__(
*,
serializer: Serializer[ValueT, WireT],
backend: SyncBackend[WireT],
prefix: str = ""
) -> None
Instantiate a cache.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefix
|
str
|
backend key prefix |
''
|
Source code in cachetory/caches/sync.py
__getitem__ ¶
Retrieve a single value from the cache.
Raises:
Type | Description |
---|---|
KeyError
|
the key is not found in the cache |
Examples:
>>> cache["key"] = 42
>>>
>>> assert cache["key"] == 42
>>>
>>> with pytest.raises(KeyError):
>>> _ = cache["missing"]
Source code in cachetory/caches/sync.py
get ¶
Retrieve a single value from the cache.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
cache key |
required |
default
|
DefaultT
|
default value – if the key is not found |
None
|
Returns:
Type | Description |
---|---|
Union[ValueT, DefaultT]
|
Retrieved value if present, or |
Examples:
Source code in cachetory/caches/sync.py
get_many ¶
Retrieve many values from the cache.
Returns:
Type | Description |
---|---|
dict[str, ValueT]
|
Dictionary of existing values indexed by their respective keys. Missing keys are omitted. |
Examples:
Source code in cachetory/caches/sync.py
expire_in ¶
Set the expiration time for the key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
cache key |
required |
time_to_live
|
Optional[timedelta]
|
time to live, or |
None
|
Source code in cachetory/caches/sync.py
__setitem__ ¶
Set the cache item. To customize the behavior, use set()
.
set ¶
set(
key: str,
value: ValueT,
time_to_live: Optional[timedelta] = None,
if_not_exists: bool = False,
) -> None
Set the cache item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
cache key |
required |
value
|
ValueT
|
cached value |
required |
time_to_live
|
Optional[timedelta]
|
time to live, or |
None
|
if_not_exists
|
bool
|
only set the item if it does not already exist |
False
|
Source code in cachetory/caches/sync.py
set_many ¶
Set many cache items at once.
Examples:
Source code in cachetory/caches/sync.py
delete ¶
Delete the cache item.
Returns:
Type | Description |
---|---|
bool
|
|
clear ¶
__delitem__ ¶
Delete the cache item.
Raises:
Type | Description |
---|---|
KeyError
|
the key didn't exist |
Asynchronous cache¶
Asynchronous cache.
Source code in cachetory/caches/async_.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
__init__ ¶
__init__(
*,
serializer: Serializer[ValueT, WireT],
backend: AsyncBackend[WireT],
serialize_executor: Executor | NotSet | None = _NOT_SET,
prefix: str = ""
) -> None
Instantiate a cache.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
serialize_executor
|
Executor | NotSet | None
|
If specified, underlying serializing and deserializing will be performed
using the executor (for example, |
_NOT_SET
|
prefix
|
str
|
backend key prefix |
''
|
Source code in cachetory/caches/async_.py
get
async
¶
Retrieve a single value from the cache.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
cache key |
required |
default
|
DefaultT
|
default value – if the key is not found |
None
|
Returns:
Type | Description |
---|---|
ValueT | DefaultT
|
Retrieved value if present, or |
Examples:
>>> await cache.set("key", 42)
>>> assert await cache.get("key") == 42
>>> assert await cache.get("missing") is None
Source code in cachetory/caches/async_.py
get_many
async
¶
Retrieve many values from the cache.
Returns:
Type | Description |
---|---|
dict[str, ValueT]
|
Dictionary of existing values indexed by their respective keys. Missing keys are omitted. |
Examples:
>>> await memory_cache.set("foo", 42)
>>> assert await memory_cache.get_many("foo", "bar") == {"foo": 42}
Source code in cachetory/caches/async_.py
expire_in
async
¶
Set the expiration time for the key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
cache key |
required |
time_to_live
|
timedelta | None
|
time to live, or |
None
|
Source code in cachetory/caches/async_.py
set
async
¶
set(
key: str,
value: ValueT,
time_to_live: timedelta | None = None,
if_not_exists: bool = False,
) -> None
Set the cache item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
cache key |
required |
value
|
ValueT
|
cached value |
required |
time_to_live
|
timedelta | None
|
time to live, or |
None
|
if_not_exists
|
bool
|
only set the item if it does not already exist |
False
|
Source code in cachetory/caches/async_.py
set_many
async
¶
Set many cache items at once.
Examples:
Source code in cachetory/caches/async_.py
delete
async
¶
Delete the cache item.
Returns:
Type | Description |
---|---|
bool
|
|