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"