Created
August 1, 2021 15:08
-
-
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
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
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