开发者

Django Memcached - how to check if memcached is used?

开发者 https://www.devze.com 2023-04-12 12:49 出处:网络
I decided to accelerate my Django application by using Memcached, but I am not sure that it is really working, even if I don’t see any error message.

I decided to accelerate my Django application by using Memcached, but I am not sure that it is really working, even if I don’t see any error message. I googled, of course, for an answer, but nothing helped...

My main question is “how can I check whether Memcached is used or not ?“

Here is what I have:

Django 1.3 with PostgreSQL. Memcached server for windows (1.4.5) Memcached client: python开发者_JS百科-memcached (latest, I think 1.4.7)

I configured the middleware with :

MIDDLEWARE_CLASSES = (
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',)

And the cache with:

CACHES = {
'default': {
    'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
    'LOCATION': '127.0.0.1:11211',
} }

Then I launch memcached.exe. I can access it with the command “telnet localhost 11211”.

So everything seems working fine: When I run the “stats” command I can see that “GET” requests are seen by memcached.

But when I retrieve data:

Object.objects.get(ObjectId=1)

I can see that the table “pg_stat_activity” changes on each request. So I guess that the query is redirected to the database, while it should not…

An other test: I tried to add an entry to a table with a command line. As far as I understood Memcached, the entry should not be added to the real database, but only into the cache, is that right ? The fact is that when I add an entry, it is added to the database…

Does somebody know how I can check whether Memcached is really used or not ?

Thanks in advance, S.


Why would you think that command would use the cache? It's for fetching data from the database, so that's what it does. Django doesn't try and use the cache instead of the database, that would be weird.

The correct way to cache db lookups is to ask the cache for the item, and if it's not there, get it from the db and cache it:

object = cache.get(key)
if not object:
    object = Object.objects.get(id=key)
    cache.set(key, object, timeout)
return object
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号