operations
Extras for diskcache_utils.
Separates some functions/classes from the main diskcache_utils to shorten length of individual scripts.
check_cache(cache=None)
Run healthcheck on cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache |
Cache
|
A |
None
|
Source code in src/red_utils/ext/diskcache_utils/operations.py
check_cache_key_exists(cache=None, key=None)
Check if a key exists in a cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache |
Cache
|
A |
None
|
key |
str
|
The key name to search the |
None
|
Returns
(bool): `True` if the key exists
(bool): `False` if the key does not exist
Source code in src/red_utils/ext/diskcache_utils/operations.py
clear_cache(cache=None)
Clear all items from the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache |
Cache
|
The target cache to clear |
None
|
Returns
(bool): `True` if cache cleared successfully
(bool): `False` if cache not cleared successfully
Source code in src/red_utils/ext/diskcache_utils/operations.py
convert_to_seconds(amount=None, unit=None)
Convert an amount of time to seconds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount |
int
|
Amount of time |
None
|
unit |
str
|
The starting unit of time to convert to seconds. Options: ["seconds", "hours", "minutes", "days", "weeks"] |
None
|
Returns
(int): `amount` of time converted to seconds representing the `unit` of time passed
Source code in src/red_utils/ext/diskcache_utils/operations.py
delete_val(cache=None, key=None, tag=None)
Delete a value from the cache
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache |
Cache
|
A |
None
|
key |
str
|
The name of a key to delete |
None
|
tag |
str
|
Tag to filter by |
None
|
Source code in src/red_utils/ext/diskcache_utils/operations.py
get_cache_size(cache=None)
Get the total size of a diskcache.Cache instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache |
Cache
|
A |
None
|
Returns
(dict[str, int]): Details about the cache's size. Example:
`{"unit": "bytes", "size": cache_size}`
Source code in src/red_utils/ext/diskcache_utils/operations.py
get_val(cache=None, key=None, tags=None)
Search for a key in a given cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache |
Cache
|
A |
None
|
key |
str
|
A key name to retrieve from the cache |
None
|
tags |
list[str]
|
A list of tags to filter by |
None
|
Returns
(Any): The cached value
(dict[str, str]): A structured dict with error details, if operation fails
Source code in src/red_utils/ext/diskcache_utils/operations.py
manage_cache_tag_index(cache=None, operation='create')
Create or delete a cache's tag index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache |
Cache
|
A |
None
|
operation |
str
|
The operation (create/delete) to perform on the tag index |
'create'
|
Source code in src/red_utils/ext/diskcache_utils/operations.py
new_cache(cache_dir=CACHE_DIR, cache_conf=None, index=True)
Prepare and return a diskcache.Cache object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir |
str
|
Directory path where cache will be created |
CACHE_DIR
|
cache_conf |
dict
|
A Python |
None
|
index |
bool
|
Whether or not to create an index for the cache |
True
|
Returns
(diskcache.core.Cache): An initialized `diskcache.Cache` object
Source code in src/red_utils/ext/diskcache_utils/operations.py
set_expire(cache=None, key=None, expire=None)
Set/change a cache key's expiration time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache |
Cache
|
A |
None
|
key |
str
|
The cache key name to set an expiration time on |
None
|
expire |
int
|
Time (in seconds) before cache key expires |
None
|
Source code in src/red_utils/ext/diskcache_utils/operations.py
set_val(cache=None, key=None, val=None, expire=None, read=False, tag=None, retry=False)
Set a key value pair in the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache |
Cache
|
A |
None
|
key |
str
|
The key to store the value under in the cache |
None
|
val |
str
|
The value to store in the cache |
None
|
expire |
int
|
Time (in seconds) before value expires |
None
|
read |
bool
|
If |
False
|
tag |
str
|
Applies a tag to the cached value |
None
|
retry |
bool
|
If |
False
|