Skip to content

cache

Provide an extension for caching.

Classes:

  • Cache

    Define an extension base for caching.

  • CacheFile

    Define a extra step for caching.

Cache

Bases: Extension

Define an extension base for caching.

Methods:

  • load

    Load the cache for given key.

  • save

    Save the cache for given key.

load abstractmethod

load(key: Any) -> Any

Load the cache for given key.

Source code in src/rago/extensions/cache.py
21
22
23
24
@abstractmethod
def load(self, key: Any) -> Any:
    """Load the cache for given key."""
    raise Exception(f'Load method is not implemented: {key}')

save abstractmethod

save(key: Any, data: Any) -> None

Save the cache for given key.

Source code in src/rago/extensions/cache.py
26
27
28
29
@abstractmethod
def save(self, key: Any, data: Any) -> None:
    """Save the cache for given key."""
    raise Exception(f'Save method is not implemented: {key}')

CacheFile

CacheFile(target_dir: Path)

Bases: Cache

Define a extra step for caching.

Methods:

  • get_file_path

    Return the file path.

  • load

    Load the cache for given key.

  • save

    Load the cache for given key.

Source code in src/rago/extensions/cache.py
38
39
40
def __init__(self, target_dir: Path) -> None:
    self.target_dir = target_dir
    self.target_dir.mkdir(parents=True, exist_ok=True)

get_file_path

get_file_path(key: Any) -> Path

Return the file path.

Source code in src/rago/extensions/cache.py
42
43
44
45
def get_file_path(self, key: Any) -> Path:
    """Return the file path."""
    ref = sha256(str(key).encode('utf-8')).hexdigest()
    return self.target_dir / f'{ref}.pkl'

load

load(key: Any) -> Any

Load the cache for given key.

Source code in src/rago/extensions/cache.py
47
48
49
50
51
52
def load(self, key: Any) -> Any:
    """Load the cache for given key."""
    file_path = self.get_file_path(key)
    if not file_path.exists():
        return
    return joblib.load(file_path)

save

save(key: Any, data: Any) -> None

Load the cache for given key.

Source code in src/rago/extensions/cache.py
54
55
56
57
def save(self, key: Any, data: Any) -> None:
    """Load the cache for given key."""
    file_path = self.get_file_path(key)
    joblib.dump(data, file_path)