如何将Redis用作Django项目的缓存服务器?

Mat*_*lla 3 django caching redis

用最简单的术语来说,如何在我的django项目中设置redis进行缓存?

我一直试图找出上周的缓存,但这些碎片并没有落实到位.

我需要做什么才能启动和运行缓存.只需要处理标题并安装redis?

各种教程和介绍似乎都是理所当然的基本步骤,这些步骤阻碍了我理解如何实现缓存.这不是无处不在吗?有人如何了解这一点?

jdi*_*jdi 6

好吧,也许您缺少的连接是,一旦你有redis运行,并且你有能力使用python http://github.com/andymccurdy/redis-py设置和获取它的价值你可以开始做这样的事情:

(伪代码保持简单)

client_request_for_data():
    check if redis has this data already under a specific "key"
       True:
          * GET value from redis using "key"
          * return it!
       False:
          * Do the normal process of building the data
          * SET it into redis with a unique "key", with maybe an EXPIRE time
          * return it!
Run Code Online (Sandbox Code Playgroud)

这里的想法是,您开始将现有代码包装在为客户端请求生成结果的位置,首先检查密钥是否存在,如果存在,则将其返回.