Skip to main content

CacheAbstract

ClassAbstractSource

cache = CacheAbstract[Info, Data]()

The abstract cache class, where Info and Data are generic types.

In this project, Info and Data are configured as CachedFileInfo and CachedData, respectively.

Derive this base class to provide more implementations of caches.

Abstract Methods

is_in

flag: bool = cache.is_in(key: str)

Abstract method checking whether key is registered in the cache.

The __contains__ operator is delegated to this method.

Requires

ArgumentTypeRequired
Description
keystrThe keyword to be validated.

Returns

ArgumentType
Description
flagboolIf True, the given key value exists in the cache.

remove

info: Info = cache.remove(key: str)

Abstract method for removing the info of one cached item from this cache.

Using this method implies that the data in the cache reaches its end of life. Only the cache item information will still be usable.

Requires

ArgumentTypeRequired
Description
keystrThe keyword of the data to be removed.

Returns

ArgumentType
Description
infoInfoThe light-weighted metadata queried in the cache.

dump

cache.dump(key: str, info: Info, data: Data)

Abstract method for dumping data to the cache.

Requires

ArgumentTypeRequired
Description
keystrThe key value of this new data. If the key exists in the cache, will replace the original value.
infoInfoThe light-weighted metadata to be dumped into the cache.
dataDataThe data to be dumped into the cache.

load

info, data_loader = cache.load(key: str)

Abstract method for loading the data by a specific keyword.

Requires

ArgumentTypeRequired
Description
keystrThe key value of to be queried. If key does not exist in the cache, will raise a FileNotFoundError.

Returns

ArgumentType
Description
infoInfoThe light-weighted metadata queried in the cache.
data_loaderCallable[[], Data]The lazy loader for the data. This function implements a deferred loading mechanism allowing that the large-size data to be actually loaded when this method is called.

Methods

load_info

info: Info = cache.load_info(key: str)

Load the metadata by a specific keyword.

This method is implemented by fetching the returned value load(key)[0].

Requires

ArgumentTypeRequired
Description
keystrThe key value of to be queried. If key does not exist in the cache, will raise a FileNotFoundError.

Returns

ArgumentType
Description
infoInfoThe light-weighted metadata queried in the cache.

load_data

data: Data = cache.load_data(key: str)

Load the data by a specific keyword.

This method is implemented by fetching and calling load(key)[1]().

Requires

ArgumentTypeRequired
Description
keystrThe key value of to be queried. If key does not exist in the cache, will raise a FileNotFoundError.

Returns

ArgumentType
Description
dataDataThe data object queried in the cache. Since data may be large, this value should be a file-like object in most cases.

Operators

__contains__

flag: bool = key in cache

Check whether key is registered in the cache.

Requires

ArgumentTypeRequired
Description
keystrThe keyword to be validated.

Returns

ArgumentType
Description
flagboolIf True, the given key value exists in the cache.