cache
Per-model cache of statement templates.
Every model class gets its own least-recently-used cache, keyed by cache key
(see cache_keys), holding statements built with named slots (see
slots). Reads and writes share it::
| Text Only | |
|---|---|
1 2 | |
StatementTemplate
Bases: NamedTuple
A statement from the template cache and the values to run it with for one call::
| Text Only | |
|---|---|
1 2 3 | |
Source code in ormar/queryset/templates/cache.py
| Python | |
|---|---|
31 32 33 34 35 36 37 38 39 40 41 42 | |
TemplateCache
Least-recently-used cache of one model's statements, keyed by cache key.
The size is bounded so an application that builds queries from many
different filter combinations cannot grow memory without limit: once
max_size keys are cached, adding one drops the key used longest ago.
Source code in ormar/queryset/templates/cache.py
| Python | |
|---|---|
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 | |
__init__(max_size=TEMPLATE_CACHE_SIZE)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_size
|
int
|
number of cache keys kept |
TEMPLATE_CACHE_SIZE
|
Source code in ormar/queryset/templates/cache.py
| Python | |
|---|---|
54 55 56 57 58 59 60 | |
clear()
Remove every cached statement.
Source code in ormar/queryset/templates/cache.py
| Python | |
|---|---|
98 99 100 | |
get(key)
Return the statement cached for a key, or None, and mark it as the most recently used.
Another thread using the same model may evict the entry between reading it and marking it, so that case is ignored: the statement is still valid for this call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
cache key of the statement |
required |
Returns:
| Type | Description |
|---|---|
Any
|
cached statement or None |
Source code in ormar/queryset/templates/cache.py
| Python | |
|---|---|
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
put(key, statement)
Cache the statement of a key. When the cache is full, the key used longest ago is dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
cache key of the statement |
required |
statement
|
Any
|
statement to cache |
required |
Source code in ormar/queryset/templates/cache.py
| Python | |
|---|---|
84 85 86 87 88 89 90 91 92 93 94 95 96 | |
cached_statement(model_cls, key, builder, **builder_kwargs)
Return the statement cached for a write cache key, building it on a miss.
The statement is marked as a template (TEMPLATE_OPTION), so the native
compiler keeps its plan even when SQLAlchemy cannot cache it.
A write's cache key is small (the kind of statement and the written column
names), so callers pass the key directly. The statement is only built on
a miss, from builder(**builder_kwargs)::
| Text Only | |
|---|---|
1 2 3 4 5 6 7 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_cls
|
Type[Any]
|
model the statement writes |
required |
key
|
Tuple[Any, ...]
|
cache key of the write |
required |
builder
|
Callable[..., Executable]
|
builds the statement from |
required |
builder_kwargs
|
Any
|
keyword arguments of the builder |
{}
|
Returns:
| Type | Description |
|---|---|
Executable
|
statement for the key |
Source code in ormar/queryset/templates/cache.py
| Python | |
|---|---|
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 | |
template_cache_for(model_cls)
Return the statement template cache of a model class, creating it on the
model's first read. setdefault keeps a single cache when two threads
create it at the same time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_cls
|
Type[Any]
|
model class |
required |
Returns:
| Type | Description |
|---|---|
TemplateCache
|
the model's template cache |
Source code in ormar/queryset/templates/cache.py
| Python | |
|---|---|
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |