django中使用redis
第一种 自定义redis
1.创建utils文件夹,创建一个redis_pool.py文件,用导入的方式使用 import redis POOL = redis.ConnectionPool(host='1277.0.0.1',port=6397,password='chenchen',max_commections=100) 2.views.py from django.shortcuts import HttpResponse import redis from utils.redis_pool import POOL # Create your views here. def index(request): conn = redis.Redis(connection_pool=POOL) conn.hset('k1','age','18') return HttpResponse("设置成功") def order(request): conn = redis.Redis(connection_pool=POOL) conn.hget('k1', 'age',) return HttpResponse("获取成功")
第二种 使用第三方组件django-redis
setting.py中配置 #redis的配置 首先要pip3 install django-redis #两个redis,想连接那个连接那个conn = get_redis_connection("default") CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", "CONNECTION_POOL_KWARGS": {"max_connections": 100} # "PASSWORD": "密码", } }, "back": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", "CONNECTION_POOL_KWARGS": {"max_connections": 100} # "PASSWORD": "密码", } } }
views.py
from django.shortcuts import HttpResponse from django_redis import get_redis_connection import time # Create your views here. def index(request): conn = get_redis_connection("default") return HttpResponse("设置成功") def order(request): conn = get_redis_connection("back") return HttpResponse("获取成功")
第三种 全站缓存,利用中间件
使用中间件,经过一系列的认证等操作,如果内容在缓存中存在,则使用FetchFromCacheMiddleware获取内容并返回给用户,当返回给用户之前,判断缓存中是否已经存在,如果不存在则UpdateCacheMiddleware会将缓存保存至缓存,从而实现全站缓存
1.setting.py中配置 #redis的配置 首先要pip3 install django-redis CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", "CONNECTION_POOL_KWARGS": {"max_connections": 100} # "PASSWORD": "密码", } } } 2. 中间件配置
MIDDLEWARE = [
'django.middleware.cache.UpdateCacheMiddleware', #全站缓存
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware', #全站缓存
]
CACHE_MIDDLEWARE_ALIAS = ""
CACHE_MIDDLEWARE_SECONDS = "" #全站缓存的超时时间
CACHE_MIDDLEWARE_KEY_PREFIX = ""
views.py
from django.shortcuts import HttpResponse #不加缓存的话,时间每次都是变的,加缓存的话时间不变 import time # Create your views here. def index(request): ctime = str(time.time()) return HttpResponse(ctime) def order(request): ctime = str(time.time()) return HttpResponse(ctime)
第四种 单视图缓存
setting.py中配置 #redis的配置 首先要pip3 install django-redis CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", "CONNECTION_POOL_KWARGS": {"max_connections": 100} # "PASSWORD": "密码", } } }
views.py
from django.shortcuts import HttpResponse from django.views.decorators.cache import cache_page import time # Create your views here. #a.单视图缓存,15分钟的缓存,15分钟后消失 @cache_page(60*15) def index(request): ctime = str(time.time()) return HttpResponse(ctime) def order(request): ctime = str(time.time()) return HttpResponse(ctime) #b.局部缓存 比如购买商品,除了数量变化外,其他的如商品名称,描述,图片不变化,可以放到缓存中 #某个页面的某部分缓存
第五种 局部页面缓存
#b.局部缓存 比如购买商品,除了数量变化外,其他的如商品名称,描述,图片不变化,可以放到缓存中 #某个页面的某部分缓存
html文件
{% load cache %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h2>redis局部缓存</h2> <div>111111111111111</div> {##这里面的东西是静止不动的,可以用缓存#} 5000是秒,key001是redis中的key值 {% cache 5000 key001 %} <div></div> {% endcache %} </body> </html>