Package: cachepy
A caching toolset for Python. It is tested for both
Python 2.7.x and 3.4+ (<3.10).FeaturesMemory-based and file-based caches;Ability to set the TTL (time-to-live) and NOC (the number of calls) on caches;Encryption of the cached data (symmetric encryption algorithm (RSA) is used);LFU (least frequently used) and MFU (most frequently used) cache-clearing strategies;caches of limited size;NotesEncryption functionality requires thePyCryptodomepackage to be installed;File-based caches save the cached data in a file; files with cached data should be
cleaned up manually, if needed.Examplesfromcachepyimport*mycache=Cache()# save the cached data to memory without encryption@mycachedefmy_heavy_function(x):'''Performs heavy computations'''print('Hi, I was invoked...')returnx**2my_heavy_function(2)# "Hi, I was invoked..." will be printed to stdout only once# return 4my_heavy_function(2)# return 4To store data to a file, one needs to initialize a decorator, as follows:# create cache-to-file decoratorfilecache=FileCache('mycache')# mycache.dat file will be created;# `.dat` extension is appended automatically to the filename# (depends on the shelve module implementation);Its behavior is the same as a memory-based one, but all cached data is stored in
the specified file.One can set up time-to-live (TTL) and/or maximum number of calls (NOC)
for the cached data when the decorator is initialized:importtimefromcachepyimport*cache_with_ttl=Cache(ttl=2)# ttl given in seconds@cache_with_ttldefmy_heavy_function(x):'''Performs heavy computations'''print('Hi, I was invoked...')returnx**2my_heavy_function(3)# Hi, I was invoked... will be printed# return 9my_heavy_function(3)# 'Hi, I was invoked ...' will not be printed# return 9time.sleep(2)my_heavy_function(3)# 'Hi, I was invoked ...' will be printed again# return 9cache_with_noc=Cache(noc=2)# the number-of-calls: noc = 2@cache_with_nocdefmy_heavy_function(x):'''Performs heavy computations'''print('Hi, I was invoked...')returnx**2my_heavy_function(3)my_heavy_function(3)# 'Hi, I was invoked ...' will not be printedmy_heavy_function(3)# 'Hi, I was invoked ...' will be printed againIt is easy to use bothNOCandTTLarguments when defining
a caching decorator:cache_with_noc_ttl=Cache(noc=2,ttl=1)@cache_with_noc_ttldefmy_heavy_function(x):'''Performs heavy computations'''print('Hi, I was invoked...')returnx**2my_heavy_function(3)my_heavy_function(3)# 'Hi, I was invoked ...' will not be printedmy_heavy_function(3)# 'Hi, I was invoked ...' will be printed (noc is# reached, recompute the func value)time.sleep(2)# get ttl expiredmy_heavy_function(3)# 'Hi, I was invoked ...' will be printed againOne can encrypt the cached data by providing a non-emptykeyargument as
a password (RSA encryption algorithm is used):cache_to_file_ttl_noc=FileCache('mycache',noc=2,ttl=2,key='mypassword')@cache_to_file_ttl_nocdefmy_heavy_function(x):'''Performs heavy computations'''print('Hi, I was invoked...')returnx**2my_heavy_function(2)# 'Hi, I was invoked...' will be printedmy_heavy_function(2)# 'Hi, I was invoked...' will not be printedWhenmy_heavy_functionis decorated bycache_to_file_ttl_noc, as shown
in the example above, the value2**2 = 4will be computed and the result of
the computation will be stored in the file namedmycache.dat. Along
with the result of the computation,  additional information will be stored
in the filemycache.dat. The additional information includes:
1) the result’s expiration time (computed from the TTL),
2) NOC and 3) the number of already performed calls of the function being
decorated (my_heavy_function).Encryption is available only ifPyCryptodomepackage is installed and thekeyparameter (a non-empty string representing the password) is passed to the
cache constructor. It also could work with the old PyCrypto package.If you passed the non-emptykeyparameter to the cache constructor
butPyCryptodomewas not found, a special warning would be raised in this case
(“PyCryptodome not installed. Data will not be encrypted”) and
the cache would work as usual but without encryption functionality.Caching with limitationsStandard cache constructors are used to initialize caches of unlimited capacity.
There are also caches of limited capacity.
Such caches are initialized by constructors namedLimitedCacheandLimitedFileCache.
These constructors have additional
parameterscache_size(the maximum number of items stored in the cache) andalgorithm(cache-clearing algorithm). Availablealgorithmvalues arelfu(default, which stands for least frequently used) andmfu(most frequently used).
Whenalgorithm=’lfu’, then the least frequently used item is removed from the cache,
if it is exhausted. In case ofalgorithm=’mfu’, everything behaves the same way,
with the only difference being that the most frequently used item is removed.Testingpython-mcachepy.testTODOWriting backend for redis serverLog listVersion 1.2 (testing compatibility with Python 3.8 - 3.9)Version 1.1Version 1.0 (broken installation via pip/pipenv)Version 0.1initial releaseAuthorDmitry Kislov <kislov@easydan.com>