Cache randomly removing items

2019-07-17 19:43发布

I set up a simple local memory cache that I use like this:

from django.core.cache import caches

def stats_service(db):
    stats_cache = caches['stats']
    if stats_cache.get(db) is None:
        stats_cache.set(db, GlobalStatsService(db))
    return stats_cache.get(db)

After the server is running, I call this function through a view, with a curl on the command-line, to initialize the cache.

The problem is that if I call it several times, sometimes it will find the item and return the value immediately, as expected, and sometimes will not find it and will recalculate the value. The keys (here db) are the strings I expect them to be. I cannot understand why items are dropped from the cache, apparently randomly, and how to make them stay.

Interestingly, the behavior was the same when I was using global variables instead of Django's cache framework (and I tried them all except memcached because of the 1MB limitation).

I have set no TIMEOUT value (and obviously the global variables version had none either):

CACHES = {
    ...
    'stats': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': 'stats',
    },

My app runs with Apache and mod_wsgi, 2 processes and 4 threads. Maybe it is related. Could it be that a different process accesses its own version of the cache ?

What am I doing wrong ?

1条回答
放荡不羁爱自由
2楼-- · 2019-07-17 19:43

Yes, that is precisely what is happening. The local memory cache is exactly that: local to the process.

It is not really suitable for use in production, and definitely not in a multi-process environment. Use a proper cache backend; Redis for example is very simple to get up and running.

查看更多
登录 后发表回答