Django中配置redis
django-redis
·
1.首先安装相关库:
pip install django-redis
2.在Django项目文件settings.py中配置缓存信息
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": 1000,
"encoding": 'utf-8'
},
# "PASSWORD": "foobared" # redis密码
}
}
}
# 测试django-redis
def get_redis_test(request):
# 创建redis连接对象
conn = get_redis_connection()
# 设置键值对并设置过期时间
conn.set('drcasdas', '党瑞聪', ex = 60)
val = conn.get('drcasdas')
print(val)
return HttpResponse("OK")
创建连接对象函数源码
def get_redis_connection(alias="default", write=True):
"""
Helper used for obtaining a raw redis client.
"""
from django.core.cache import caches
cache = caches[alias]
if not hasattr(cache, "client"):
raise NotImplementedError("This backend does not support this feature")
if not hasattr(cache.client, "get_client"):
raise NotImplementedError("This backend does not support this feature")
return cache.client.get_client(write)
3.搭建路由信息进行简单测试即可
更多推荐
所有评论(0)