Skip to content

Instantly share code, notes, and snippets.

@mzpqnxow
Created August 1, 2021 15:08
Show Gist options
  • Save mzpqnxow/4c87dab9d9b684a988dcc20fe4bd3407 to your computer and use it in GitHub Desktop.
Save mzpqnxow/4c87dab9d9b684a988dcc20fe4bd3407 to your computer and use it in GitHub Desktop.
Using cachetools as a convenient short-hand to use Python functiion caching for a specific argument
from cachetools import cached
from cachetools.keys import hashkey
from random import randint
@cached(cache={}, key=lambda db_handle, query: hashkey(query))
@cached(cache={}, key=lambda db_handle, query: hashkey(query))
def find_object(db_handle, query):
saved = query
query = 5
print("processing {0}".format(query))
query = saved
return query
queries = list(range(5))
queries.extend(range(5))
for q in queries:
print("result: {0}".format(find_object(randint(0, 1000), q)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment