利用redis做django的数据缓存

参考:

https://django-redis-chs.readthedocs.io/zh_CN/latest/(django-redis 中文文档)
http://www.aaron-zhao.com/post/8/
https://www.cnblogs.com/fuhuixiang/p/4174896.html
https://github.com/zaxlct/python-django-learning/blob/master/Django%E4%BD%BF%E7%94%A8redis%E5%81%9A%E7%BC%93%E5%AD%98%E5%B1%82.md
https://www.jianshu.com/p/04ef84c3fe3b
https://blog.csdn.net/sinat_29699167/article/details/79699200
https://niwinz.github.io/django-redis/latest/
https://realpython.com/caching-in-django-with-redis/
https: // futeng.iteye.com / blog / 2071867
https://www.fkomm.cn/article/2019/2/21/68.html
https://blog.csdn.net/m0_37886429/article/details/82556935
http://www.111cn.net/phper/163882.htm
http://www.zhongruitech.com/241624006.html
https://tengxt.com/blog/9.html
http://www.voidcn.com/article/p-dpsddhnm-bpt.html
https://www.waitig.com/django%E5%88%86%E6%9E%90%E4%B9%8B%E4%BD%BF%E7%94%A8redis%E7%BC%93%E5%AD%98%E6%9C%8D%E5%8A%A1%E5%99%A8.html(还不错)

1.

settings

CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': 'redis://127.0.0.1:6379',
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"PASSWORD":"your password",
},
},
}

添加
MIDDLEWARE=[
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
]

2.

views.py

#cache的测试函数
def cache_test(request):
if request.GET.get("url"):
url=request.GET.get("url")
cache.set('url',url , 60*60)
if cache.has_key("url"):
return HttpResponse(cache.has_key("url"))

3.

urls.py

url(r’cache_test’,views.cache_test),

4.测试:

http://localhost:8000/cache_test?url=aaaaaa

第一次没有返回值,第二次页面返回 aaaaaa

查看redis-cli里 keys * 出现views的缓存

2019.4.15

发表评论

电子邮件地址不会被公开。 必填项已用*标注