0

I need to keep some settings in RAM. I created file "cache_values.py"

class CacheValue:
    pass

I want to use it as variable container for my needs.

I'm writing some settings to "CacheValue" on server start:

from cms.models.pagemodel import Page
from modifier.cache_values import CacheValue

def cache_start_values():
   CacheValue.page_publish_dates={}

   pages=Page.objects.all()
   for page in pages:
       CacheValue.page_publish_dates[page.pk]=page.last_publish_date

   CacheValue.last_publish_date=max(CacheValue.page_publish_dates.values())

Also, when I'm saving some model - I change these settings

CacheValue.page_publish_dates[self.pk]=self.last_publish_date
CacheValue.last_publish_date=max(CacheValue.page_publish_dates.values())

But when I want to use this settings in my templates, by adding them with context processor as variables - some magic begins.

from modifier.cache_values import CacheValue
def add_for_cache_info(request):
    context_extras = {}
    context_extras['page_publish_dates']=CacheValue.page_publish_dates
    context_extras['last_publish_date_all_pages']=CacheValue.last_publish_date
    if "current_page" in request.__dict__:
        context_extras['last_publish_date']=CacheValue.page_publish_dates[request.current_page.pk]   

    return context_extras 

In template

  {{ page_publish_dates }}
  {{ last_publish_date_all_pages }}

Values that I see in rendered template alternates between old(before model save) and new(after model save) every time I refersh a page. 0_0 0_0

If I save model(change value) second time - it will alterante between the oldest value and the new one.

Why?

4

1 回答 1

2

多个 Django 进程正在运行,并且每个进程都有自己的 副本CacheValue,其中一个包含有问题的值,但大多数没有。改用Django 的缓存框架

于 2013-08-30T00:12:20.400 回答