124bit
124bit

Reputation: 506

Alternating settings at django runtime

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?

Upvotes: 0

Views: 95

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799230

Multiple Django processes are running, and each process has its own copy of CacheValue, one with the value in question, but most without. Use Django's cache framework instead.

Upvotes: 2

Related Questions