Reputation: 862
I am trying to save a value "name2" into memcache from Django and getting...interesting results.
I am using Django 1.5.4., pythn 2.7.3.
My current setup:
In my django.settings file:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '123.123.123.123:11211',
}
}
import memcache
def heavy_view2():
s = memcache.Client(['123.123.123.123:11211'])
# s.set("name", "david");
return s.get("name1")
so for everything standard, when I go to my server and set memcache value "name1", I can pull it down from my machine!
Now using Django:
from django.core.cache import cache
def heavy_view():
cache_key = 'name'
result = cache.get(cache_key)
return result
>>>> print(heavy_view())
>>>> None
Which is very odd! Especially because I set "name" server side! But wait! When I do:
from django.core.cache import cache
def heavy_view():
cache_key = 'name1'
cache_time = 100 # time to live in seconds
if not result:# some calculations here
print('set name1')
result = 'tbone'
cache.set(cache_key, result, 100)
return result
>>>> heavy_view()
>>>> 'set name1'
>>>> r = heavy_view()
>>>> 'tbone'
But when I go back to my server and request "name1", I get None.
Subsequent calls using heavy_view() return my "cached" result.
BUT if I restart the server memcache using: sudo service memcache restart, the results are LOST.
Despite all this, looking for "name1" on the SERVER always return None.
Does django run it's own memcache daemon? I am very confused by where my result is going!
Upvotes: 0
Views: 455
Reputation: 862
use the following from: How to export all keys and values from memcached with python-memcache?
and realize that Django is adding ":1:" + key name.
import telnetlib
def get_all_memcached_keys(host='127.0.0.1', port=11211):
t = telnetlib.Telnet(host, port)
t.write('stats items STAT items:0:number 0 END\n')
items = t.read_until('END').split('\r\n')
keys = set()
for item in items:
parts = item.split(':')
if not len(parts) >= 3:
continue
slab = parts[1]
t.write('stats cachedump {} 200000 ITEM views.decorators.cache.cache_header..cc7d9 [6 b; 1256056128 s] END\n'.format(slab))
cachelines = t.read_until('END').split('\r\n')
for line in cachelines:
parts = line.split(' ')
if not len(parts) >= 3:
continue
keys.add(parts[1])
t.close()
return keys
Upvotes: 1