Singleton TextCache
Introduction
This class is a singleton class that stores text data to file.
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 text data in a the LibreOffice tmp directory.
Something like /tmp/ooo_uno_tmpl/my_tmp/tmp_data.txt
.
The cache is set to expire after 300
seconds.
from ooodev.utils.cache.singleton import TextCache
cache = TextCache(tmp_dir="my_tmp", lifetime=300)
file_name = "temp_data.txt"
cache[file_name] = "Hello World!"
print(cache[file_name]) # prints "Hello World!"
if file_name in cache:
del cache[file_name]
This example store text data in a the LibreOffice tmp directory.
Something like /tmp/ooo_uno_tmpl/txt_only/text_data.txt
.
The cache is set to expire after 300
seconds.
from ooodev.utils.cache.singleton import TextCache
cache = TextCache(tmp_dir="txt_only", lifetime=300, custom1="custom1", custom2="custom2")
file_name = "text_data.txt"
cache[file_name] = "Hello World!"
print(cache[file_name]) # prints "Hello World!"
if file_name in cache:
del cache[file_name]
Class
- class ooodev.utils.cache.singleton.TextCache(tmp_dir='', lifetime=-1, **kwargs)[source]
Bases:
CacheBase
Singleton Class. Caches files and retrieves cached files. Cached file are in a subfolder of system tmp dir.
- __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 (str) – File to retrieve
- Returns:
File contents if retrieved; Otherwise,
None
- Return type:
Union[str, None]
- put(filename, content)[source]
Saves file contents into cache
- Parameters:
filename (str) – filename to write.
content (str) – 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