| 1 |
Store caching |
| 2 |
============= |
| 3 |
|
| 4 |
The ``Store`` can optionally use a second backend as a local cache for selected |
| 5 |
namespaces, which is especially useful when the primary backend is remote, |
| 6 |
slower or otherwise more "expensive" than the cache. |
| 7 |
|
| 8 |
Configuration |
| 9 |
------------- |
| 10 |
|
| 11 |
- ``cache_url`` or ``cache_backend``: where cached data is stored |
| 12 |
- ``config``: mapping of namespace to its configuration dict, containing |
| 13 |
nesting levels and cache policy settings. |
| 14 |
|
| 15 |
Each namespace configuration dictionary can have: |
| 16 |
|
| 17 |
- ``levels``: a required list of integers specifying nesting levels. |
| 18 |
- ``cache``: optional cache mode, accepting ``CacheMode`` values or string |
| 19 |
aliases: |
| 20 |
|
| 21 |
- ``CacheMode.C_OFF`` or ``"off"``: bypass cache completely (default). |
| 22 |
- ``CacheMode.C_MIRROR`` or ``"mirror"``: always read from primary backend, |
| 23 |
but update the cache after successful primary backend reads and writes. |
| 24 |
- ``CacheMode.C_WRITETHROUGH`` or ``"writethrough"``: read-through + |
| 25 |
write-through. For now, only content-hash addressed namespaces should |
| 26 |
use this mode. |
| 27 |
|
| 28 |
- ``max_age``: optional maximum age expressed in seconds since last access. |
| 29 |
The default is ``None`` (no age limit). |
| 30 |
- ``size``: optional maximum size in bytes. It sets a per-namespace cache |
| 31 |
size budget enforced by evicting least-recently-used items until the |
| 32 |
namespace total size is within the configured budget. |
| 33 |
|
| 34 |
Example:: |
| 35 |
|
| 36 |
from borgstore.store import Store, CacheMode |
| 37 |
|
| 38 |
store = Store( |
| 39 |
url="sftp://user@host/repo", |
| 40 |
config={ |
| 41 |
"data": { |
| 42 |
"levels": [2], |
| 43 |
"cache": "writethrough", |
| 44 |
"max_age": 3600, |
| 45 |
"size": 4 * 1024**3, |
| 46 |
}, |
| 47 |
"meta": { |
| 48 |
"levels": [1], |
| 49 |
"cache": CacheMode.C_MIRROR, |
| 50 |
}, |
| 51 |
}, |
| 52 |
cache_url="file:///home/user/.cache/borgstore/repo", |
| 53 |
) |
| 54 |
|
| 55 |
Behavior |
| 56 |
-------- |
| 57 |
|
| 58 |
- Cache keys are identical to primary backend keys (same nesting). |
| 59 |
- Soft-deleted items are cached under the same ``.del`` name as primary. |
| 60 |
- Soft delete/undelete renames cache entries as well. |
| 61 |
- On ``Store.open()`` and ``Store.close()``, cache-enabled namespaces are scanned |
| 62 |
to clean up the cache. Cleanup order per namespace is: |
| 63 |
|
| 64 |
1. remove expired cache objects when ``max_age`` is configured, |
| 65 |
2. if ``size`` is configured, evict the least-recently-used remaining items |
| 66 |
until the namespace total size is ``<= size``. |
| 67 |
|
| 68 |
Expired entries are always removed first, even if total size is already below |
| 69 |
the ``size`` limit. |
| 70 |
- Cache failures are non-fatal and logged as warnings. |
| 71 |
|
| 72 |
Manual Cache Invalidation |
| 73 |
------------------------- |
| 74 |
|
| 75 |
If you need to programmatically clear or invalidate parts of the cache (for |
| 76 |
example, to resolve stale objects after primary backend deletes by other |
| 77 |
clients, or if cache corruption is suspected), you can use the |
| 78 |
``cache_invalidate`` method: |
| 79 |
|
| 80 |
- To invalidate a single item:: |
| 81 |
|
| 82 |
store.cache_invalidate("data/00000000") |
| 83 |
|
| 84 |
- To invalidate all cached items in a specific namespace (e.g. ``"data/"``):: |
| 85 |
|
| 86 |
store.cache_invalidate("data/") |
| 87 |
|
| 88 |
- To invalidate all cached items across all configured namespaces, pass |
| 89 |
``ROOTNS``:: |
| 90 |
|
| 91 |
from borgstore.constants import ROOTNS |
| 92 |
store.cache_invalidate(ROOTNS) |
| 93 |
|
| 94 |
Limitations |
| 95 |
----------- |
| 96 |
|
| 97 |
- Eviction by ``max_age`` or ``size`` is open-time and close-time only |
| 98 |
(``Store.open()`` / ``Store.close()``), not continuous during |
| 99 |
``store()``/``load()`` operations. |
| 100 |
- No proactive cache validation/revalidation. |
| 101 |
- If an object is deleted in the primary backend by another client, the local |
| 102 |
cache will still have a stale object. |
| 103 |
- ``max_age`` and LRU-by-``size`` depend on backend ``ItemInfo.atime`` support, |
| 104 |
currently that is supported by ``posixfs`` and ``REST`` backends. |
| 105 |
If ``atime`` is 0 (not implemented): |
| 106 |
|
| 107 |
- using ``max_age`` would empty the cache on ``Store.open()`` or ``Store.close()`` |
| 108 |
- using ``size`` would not work in LRU order, because order can't be |
| 109 |
determined |
| 110 |
- If a partial range ``load`` call for an object in a cached namespace causes |
| 111 |
a cache miss, the full object will be read from the primary backend and the |
| 112 |
cache will be populated with the full object. |
| 113 |
|
| 114 |
Statistics |
| 115 |
---------- |
| 116 |
|
| 117 |
``Store.stats`` includes cache counters: |
| 118 |
|
| 119 |
- ``backend_load_volume`` |
| 120 |
- ``backend_store_volume`` |
| 121 |
- ``backend_load_calls`` |
| 122 |
- ``backend_store_calls`` |
| 123 |
- ``backend_delete_calls`` |
| 124 |
- ``cache_disabled`` |
| 125 |
- ``cache_hits`` |
| 126 |
- ``cache_misses`` |
| 127 |
- ``cache_hit_ratio`` |
| 128 |
- ``cache_errors`` |
| 129 |
- ``cache_load_volume`` |
| 130 |
- ``cache_store_volume`` |
| 131 |
- ``cache_load_calls`` |
| 132 |
- ``cache_store_calls`` |
| 133 |
- ``cache_delete_calls`` |