标签:pip password decode class rtc util family play soft
utils文件夹下,建立redis_pool.py
import redis POOL=redis.ConnectionPool(‘127.0.0.1‘,6379,max_connections=1000)
视图函数中使用:
from django.shortcuts import render, HttpResponse import redis from app01.redis_pool import POOL # 这是通用的方法 # 第一种方案: # 1.写一个模块,写一个pool,导入 def setname(request): conn = redis.Redis(connection_pool=POOL) conn.set(‘name‘, ‘曹蕊是好人‘) return HttpResponse(‘设置成功‘) def getname(request): conn = redis.Redis(connection_pool=POOL) name = conn.get(‘name‘).decode(‘utf-8‘) return HttpResponse(name)
安装django-redis模块
pip3 install django-redis
setting里配置:
# 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": "123", } } }
这样就可以把缓存放在redis里面了
视图函数:
from django_redis import get_redis_connection
def setname(request): conn = get_redis_connection() conn.set(‘name‘, ‘测试‘) return HttpResponse(‘设置成功‘) def getname(request): conn = get_redis_connection() name = conn.get(‘name‘).decode(‘utf-8‘) return HttpResponse(name) from django.views.decorators.cache import cache_page @cache_page(5) def test(request): import time ctime = time.time() return HttpResponse(str(ctime))
标签:pip password decode class rtc util family play soft
原文地址:https://www.cnblogs.com/cao123/p/10182741.html