Caching¶
BaseCache
¶
BaseCache persistence interface.
Purpose
Defines the cache interface for dictionaries, expiration, and namespace keys.
Attributes:
| Name | Type | Description |
|---|---|---|
None |
The abstract interface has no declared instance attributes. |
Methods:¶
get
¶
Retrieve an entry by its cache key.
Purpose
Retrieve an entry by its cache key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key identifying the record. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
Optional[ Dict[ str, Any ] ]: Result of the get operation. |
Source code in storage.py
set
¶
Store a key/value pair with an optional time-to-live.
Purpose
Store a key/value pair with an optional time-to-live.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key identifying the record. |
required |
value
|
Dict[str, Any]
|
Value or user-entered filter value. |
required |
ttl
|
Optional[int]
|
Optional time-to-live in seconds. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
Completes the operation through side effects. |
Source code in storage.py
delete
¶
Remove a cache entry.
Purpose
Remove a cache entry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key identifying the record. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
Completes the operation through side effects. |
Source code in storage.py
clear
¶
Clear entries matching an optional namespace.
Purpose
Clear entries matching an optional namespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
Optional[str]
|
Optional cache namespace. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
Completes the operation through side effects. |
Source code in storage.py
contains
¶
Determine whether a cache entry exists.
Purpose
Determine whether a cache entry exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key identifying the record. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
Result of the contains operation. |
Source code in storage.py
namespace_key
¶
Create a cache key prefixed by a namespace.
Purpose
Create a cache key prefixed by a namespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
str
|
Logical cache namespace, such as geocode, places, weather, distance, or fetcher. |
required |
key
|
str
|
Raw cache key. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Namespace-qualified cache key. |
Source code in storage.py
stats
¶
Return cache usage and diagnostic statistics.
Purpose
Return cache usage and diagnostic statistics.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[ str, Any ]: Result of the stats operation. |
InMemoryCache
¶
Bases: BaseCache
InMemoryCache persistence interface.
Purpose
Caches records in memory and tracks cache use, writes, and deletions.
Attributes:
| Name | Type | Description |
|---|---|---|
store |
Dict[str, Dict[str, Any]]
|
Internal cache record dictionary. |
hits |
int
|
Successful cache reads. |
misses |
int
|
Unsuccessful cache reads. |
sets |
int
|
Cache writes. |
deletes |
int
|
Deleted cache entries. |
Initialize empty cache records and operation counters.
Purpose
Initialize empty cache records and operation counters.
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
Completes the operation through side effects. |
Source code in storage.py
Methods:¶
now
¶
Return the current Unix timestamp.
Purpose
Return the current Unix timestamp.
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Current Unix timestamp. |
Source code in storage.py
make_record
¶
Create a cache metadata record for the supplied key and value.
Purpose
Create a cache metadata record for the supplied key and value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key. |
required |
value
|
Dict[str, Any]
|
JSON-serializable cache value. |
required |
ttl
|
Optional[int]
|
Optional time-to-live in seconds. |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: Cache metadata record. |
Source code in storage.py
is_expired
¶
Determine whether an in-memory cache record has expired.
Purpose
Determine whether an in-memory cache record has expired.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
Dict[str, Any]
|
Cache metadata record. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if expired; otherwise False. |
Source code in storage.py
get
¶
Return a cached value by key when present and not expired.
Purpose
Return a cached value by key when present and not expired.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
Optional[Dict[str, Any]]: Cached value or None. |
Source code in storage.py
set
¶
Set a cache value by key.
Purpose
Set a cache value by key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key. |
required |
value
|
Dict[str, Any]
|
JSON-serializable cache value. |
required |
ttl
|
Optional[int]
|
Optional time-to-live in seconds. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in storage.py
delete
¶
Delete a cache entry by key.
Purpose
Delete a cache entry by key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in storage.py
clear
¶
Clear all cache entries or only entries under a namespace prefix.
Purpose
Clear all cache entries or only entries under a namespace prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
Optional[str]
|
Optional namespace prefix. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in storage.py
contains
¶
Determine whether a cache key exists and is not expired.
Purpose
Determine whether a cache key exists and is not expired.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the key exists and is not expired. |
Source code in storage.py
stats
¶
Return cache diagnostics.
Purpose
Return cache diagnostics.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: Cache diagnostics. |
Source code in storage.py
namespace_key
¶
Create a cache key prefixed by a namespace.
Purpose
Create a cache key prefixed by a namespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
str
|
Logical cache namespace, such as geocode, places, weather, distance, or fetcher. |
required |
key
|
str
|
Raw cache key. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Namespace-qualified cache key. |
Source code in storage.py
SQLiteCache
¶
Bases: BaseCache
SQLiteCache persistence interface.
Purpose
Persists cache records and time-to-live metadata in an on-disk SQLite database.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
Optional[str]
|
SQLite cache file path. |
connection |
Connection
|
Active SQLite cache connection. |
Open the SQLite cache file and initialize its schema.
Purpose
Open the SQLite cache file and initialize its schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
SQLite database filename. |
'mappy_cache.sqlite'
|
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
Completes the operation through side effects. |
Source code in storage.py
Methods:¶
initialize
¶
Create the SQLite cache table when it does not already exist.
Purpose
Create the SQLite cache table when it does not already exist.
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in storage.py
migrate
¶
Add metadata columns to older kv tables that only contain k and v.
Purpose
Add metadata columns to older kv tables that only contain k and v.
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in storage.py
now
¶
Return the current Unix timestamp.
Purpose
Return the current Unix timestamp.
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Current Unix timestamp. |
Source code in storage.py
get
¶
Return a cached value by key when present and not expired.
Purpose
Return a cached value by key when present and not expired.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cached value or None. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
Optional[ Dict[ str, Any ] ]: Result of the get operation. |
Source code in storage.py
set
¶
Set a cache value by key.
Purpose
Set a cache value by key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key. |
required |
value
|
Dict[str, Any]
|
JSON-serializable cache value. |
required |
ttl
|
Optional[int]
|
Optional time-to-live in seconds. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in storage.py
delete
¶
Delete a cache entry by key.
Purpose
Delete a cache entry by key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in storage.py
clear
¶
Clear all cache entries or only entries under a namespace prefix.
Purpose
Clear all cache entries or only entries under a namespace prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
Optional[str]
|
Optional namespace prefix. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in storage.py
contains
¶
Determine whether a cache key exists and is not expired.
Purpose
Determine whether a cache key exists and is not expired.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the key exists and is not expired. |
Source code in storage.py
purge_expired
¶
Delete expired cache entries.
Purpose
Delete expired cache entries.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of rows deleted. |
Source code in storage.py
stats
¶
Return SQLite cache diagnostics.
Purpose
Return SQLite cache diagnostics.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: Cache diagnostics. |
Source code in storage.py
close
¶
Close the SQLite connection.
Purpose
Close the SQLite connection.
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in storage.py
namespace_key
¶
Create a cache key prefixed by a namespace.
Purpose
Create a cache key prefixed by a namespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
str
|
Logical cache namespace, such as geocode, places, weather, distance, or fetcher. |
required |
key
|
str
|
Raw cache key. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Namespace-qualified cache key. |