Last active
December 15, 2020 20:25
-
-
Save jondkelley/4c61edf312a552edaea196238188fa91 to your computer and use it in GitHub Desktop.
Caching from serialized file example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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