Issue #49 resolved

Offline caching?

LetoLab Ltd
created an issue

We have an issue with our app where we run update queries in a separate thread or via management commands. Johnny patches Django's ORM via a middleware, but if it hasn't been initialised (e.g. when using a management command), then caches are not invalidated. We can do this manually, but it becomes much harder to manage...

I was wondering if there's a way to patch the ORM without using a middleware?

Comments (1)

  1. Jason Moiron repo owner

    Instantiating the middleware patches the ORM, so you could enable johnny-cache in scripts by simply:

    from johnny.middleware import QueryCacheMiddleware
    qcm = QueryCacheMiddleware()
    
    # do some work
    
    qcm.unpatch() 
    # this unpatches and flushes the cache
    

    It's possible to patch not using the middleware:

    from johnny.cache import get_backend
    get_backend().patch()
    

    The QCM does a few things for you like keeping track of whether or not it is disabled and unpatching cleanly, but if these are little oneoffs the second method would work just as well.

  2. Log in to comment