Cache¶
Responses are cached to ~/.go2web_cache.json using a simple TTL-based
key/value store. The cache respects Cache-Control: no-store and no-cache.
CacheStore¶
A file-backed key/value store for HTTP responses.
Entries are keyed by URL and serialised as JSON to cache_file.
Expired entries are evicted lazily on read. The store respects
Cache-Control: no-store and no-cache directives — responses
carrying these headers are never persisted.
Attributes:
| Name | Type | Description |
|---|---|---|
DEFAULT_TTL |
Default time-to-live in seconds ( |
Example
Using a temporary cache file:
from pathlib import Path from go2web.cache.store import CacheStore cache = CacheStore(cache_file=Path("/tmp/test_cache.json")) cache.set("https://example.com", 200, {}, "hello") cache.get("https://example.com").body 'hello' cache.clear()
Source code in src/go2web/cache/store.py
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 | |
__init__(cache_file=CACHE_FILE)
¶
Initialise the store, loading existing entries from cache_file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_file
|
Path
|
Path to the JSON file used for persistence. Created
on the first :meth: |
CACHE_FILE
|
Source code in src/go2web/cache/store.py
clear()
¶
get(url)
¶
Return the cached entry for url, or None if missing or expired.
A dim info message is printed to stderr when a valid entry is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The exact URL used as the cache key. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
CacheEntry | None
|
class: |
Source code in src/go2web/cache/store.py
set(url, status, headers, body)
¶
Store a response in the cache and persist to disk.
The entry is not stored when the server sends
Cache-Control: no-store or no-cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Cache key — should be the final (post-redirect) URL. |
required |
status
|
int
|
HTTP status code of the response. |
required |
headers
|
dict[str, str]
|
Response headers (lower-cased keys). |
required |
body
|
str
|
Decoded response body. |
required |
Source code in src/go2web/cache/store.py
CacheEntry¶
A single cached HTTP response.
Attributes:
| Name | Type | Description |
|---|---|---|
body |
str
|
The decoded response body. |
status |
int
|
The HTTP status code at the time of caching. |
headers |
dict[str, str]
|
Response headers (lower-cased keys). |
expires_at |
float
|
Unix timestamp after which this entry is considered stale. |