dogpile.cache
dogpile.cache copied to clipboard
Hit ratio statistics
Hi guys In order to monitor and improve my app, i need to know number of calls and cache hit ratio per key in certain intervals. is it possible with dogpile?
I would implement this by intercepting calls to the backend, which you can do using ProxyBackend. we can add examples to https://dogpilecache.sqlalchemy.org/en/latest/recipes.html if someone wants to come up with this.
Upvote for adding a metrics collection example to the recipes page :)
FWIW, I open sourced a Pyramid plugin to do this a few years ago, which illustrates how to accomplish this.
The package offers a DogpileLoggingProxy
proxy object - derived from ProxyBackend - which is used to track all the Dogpile API calls, and stashes the metrics on the request object.
https://github.com/jvanasco/pyramid_debugtoolbar_dogpile
If you are using Redis as backend, you should override set_serialized
and get_serialized
instead as get
and set
are not implemented on this backend. Eg:
from dogpile.cache import make_region
from dogpile.cache.proxy import ProxyBackend
from dogpile.cache.api import NO_VALUE
class ReportingProxy(ProxyBackend):
def set_serialized(self, key, value):
# report cache miss
self.proxied.set_serialized(key, value)
def get_serialized(self, key):
cached = self.proxied.get_serialized(key)
if cached != NO_VALUE:
# report cache hit
pass
return cached
region = make_region().configure(
'dogpile.cache.redis',
arguments={
'url': 'redis://localhost:6379/0', # Redis connection URL
'distributed_lock': True, # Use distributed lock to prevent dogpiling
'thread_local_lock': False,
},
wrap=[ReportingProxy],
)