[英]Django per-view caching to database
I was learning how to implement django cache framework in my simple web application. 我正在学习如何在我的简单Web应用程序中实现django缓存框架。
I've used postgresql database and i've created my_cache_table. 我使用了postgresql数据库,并创建了my_cache_table。 I tried using "per-view cache" technique but it doesn't work.
我尝试使用“按视图缓存”技术,但是它不起作用。 No database entries at my_cache_table have been created.
在my_cache_table上没有创建数据库条目。
#settings.py
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
# Database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
}
}
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'my_cache_table',
'TIMEOUT': 3600,
'OPTIONS': {
'MAX_ENTRIES': 1000
}
}
}
#urls.py
urlpatterns = [
url(r'^$', views.search_form),
url(r'^s', cache_page(60 * 60)(views.search)),
]
#views.py
from .scraper import scrape
from django.views.decorators.cache import cache_page
def search_form(request):
return render(request, 'scraper/search_form.html')
def search(request):
q = request.GET["k"]
ftitles, fprices, furls = scrape(q)
context = {'ftitles': ftitles, 'fprices': fprices , 'furls': furls , 'q': q}
return render(request, 'scraper/output.html', context)
This is how my web app works - 这就是我的网络应用程序的工作方式-
1.User enters keyword to search 1.用户输入关键词进行搜索
2.Keyword is sent to the script "scrape.py" and processed and output is rendered and displayed in a page output.html. 2.将关键字发送到脚本“ scrape.py”并进行处理,然后将输出呈现并显示在页面output.html中。
My QUESTION - 我的问题 -
It would be great if i could cache the search view response so that a hefty process that happens in the search view can be avoided. 如果我可以缓存搜索视图响应,这样可以避免搜索视图中发生的繁琐过程,那将是很好的。
So when user searches for the same keyword that has been cached, the response can be displayed from cache avoiding the process. 因此,当用户搜索已缓存的相同关键字时,可以从缓存中显示响应,从而避免了该过程。
I tried using per-view caching in urls but it doesn't work 我尝试在网址中使用按视图缓存,但是它不起作用
Please point out what am i missing here . 请指出我在这里想念的是什么。
Any help is appreciated. 任何帮助表示赞赏。
Found the solution, i'm posting it coz it might help someone. 找到了解决方案,我将其发布,因为它可能会帮助某人。
The problem is i forgot to import cache decorators in url.py file I wonder why runserver didn't throw me an error before. 问题是我忘了在url.py文件中导入缓存装饰器,我不知道为什么runserver之前没有向我抛出错误。 Well today it did throw me the error.
好吧,今天的确向我抛出了错误。
NameError: name 'cache_page' is not defined
After updating my urls.py per-view caching worked super fine . 更新我的urls.py后,每个视图缓存都可以正常工作。
from django.views.decorators.cache import cache_page
Something i learnt - Restart your cmd and then restart your server. 我学到的东西-重新启动cmd,然后重新启动服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.