第一次写关于stackoverflow的问题。我使用的是python 2.7.11和djnago 1.10.2。我创建了产品模型并将1000个产品保存在我的数据库中。(Postgrelsql)实际上,我使用了Django memcached,但它不起作用。
Following steps:- 1. Added settings.py in caches backends.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'product_cache_table',
}
}然后是“创建缓存表”。使用Django Memcached的数据库缓存:
Django可以将其缓存的数据存储在数据库中。如果你有一个快速的,索引良好的数据库server.To,使用一个数据库表作为你的缓存后端,这是最好的:
设置"BACKEND“为"django.core.cache.backends.memcached.MemcachedCache".将LOCATION设置为tablename,即数据库表的名称。这个名称可以是您想要的任何名称,只要它是一个尚未在数据库中使用的有效表名即可。
In this example, the cache table’s name is "product_cache_table:"
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'product_cache_table',
}
}然后是“创建缓存表”。在使用数据库缓存之前,您必须使用以下命令创建缓存表:
python manage.py createcachetable
Write business login. (product/views.py)
django.views.decorators.cache.cache_page()¶
def heavy_view(request):
cache_key = 'product'
cache_time = 18 # time to live in seconds
result = Product.objects.all() # some calculations here
for l in result:
a = cache.set(cache_key, l, cache_time)
c = cache.get(cache_key)
return HttpResponse(c)如果我点击url,它会将所有产品保存在product_cache_table中,但只会保存product_name。当我们获得这些数据时,我们只得到product_name,但我想要该表中的所有属性。如何使用Django Memcached缓存Api。Django Memcached中的工作流。我读过“双重化”
https://stackoverflow.com/questions/41460131
复制相似问题