Singleton FileChangeAwareCache

Introduction

This class is a singleton class that stores data to file. Also the cache is invalidated when the file is changed. 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/x75c002da1ba8f255013111f3084243be.pkl.

The cache is set to expire when file changes.

from __future__ import annotations
from typing import Any
import json
from pathlib import Path
from ooodev.utils.cache.singleton import FileChangeAwareCache

def get_json_data(json_file: str | Path) -> Any:
    with open(json_file, 'r', encoding="utf-8") as json_file:
        data = json.load(json_file)
    return data

def save_json_data(json_file: str | Path, data: Any) -> None:
    with open(json_file, 'w', encoding="utf-8") as json_file:
        json.dump(data, json_file)

def get_cache_data(cache: FileChangeAwareCache, json_file: str | Path) -> Any:
    data = cache[json_file]
    if data is None:
        data = get_json_data(json_file)
        cache[json_file] = data
    return data

cache = FileChangeAwareCache(tmp_dir="my_tmp")
json_file = Path("/user/me/Documents/User.json")

data = get_cache_data(json_file)

data["new_key"] = "new_value"
# once the file is changed, the cache is invalidated
save_json_data(json_file, data)

data = get_cache_data(json_file)
assert data["new_key"] == "new_value"

Class

class ooodev.utils.cache.singleton.FileChangeAwareCache(*, tmp_dir='', **kwargs)[source]

Bases: object

Singleton Class. Caches files and retrieves cached files. Cached file are in ``ooo_uno_tmpl` subdirectory of LibreOffice tmp dir.

See Also Singleton FileChangeAwareCache

New in version 0.52.0.

__contains__(key)[source]
Return type:

bool

Parameters:

key (Any) –

__delitem__(key)[source]
Return type:

None

Parameters:

key (Any) –

__getitem__(key)[source]
Return type:

Any

Parameters:

key (Any) –

__init__(*, tmp_dir='', **kwargs)[source]

Constructor

Parameters:
  • tmp_dir (Path, str, optional) – Dir name to create in tmp folder. Defaults to ooo_uno_tmpl.

  • 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)[source]
Return type:

None

Parameters:
  • key (Any) –

  • value (Any) –

get(file_path)[source]

Fetches file contents from cache if it exist and is not expired

Parameters:

file_path (str, Path) – File to retrieve

Returns:

File contents if retrieved; Otherwise, None

Return type:

Union[object, None]

put(file_path, content)[source]

Saves file contents into cache

Parameters:
  • file_path (str, Path) – filename to write.

  • content (Any) – Contents to write into file.

remove(file_path)[source]

Deletes a file from cache if it exist

Parameters:

file_path (str, Path) – file to delete.

Return type:

None

property logger: NamedLogger

Gets logger

Return type:

NamedLogger

property path: Path

Gets cache path

Return type:

Path