I am using the python library diskcache
and its decorater @cache.memoize
to cache calls to my couchdb database. Works fine. However, I would like to print to the user whether the data is returned from the database or from the cache.
I don't even know how to approach this problem.
My code so far:
import couchdb
from diskcache import Cache
cache = Cache("couch_cache")
@cache.memoize()
def fetch_doc(url: str, database: str, doc_id: str) -> dict:
server = couchdb.Server(url=url)
db = server[database]
return dict(db[doc_id])
Here's a way but I don't really recommend it because (1) it adds an extra operation of checking the cache manually yourself, and (2) it probably duplicates what the library is already doing internally. I don't have proper checking for any performance impact since I don't have a production data/env with varied
doc_id
s, but as martineau's comment says, it could slow things down because of an extra lookup operation.But here it goes.
The diskcache.Cache object "supports a familiar Python mapping interface" (like
dict
s). You can then manually check for yourself if a given key is already present in the cache, using the same key automatically generated based on the arguments to thememoize
-d function:So, you can wrap your
fetch_doc
function into another function, that checks if a cache key based on theurl
,database
, anddoc_id
arguments exists, prints the result to the user, all before calling the actualfetch_doc
function:When testing that out with:
It outputs:
You can turn that wrapper function into a decorator:
Or as suggested in the comments, combine it into 1 new decorator:
Some quick testing:
(It would be probably better if the
doc_id
is varied randomly in calls)Again, as I mentioned at the start, caching and
memoize
-ing the function call is supposed to speed-up that function. This answer adds additional operations of cache lookup and printing/logging whether or not you are fetching from DB or from cache, and it could impact the performance of that function call. Test appropriately.