Django runserver and memcached: variable in cache gets lost - django

In a Django website I use memcached to save a variable in cache.
After some minutes, if I refresh the page, the variable is lost.
I am in develop mode using runserver.
Any ideas?

Cache keys are expired after some time. If you don't explicitly pass timeout to cache.set, Django will use defaul, which it retrieves from settings. If you didn't specify default timeout yourself, Django will use its own default.

Related

How to access local & session storage variable from HTML to Django and how to get in my app views.py?

Django version is - 1.10.5 Python 2.7.10
I'm beginner in Django. Assistance required.
What i have done so far:-
1) I have already passing successfully local or session variables between HTML pages.
My Questions are:-
1) Do i need to specify anything in HTML or Django to access local or session storage variables?
2) How do i get that local or session variables in my views.py?
3) Without models can i do this? because i don't want anything to store in database.
Thanks in Advance
LocalStorage is client storage in your browser. Your *.py files will be executed in server. So you can not access them directly. You can save them as cookie, or put them to server via ajax request.

Django Session with Memcached or ElastiCache, does the Django code need to be changed?

I have an app using DB as session backend, and I realize that Django allows a Memcached-like (memcached, ElastiCache) session backend.
Reading the doc at Django site, I found the setting file can specify that change. My question is whether my view logic code files need any change.
No. You shouldn't have to change anything. If you deploy the code on a live site, active sessions will be lost (all users will be logged out).

django flush query caches

I have 2 instances of django application.
One is frontend - a normal wsgi app.
Another is backend - a twisted daemon running with ./manage.py rundaemon.
They share django settigns and models.
Now, when one of them has a query, it got cached.
And when another updates database - the cache will not be flushed.
That's obviously because they have no clue about another instance accessing the same database.
Is there a way to disable caching, or flush it manually and force query to be reexecuted?
(I guess the admin app does flush query caches somehow)
I'm not sure if this is the best solution, but it worked for me when I faced the same problem.
import django
django.db.connection.close()
The connection will automatically get reopened the next time it is needed.

Django - Persistant cache

Is there a way I can set a cache key indefinitely using the default django cache framework? I tried setting the timeout to 0, but that doesn't set the key at all unfortunately.
Django 1.6 now accepts None for the timeout argument to specify forever.
https://docs.djangoproject.com/en/dev/topics/cache/#basic-usage
Someone recently mentioned Johnny Cache in an unrelated answer. It provides a infinite caching locmem and memcached backend when a timeout of 0 is provided.

session issue with django+apache+mod_wsgi

I've written a django application, and put it on a CentOS server. It is definitely okay when I use django development web server.
Such as I start it by "python ./manage.py runserver", and access that server from browser on another computer. I can sign in one time, and access all the pages without issues.
However when I run it with apache+mod_wsgi, I just found I have to login with user and password time by time. I think maybe there is some problem with the session middleware, so, how can I find the root cause and fix it?
There are a couple of different options for this.
In order of likelyhood (imho):
The session backend uses the cache system to store the sessions and you're using the locmem cache backend
The session backend isn't storing the cookies (secure cookies enabled? cookie timeouts? incorrect date on the server?)
The session middleware might not be loaded (custom settings for production server?)
Storing the session in the cache is only a good solution if you use memcached as the cache backend. So if you're storing the sessions in cache, make sure you use memcache :)
Either way, check if SESSION_ENGINE is set to django.contrib.sessions.backends.db