Commits

Antti Kaihola committed d44f5b1

Documentation: Added a warning and instructions about Johnny outside Django's request-response loop

A common gotcha only mentioned in some closed Bitbucket issues is that
Johnny is not enabled in e.g. scripts, management commands,
asynchronous workers and the shell. I gathered and added to the
documentation some information about this from the following issues in
Bitbucket:

https://bitbucket.org/jmoiron/johnny-cache/issue/39
https://bitbucket.org/jmoiron/johnny-cache/issue/49
https://bitbucket.org/jmoiron/johnny-cache/issue/50

Comments (0)

Files changed (2)

 Johnny does not define any views, urls, or models, so we can skip adding it
 to ``INSTALLED_APPS``.
 
+*Note*: Johnny is *not* enabled by default in scripts, management commands,
+asynchronous workers and the shell.  See `the queryset cache documentation
+<queryset_cache.html#using-with-scripts-management-commands-asynchronous-workers-and-the-shell>`_
+for instructions on how to enable it in these cases.
+
+
 New in this version
 ~~~~~~~~~~~~~~~~~~~
 

docs/queryset_cache.rst

 
 .. autofunction:: johnny.cache.invalidate
 
+Using with scripts, management commands, asynchronous workers and the shell
+---------------------------------------------------------------------------
+
+Since the QuerySet Cache is enabled via middleware, queries made from outside
+of Django's request-response loop don't use Johnny. For example, saves and
+deletes don't invalidate models in management commands.
+
+You can enable the QuerySet Cache manually by instantiating the middleware in
+your code before using the ORM::
+
+    from johnny.middleware import QueryCacheMiddleware
+    qcm = QueryCacheMiddleware()
+
+    # do some work
+
+    qcm.unpatch() 
+    # this unpatches and flushes the cache
+ 
+To make sure Johnny is always active in management commands, you can enable it
+the project's ``__init__.py`` file::
+
+    cd /tmp
+    django-admin.py createproject myproject
+
+Now insert into ``/tmp/myproject/__init__.py``::
+
+    from johnny.middleware import QueryCacheMiddleware
+    QueryCacheMiddleware()
+
+This works because :func:`django.core.management.setup_environ` always imports
+the project module before executing the management command.
+
 
 Settings
 ~~~~~~~~