django+SQLite搭建轻量级个人博客(十三)连接redis的配置和操作

1、setting.py

CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://ip:port/0",
        "OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {"max_connections": 100, 'decode_responses': True},
"PASSWORD": "12345678", # 密码
}
},
  
  #可设置多个redis。。。。。。
"redis2": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://ip:port/0",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {"max_connections": 100, 'decode_responses': True},
"PASSWORD": "12345678", # 密码
}
}
}

2、连接redis

import django_redis
redis = django_redis.get_redis_connection() #默认使用default,setting里面设置
# redis = django_redis.get_redis_connection('redis2') #使用设置里面其他的redis数据库
redis.set(token,pickle.dumps(user),const.token_expire) #pickle模块将对象转换成字符串序列,token自己定义此处省略

猜你喜欢

转载自www.cnblogs.com/xuexizongjie/p/11817193.html