I try to install johnny cache with my django website.

So i set up all the johnny cache related settings like this:

CACHES = {
  'default': {
    # 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',                                                                                                        
    'LOCATION': '127.0.0.1:11211',
    'BACKEND': 'johnny.backends.memcached.MemcachedCache',
    'JOHNNY_CACHE': True,
  }
}

So far, the whole project still run properly in production mode. But immediatly after setting up the middlewares 'johnny.middleware.LocalStoreClearMiddleware' and 'johnny.middleware.QueryCacheMiddleware' I get the following exception:

Environment:

Request Method: GET
Django Version: 1.3.1
Python Version: 2.6.6
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'core_knowledge_platform.core_web_service',
 'south']
Installed Middleware:
('johnny.middleware.LocalStoreClearMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'johnny.middleware.QueryCacheMiddleware')


Traceback:
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
  117.                             response = middleware_method(request, e)
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/u1/msc/vg55/public_html/core_knowledge_web_platform/core_knowledge_platform/core_web_service/views.py" in __call__
  95.         return getattr(self, method)(request, *args, **kwargs)
File "/u1/msc/vg55/public_html/core_knowledge_web_platform/core_knowledge_platform/core_web_service/views.py" in GET
  673.             if not access.validate_user_is_editor(request.user):
File "/u1/msc/vg55/public_html/core_knowledge_web_platform/core_knowledge_platform/core_web_service/business_logic/access.py" in validate_user_is_editor
  38.     if papergroups:
File "/usr/lib/python2.6/site-packages/django/db/models/query.py" in __nonzero__
  113.             iter(self).next()
File "/usr/lib/python2.6/site-packages/django/db/models/query.py" in _result_iter
  107.                 self._fill_cache()
File "/usr/lib/python2.6/site-packages/django/db/models/query.py" in _fill_cache
  772.                     self._result_cache.append(self._iter.next())
File "/usr/lib/python2.6/site-packages/django/db/models/query.py" in iterator
  273.         for row in compiler.results_iter():
File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py" in results_iter
  698.                     row = self.resolve_columns(row, fields)
File "/usr/lib/python2.6/site-packages/django/db/backends/mysql/compiler.py" in resolve_columns
  12.         return row[:index_extra_select] + tuple(values)

Exception Type: TypeError at /publication/
Exception Value: cannot concatenate 'str' and 'tuple' objects

NB: johnny cache is not set in the INSTALLED_APPS as the documentation says it's useless

EDIT:

Well, I've discovered that the exception is thrown only when there is a cache hit. When I wait for the stored values in memcached to be expired, then on loading page, no exception is thrown...

To add more shadow to my issue, when I run the whole project with johnny activated in localhost, everything works out. But when I run it in prod environment (Apache/2.2.15 (CentOS)), there the exception is thrown... And django version on both environment are exactly the same: 1.3.1

NB: I've set up the middleware order as specified by okm

Thank you

有帮助吗?

解决方案

I have had the exact same issue. While I am still trying to get to the exact bottom of it, I have found some useful things.

  1. Older versions of johnny cache work. Since I need master/slave setups to work, I used this fork: https://bitbucket.org/skoczen/johnny-cache It only includes commits up to November, so the problem is introduced after that.

  2. On the admin pages, the error is caused by the auth_user and auth_table table caches. Adding these to the JOHNNY_BLACKLIST circumvents the issue, and also disables caching for those tables. I doubt the issue is unique to those tables, so I'm blacklisting those tables is not a good solution.

I have checked my configuration and tried many things, just like you and that is not the problem.

Hope this helps.

Edit: More digging has revealed that the issue crops up with this commit bcdb46c5d357, which added code to cache queries returning null: https://bitbucket.org/jmoiron/johnny-cache/changeset/bcdb46c5d357

If you stick to an earlier one, it should work.

其他提示

Then the ordering of middlewares could be the reason of the error, try

'johnny.middleware.LocalStoreClearMiddleware',
'johnny.middleware.QueryCacheMiddleware', # Here
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',    
# Probably you need this for transaction, later
# 'johnny.middleware.CommittingTransactionMiddleware',

The 'johnny.middleware.QueryCacheMiddleware' should be initialized and thus be placed before other middlewares. 'johnny.middleware.LocalStoreClearMiddleware' only deals w/ response and exception, thus it could be the first.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top