Skip to content Skip to sidebar Skip to footer

How To Exclude Parameters When Caching Function Calls With Diskcache And Memoize?

I am using Python's DiskCache and the memoize decorator to cache function calls to a database of static data. from diskcache import Cache cache = Cache('database_cache) @cache.me

Solution 1:

Documentation for memoize doesn't show option to exclude parameters.

You may try to write own decorator - using source code.

Or use cache on your own inside fetch_document - something like this

deffetch_document(row_id: int, user: str, password: str):
    if row_id in cache:
         return cache[row_id]

    # ... code ...# result = ...

    cache[row_id] = result

    return result              

EDIT:

OR create cached version of your function - like this

defcached_fetch_document(row_id: int, user: str, password: str):
    if row_id in cache:
         return cache[row_id]

    result = fetch_document(row_id: int, user: str, password: str)

    cache[row_id] = result

    return result              

and later you can decide if you want to use cached_fetch_document in place of fetch_document

Post a Comment for "How To Exclude Parameters When Caching Function Calls With Diskcache And Memoize?"