当前位置: 首页 >服务端 > django-redis-cache缓存使用

django-redis-cache缓存使用

1. redis安装配置

(1)到redis目录[root@localhost redis-2.8.17]# ls00-RELEASENOTES  CONTRIBUTING  deps MakefileREADME  runtestsentinel.conf  testsBUGS COPYINGINSTALL  MANIFESTO  redis.conf  runtest-sentinel  srcutils(2) redis 源码包安装 	make(3)修改环境变量vim /etc/profile添加以下一行:export PATH=/qqc_pack/redis-2.8.17/src:$PATH(4)生效配置source /etc/profile(5)启动服务端:redis-server &客户端:redis-cli指定配置启动:redis-server /qqc_pack/redis-2.8.17/redis.conf(6)查看进程:[root@localhost ~]# ps -aux|grep redisroot 21692  0.1  0.4 140812  7876 ?Sl18:290:30 redis-server 0.0.0.0:6379root 21869  0.0  0.2  20200  5192 pts/1S+18:480:00 redis-cliroot 22139  0.0  0.0 112724992 pts/0R+23:340:00 grep --color=auto redis(7) 修改密码,开放host[root@localhost redis-2.8.17]# vi redis.confbind 0.0.0.0# bind 127.0.0.1# requirepass foobaredrequirepass qqcqqc(8) 登录127.0.0.1:6379> auth qqcqqcOK

2.django中配置,连接redis服务

1、setting中配置:# redis配置CACHES = {"default": {"BACKEND": "django_redis.cache.RedisCache","LOCATION": "redis://172.29.32.104:6379/0","OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient","CONNECTION_POOL_KWARGS": {"max_connections": 100},"PASSWORD": "qqcqqc",}}}2、views中使用:from django_redis import get_redis_connectiondef resdis_test(request):conn = get_redis_connection('default')all=conn.get("age")data={"age":all}print(type(data))retu JsonResponse(data=data, safe=False)3、cache命令操作:到manage.py目录[root@localhost test_pro]# python3 manage.py shellPython 3.6.4 (default, Nov 25 2019, 21:07:27) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linuxType "help", "copyright", "credits" or "license" for more information.(InteractiveConsole)>>> from django.core.cache import cache>>> cache.get("name")>>> cache.get("103"){'password': '123456', 'mobile': '22222'}4、在redis 中查看127.0.0.1:6379> keys *1) "name"2) ":1:103"3) "age"127.0.0.1:6379> get ":1:103""\x80\x04\x95*\x00\x00\x00\x00\x00\x00\x00}\x94(\x8c\bpassword\x94\x8c\x06123456\x94\x8c\x06mobile\x94\x8c\x0522222\x94u."5.业务场景中使用def tset_user_cz(request):user_id = request.GET["user_id"]# User_info.objects.create(user_id=user_id, name="name", password="123456", remark="庐州", mobile="22222")info = get_user_cache(user_id)if not info:values = User_info.objects.filter(user_id=user_id).values_list("password", "mobile")data = {"password": values[0][0], "mobile": values[0][1]}create_user_cache(user_id, data)retu JsonResponse(data=data, safe=False)retu JsonResponse(data=info, safe=False)6.缓存方法from django.core.cache import cachedef create_user_cache(user_id, value):cache.set(user_id, value, timeout=300) # 默认过期时间5分钟def get_user_cache(user_id):data = cache.get(user_id)if not data:"""查数据库"""passretu datadef delete_user_cache(user_id):cache.delete(user_id)

作者:朝朝哥
来源链接:https://www.cnblogs.com/quqinchao/p/11944510.html

版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaclub.cn/server/113352.html

标签:Cache
分享给朋友:

“django-redis-cache缓存使用” 的相关文章