dogpile.cache icon indicating copy to clipboard operation
dogpile.cache copied to clipboard

Hit ratio statistics

Open masoudsafa opened this issue 3 years ago • 4 comments

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?

masoudsafa avatar Aug 29 '21 14:08 masoudsafa

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.

zzzeek avatar Aug 29 '21 15:08 zzzeek

Upvote for adding a metrics collection example to the recipes page :)

megan-carey avatar Nov 19 '21 18:11 megan-carey

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

jvanasco avatar Jan 17 '22 19:01 jvanasco

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],
)

glaucocustodio avatar Aug 11 '23 15:08 glaucocustodio