Issue #17 resolved

johny-cache incompatible w/ TransactionManager

Anonymous created an issue

From settings.py:

MIDDLEWARE_CLASSES = ( 'johnny.middleware.QueryCacheMiddleware', 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.locale.LocaleMiddleware', 'django.middleware.gzip.GZipMiddleware', 'django.middleware.http.ConditionalGetMiddleware', 'johnny.middleware.LocalStoreClearMiddleware', 'django.middleware.transaction.TransactionMiddleware', )

It looks as though unless either the TransactionMiddleware is commented out or the @transaction.autocommit decorator is used, johny-cache never commits local cache that was populated in a template to the cache backend (e.g. memcached).

view.py: def foo(self): foo_list = Foo.objects.all().filter(...) render_to_response('foo.html', {'foo_list': foo_list'}, context_instance=RequestContext(req))

foo.html:

{% for foo in foo_list %} {{ foo }} {% endfor %}

Should see a ton of uncached queries for foo objects. Using PostgreSQL and django 1.2.1. Thoughts?

Comments (3)

  1. Anonymous

    Err... updating w/ better formatting. Can find me on irc on freenode as seanc0

    # From settings.py:
    
    MIDDLEWARE_CLASSES = (
        'johnny.middleware.QueryCacheMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.locale.LocaleMiddleware',
        'django.middleware.gzip.GZipMiddleware',
        'django.middleware.http.ConditionalGetMiddleware',
        'johnny.middleware.LocalStoreClearMiddleware',
        'django.middleware.transaction.TransactionMiddleware',
    )
    

    It looks as though unless either the TransactionMiddleware is commented out or the @transaction.autocommit decorator is used, johny-cache never commits local cache that was populated in a template to the cache backend (e.g. memcached).

    # view.py:
      def foo(self):
        foo_list = Foo.objects.all().filter(...)
        render_to_response('foo.html', {'foo_list': foo_list'}, context_instance=RequestContext(req))
    
    foo.html:
    
    {% for foo in foo_list %}
      {{ foo }}
    {% endfor %}
    

    Should see a ton of uncached queries for foo objects. Using PostgreSQL and django 1.2.1. Thoughts?

  2. Jason Moiron repo owner

    So, it turns out that this bug _was_ entirely related to ticket 9964 in django. The the TransactionManager leaves clean transactions hanging, which means that johnny never pushes its local transaction reads to the global cache.

    I've added a "fixed" version of the TransactionMiddleware which will always commit transactions. There are reasons why you might not want this, and they are covered pretty thoroughly in the django ticket where this behavior is being discussed. However, if you want johnny to function, you can use:

    'johnny.middleware.CommittingTransactionMiddleware'

    Instead of:

    'django.middleware.transaction.TransactionMiddleware'

    I've added tests that verify the behaviors of both pieces of middleware. Johnny's docs will be updated to reflect this caveat with the TransactionMiddleware, since it's shipped as part of Django.

  3. Log in to comment