Skip to content

Instantly share code, notes, and snippets.

@jondkelley
Last active December 15, 2020 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jondkelley/4c61edf312a552edaea196238188fa91 to your computer and use it in GitHub Desktop.
Save jondkelley/4c61edf312a552edaea196238188fa91 to your computer and use it in GitHub Desktop.
Caching from serialized file example
# pip3 install cachepy
# https://github.com/scidam/cachepy
import time # used to simulate heavy CPU penalty
from cachepy import FileCache
from os.path import expanduser
from os import environ
import warnings
if not environ.get('SHOW_CACHEPY_WARNINGS'):
warnings.filterwarnings('ignore', module='cachepy')
cache = FileCache(expanduser('~/.logdnactl.es_account_percentages.cache.db'),
noc=10, # number of function calls before the cache is busted
ttl=30, # ttl in seconds before cache is busted
)
@cache
def es_account_percentages(x):
'''Performs heavy computations'''
print(f'--- Calculating heavy computation with argvalue {x}')
time.sleep(5)
return x**2
print('Just calling print(my_heavy_function(1))')
print(es_account_percentages(22))
print('Just calling print(my_heavy_function(5))')
print(es_account_percentages(5))
print('Just calling print(my_heavy_function(1))')
print(es_account_percentages(22))
print('Just calling print(my_heavy_function(1))')
print(es_account_percentages(22))
print('Just calling print(my_heavy_function(5))')
print(es_account_percentages(5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment