Tuesday, 15 January 2013

python - Django cache, missing keys -



python - Django cache, missing keys -

i trying create cache flow, on user request cache big dict of 870 records , should remain in cache time. when defined time pass on next request dict should updated in na cache memory.

so have created such function:

from django.core.cache import get_cache def update_values_mapping(): cache_en = get_cache('en') values_dict = get_values_dict() <- create request obtain dict values cache_en.set_many(values_dict, 120) # 120s testing cache_en.set('expire', datetime.datetime.now() + datetime.timedelta(seconds=120))

then in sec function seek values cache

from django.core.cache import get_cache def get_value_details(_id): cache = get_cache('en') details = cache.get(_id, {}) # values in cache has expire date should gone expire = cache.get('expire', none) if not details , expire , expire < datetime.now(): update_values_mapping() value = cache.get(_id, {}) homecoming details

during rendering view get_value_details() called many times obtain needed values.

the problem of values missing e.g. cache.get('b', {}) homecoming {} if value 'b' saved cache (and expire date not pass yet). missing values changing, 'a', 'b', other time 'c' etc.

i have been testing on locmemcache , dummycache far. illustration cache settings:

caches = { 'default': { 'backend': 'django.core.cache.backends.locmem.locmemcache', 'location': 'cache-default' }, 'en': { 'backend': 'django.core.cache.backends.locmem.locmemcache', 'location': 'cache-en' }, 'pl': { 'backend': 'django.core.cache.backends.locmem.locmemcache', 'location': 'cache-pl' } }

when playing in console of values disappearing cache after next phone call of update_values_mapping(), missing beginning.

does have clue ? or maybe how solve described flow in way ?

locmemcache - local memory cache. means it's local particular server process, , won't visible either in other processes or in console.

if need shared across processes, should utilize proper cache backend memcached or redis.

python django caching

No comments:

Post a Comment