使用Django设置Redis缓存

时间:2015-03-21 16:38:02

标签: python django caching redis

我在ubuntu EC2节点上有一个django项目,它执行计算密集型的长时间运行过程,通常需要60秒。我需要缓存结果。以前从未使用过缓存,我正在关注http://michal.karzynski.pl/blog/2013/07/14/using-redis-as-django-session-store-and-cache-backend/以使用redis。在文章中,作者引用了https://docs.djangoproject.com/en/1.7/topics/cache/,其中包含以下内容:

  given a URL, try finding that page in the cache
  if the page is in the cache:
  return the cached page
else:
  generate the page
  save the generated page in the cache (for next time)
  return the generated page

这是我需要在django视图中实现的基本逻辑。但是,虽然我已经阅读了其余的缓存文档,但我仍然不清楚如何将这个逻辑编码到我的django视图中。

我当前的django视图包含;

from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def index(request):

# long running process started on request. Resulting html page should be cached

    return HttpResponse(html)

如何将上述逻辑编码到我的视图中?

0 个答案:

没有答案
相关问题