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
Argument | Type | Required | |
---|---|---|---|
key | str | The keyword to be validated. |
Returns
Argument | Type | |
---|---|---|
flag | bool | If 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
Argument | Type | Required | |
---|---|---|---|
key | str | The keyword of the data to be removed. |
Returns
Argument | Type | |
---|---|---|
info | Info | The 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
Argument | Type | Required | |
---|---|---|---|
key | str | The key value of this new data. If the key exists in the cache, will replace the original value. | |
info | Info | The light-weighted metadata to be dumped into the cache. | |
data | Data | The 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
Argument | Type | Required | |
---|---|---|---|
key | str | The key value of to be queried. If key does not exist in the cache, will raise a FileNotFoundError . |
Returns
Argument | Type | |
---|---|---|
info | Info | The light-weighted metadata queried in the cache. |
data_loader | Callable[[], 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
Argument | Type | Required | |
---|---|---|---|
key | str | The key value of to be queried. If key does not exist in the cache, will raise a FileNotFoundError . |
Returns
Argument | Type | |
---|---|---|
info | Info | The 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
Argument | Type | Required | |
---|---|---|---|
key | str | The key value of to be queried. If key does not exist in the cache, will raise a FileNotFoundError . |
Returns
Argument | Type | |
---|---|---|
data | Data | The 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
Argument | Type | Required | |
---|---|---|---|
key | str | The keyword to be validated. |
Returns
Argument | Type | |
---|---|---|
flag | bool | If True , the given key value exists in the cache. |