Django实战016:django中使用redis详解( 二 )
为了方便学习 , 下面附上本文用到的源码:
>pip install django-redis# Redis缓存配置CACHES = {"default": {"BACKEND": "django_redis.cache.RedisCache","LOCATION": "redis://host:port/database","OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient","CONNECTION_POOL_KWARGS": {"max_connections": 100},# "PASSWORD": "密码",}}}# 将session的存储到Django缓存中SESSION_ENGINE = "django.contrib.sessions.backends.cache"SESSION_CACHE_ALIAS = "default"# 将session存储配置到redis中SESSION_ENGINE = 'redis_sessions.session'SESSION_REDIS_HOST = 'localhost'SESSION_REDIS_PORT = 6379SESSION_REDIS_DB = 2SESSION_REDIS_PASSWORD = ''SESSION_REDIS_PREFIX = 'session'-----------------------------------------------------------------------------import django_redisfrom django.http import HttpResponsedef SetHash(request):conn=django_redis.get_redis_connection('default')conn.hset('person', 'name', 'jack')return HttpResponse('200')// urls模块使用from django.conf.urls import include, urlfrom django.contrib import adminurlpatterns = [url(r'^admin/', include(admin.site.urls)), //直接定义路由url(r'', include('app.urls')),//引用应用中的路由]// app.urls子应用中使用urlfrom django.conf.urls import urlfrom app import viewsurlpatterns = [ url(r'^login/$', app .login), ]// path模块应用from django.urls import path,includeurlpatterns = [path('admin/', admin.site.urls),//直接定义路由path('', include('app.urls')),//引用应用中的路由]// app.urls子应用中使用pathfrom django.urls import pathfrom app import viewsurlpatterns = [ path('login/', app.login), ]>redis-cli.exe127.0.0.1:6379> auth 123456O127.0.0.1:6379> hget person name"jack"
- SpringBoot2.x入门到项目实战课程系列(第二章)
- Go 实战项目推荐:Gin 实现的工单系统
- HLS实战之Wireshark抓包分析
- SpringBoot2.x入门到项目实战课程系列(第五章)
- python3和Django3+admin搭建后台之埋坑路
- 多视图几何三维重建实战系列之R-MVSNet
- 机器学习实战:GNN(图神经网络)加速器的FPGA解决方案
- 为什么 Django 框架持续统治着 Python 开发?
- Django实现支付宝沙箱操作,太强了呀
- 国产之光——蔚来EP9