Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hit ratio statistics #210

Open
masoudsafa opened this issue Aug 29, 2021 · 4 comments
Open

Hit ratio statistics #210

masoudsafa opened this issue Aug 29, 2021 · 4 comments
Labels
question Further information is requested

Comments

@masoudsafa
Copy link

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?

@zzzeek
Copy link
Member

zzzeek commented Aug 29, 2021

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 zzzeek added the question Further information is requested label Aug 29, 2021
@megan-carey
Copy link

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

@jvanasco
Copy link
Member

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

@glaucocustodio
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants