Cache randomly removing items

寵の児 提交于 2020-01-04 05:19:12

问题


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:


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.



来源:https://stackoverflow.com/questions/36200096/cache-randomly-removing-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!