-
serializer (perscache.serializers.Serializer)
: a serializer class to use for converting stored data. Defaults toperscache.serlializers.PickleSerializer
. -
storage (perscache.storage.Storage)
: a storage back-end used to save and load data. Defaults toperscache.storage.LocalFileStorage
.
Tries to find a cached result of the decorated function in persistent storage. Returns the saved result if it was found, or calls the decorated function and caches its result.
Usage:
from perscache import Cache
cache = Cache()
@cache
def get_data():
...
The cache will be invalidated if the function code, its argument values or the cache serializer have been changed.
This method emits some log messages with a logger named perscache
. Add a handler to the logger to see the messages:
import logging
logging.getLogger('perscache').addHandler(logging.StreamHandler())
-
ignore (str | Iterable[str])
: arguments of the decorated function that will not be used in making the cache key. In other words, changes in these arguments will not invalidate the cache. Defaults toNone
. -
serializer (perscache.serializers.Serializer)
: Overrides the defaultCache()
serializer. Defaults toNone
. -
storage (perscache.storage.Storage)
: Overrides the defaultCache()
storage. Defaults toNone
. -
ttl (datetime.timedelta)
: The time-to-live of the cache for the decorated function. IfNone
, the cache never exprires. Defaults toNone
.
This class has no parameters. It is useful to alternate cache behaviour depending on the environment.
The underlying function will be called every time the decorated function has been called and no caching will take place.
This decorator will ignore any parameters it has been given.
Serializers are imported from the perscache.serializers
module.
To see which serializers are compatible with which data types, see the compatibility.py file.
The abstract base serializer class.
See also how to make your own serializer.
This function is used to create a serializer class.
class_name (str)
: the name of the serializer class.ext (str)
: the extension of the serialized file.dumps_fn (Callable)
: the function used to serialize an object into bytes. Accepts a single argument, the object to serialize, and returns a bytes object.loads_fn (Callable)
: the function used to deserialize bytes into an object. Accepts a single argument, the bytes to deserialize, and returns an object.
A serializer class derived from perscache.serializers.Serializer
that can be used with perscache.Cache
.
from perscache.serializers import make_serializer
from perscache import Cache
PyrogramSerializer = make_serializer(
class_name = 'PyrogramSerializer',
ext = 'pyrogram',
dumps_fn = lambda x: str(x).encode('utf-8'),
loads_fn = lambda x: eval(x.decode('utf-8')),
)
cache = Cache()
@cache(serializer=PyrogramSerializer())
def get_data():
...
Uses the cloudpickle
module. It's the most capable serializer of all, able to process most of the data types.
It's the default serializer for the Cache
class.
Uses the json
module.
Uses the yaml
module.
Uses the pickle
module.
Uses the pandas
module. Processes pandas.DataFrame
objects.
Uses the pyarrow
module. Processes pandas.DataFrame
objects.
compression (str)
: compression used bypyarrow
to save the data. Defaults to"brotli"
.
Storage back-ends are imported from the perscache.storage
module.
See also how to make your own storage back-end.
Keeps cache entries in separate files in a file system directory.
This is the default storage class used by Cache
.
-
location (str)
: a directory to store the cache files. Defaults to".cache"
. -
max_size (int)
: the maximum size for the cache. If set, then, before a new cache entry is written, the future size of the directory is calculated and the least recently used cache entries are removed. IfNone
, the cache grows indefinitely. Defaults toNone
.
Keeps cache entries in separate files in a Google Cloud Storage Bucket.
Relies on the gcsfs
module, which is not a part of the project dependencies and needs to to be installed by the user if he is to use this class.
-
location (str)
: a directory to store the cache files. Defaults to".cache"
. -
max_size (int)
: the maximum size for the cache. If set, then, before a new cache entry is written, the future size of the directory is calculated and the least recently used cache entries are removed. IfNone
, the cache size grows indefinitely. Defaults toNone
. -
storage_options (dict)
: a dictionary of parameters to pass to the constructor of theGSCFilesystem
class of thegcsfs
module (see the module documentation for more information). Defaults toNone
.
# supposing gcsfs is installed
from perscache import Cache
from perscache.storage import GoogleCloudStorage
cache = Cache(
storage=GoogleCloudStorage(
location="my-bucket/cache",
storage_options={"token": "my-token.json"}
)
)
@cache
def get_data():
...