Django Memcached - 如何检查是否使用了 memcached?
我决定使用 Memcached 来加速我的 Django 应用程序,但我不确定它是否真的有效,即使我没有看到任何错误消息。 当然,我用谷歌搜索了答案,但没有任何帮助...
我的主要问题是“如何检查是否使用了 Memcached?”
这是我所拥有的:
Django 1.3 with PostgreSQL。 适用于 Windows 的 Memcached 服务器 (1.4.5) Memcached 客户端: python-memcached (最新的,我认为是 1.4.7)
我使用以下配置中间件:
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',)
并使用以下配置缓存:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
} }
然后启动 memcached.exe。 我可以使用命令“telnet localhost 11211”访问它。
所以一切看起来都工作正常:当我运行“stats”命令时,我可以看到“GET”请求被 memcached 看到。
但是当我检索数据时:
Object.objects.get(ObjectId=1)
我可以看到表“pg_stat_activity”在每个请求上发生变化。所以我猜想查询被重定向到数据库,而它不应该......
另一个测试:我尝试使用命令行向表添加一个条目。据我了解Memcached,条目不应该添加到真正的数据库中,而只能添加到缓存中,对吗?事实是,当我添加一个条目时,它会添加到数据库中......
有人知道我如何检查 Memcached 是否真的被使用?
提前致谢, S。
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-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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您为什么认为该命令会使用缓存?它用于从数据库中获取数据,这就是它的作用。 Django 不会尝试使用缓存而不是数据库,这会很奇怪。
缓存数据库查找的正确方法是向缓存询问该项目,如果不存在,则从数据库获取并缓存它:
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: