Singleton FileCache
Introduction
This class is a singleton class that stores data to file. The data can be anything that can be pickled.
It is used to store data that is not sensitive and can be shared between different instances of the same class.
This class is more of a dynamic singleton class. When the same parameter is passed to the constructor, the same instance is returned. If the parameter is different, a new instance is created. Custom key value pairs can be passed to the constructor to create a new instance. Custom key value pairs must be hashable.
New in version 0.52.0.
Examples
This example store data in a the LibreOffice tmp directory.
Something like /tmp/ooo_uno_tmpl/my_tmp/tmp_data.pkl
.
The cache is set to expire after 300
seconds.
from ooodev.utils.cache.singleton import FileCache
cache = FileCache(tmp_dir="my_tmp", lifetime=300)
data = get_json_data() # Get JSON data from somewhere as a dictionary
file_name = "tmp_data.pkl"
cache[file_name] = data
assert cache[file_name] == data
if file_name in cache:
del cache[file_name]
This example store data in a the LibreOffice tmp directory.
Something like /tmp/ooo_uno_tmpl/json/data.pkl
.
The cache is set to expire after 300
seconds.
import json
from ooodev.utils.cache.singleton import FileCache
cache = FileCache(tmp_dir="json", lifetime=300, custom1="custom1", custom2="custom2")
data = get_json_data() # Get JSON data from somewhere as a list of dictionary
file_name = "data.pkl"
cache[file_name] = data
assert cache[file_name] == data
if file_name in cache:
del cache[file_name]
Class
- class ooodev.utils.cache.singleton.FileCache(tmp_dir='', lifetime=-1, **kwargs)[source]
Bases:
CacheBase
Singleton Class. Caches files and retrieves cached files. Cached file are in ``ooo_uno_tmpl` subdirectory of LibreOffice tmp dir.
See Also Singleton FileCache
New in version 0.52.0.
- __contains__(key)
- Return type:
bool
- Parameters:
key (Any) –
- __delitem__(key)
- Return type:
None
- Parameters:
key (Any) –
- __getitem__(key)
- Return type:
Any
- Parameters:
key (Any) –
- __init__(tmp_dir='', lifetime=-1, **kwargs)
Constructor
- Parameters:
tmp_dir (Path, str, optional) – Dir name to create in tmp folder.
lifetime (float) – Time in seconds that cache is good for.
kwargs (Any) – Additional keyword arguments. The arguments are used to create a unique instance of the singleton class.
- Return type:
None
Note
The cache root temp folder is the LibreOffice temp folder.
- __setitem__(key, value)
- Return type:
None
- Parameters:
key (Any) –
value (Any) –
- get(filename)[source]
Fetches file contents from cache if it exist and is not expired
- Parameters:
filename (Union[str, Path]) – File to retrieve
- Returns:
File contents if retrieved; Otherwise,
None
- Return type:
Union[object, None]
- put(filename, content)[source]
Saves file contents into cache
- Parameters:
filename (Union[str, Path]) – filename to write.
content (Any) – Contents to write into file.
- Return type:
None
- remove(filename)
Deletes a file from cache if it exist
- Parameters:
filename (Union[str, Path]) – file to delete.
- Return type:
None
- property can_expire: bool
Gets/Sets cache expiration
- Return type:
bool
- property logger: NamedLogger
Gets logger
- Return type:
- property path: Path
Gets cache path
- Return type:
Path
- property path_settings: ThePathSettingsComp
Gets path settings
- Return type:
- property seconds: float
Gets/Sets cache time in seconds
- Return type:
float