0

In a views I have this cache which is supposed to save some costly queries:

from django.core.cache import cache
LIST_CACHE_TIMEOUT = 120
....

topics = cache.get('forum_topics_%s' % forum_id)        

if not topics:        
        topics = Topic.objects.select_related('creator') \
                            .filter(forum=forum_id).order_by("-created")
        print 'forum topics not in cache', forum_id #Always printed out
        cache.set('forum_topics_%s' % forum_id, topics, LIST_CACHE_TIMEOUT)

I don't have problem using this method to cache other queryset results and can not think of the reson of this strange behavior, so I appreciate your hints about this.

Jand
  • 2,527
  • 12
  • 36
  • 66

1 Answers1

1

I figured out what caused this: memcache hash value can not be larger than 1mb. So I switched to redis, and the problem was gone:

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    }
}

IMPORTANT: make sure that redis version is 2.6 or higher. redis-server --version In older versions of redis, apparently redis does not recognize key timeout parameter and throughs error. This tripped me a while because the default redis on Debian 7 was 2.4.

Jand
  • 2,527
  • 12
  • 36
  • 66