django如何跟redis交互

django跟redis交互的示例:

在django项目同名文件夹下的setting中配置相关参数:

setting.py


CACHES = {

'default': {

'BACKEND': 'django_redis.cache.RedisCache',# 缓存使用redis数据库储存

'LOCATION': 'redis://127.0.0.1:6379/5',# 使用本地的6379端口(redis的默认端口)第五个数据库(redis共有16个数据库0-15)

"OPTIONS": {

"CLIENT_CLASS": "django_redis.client.DefaultClient",# 使用django_redis的默认参数

},

},

}


配置完成后,在view文件中连接redis,例如:

from django_redis import get_redis_connection


conn = get_redis_connection('default') # 建立连接 default为设置的连接名

a = redis_con.hget('school', 'class') # hget hash取值

Both comments and pings are currently closed.

Comments are closed.

Powered by KingAbc | 粤ICP备16106647号-2 | Loading Time‌ 1.729