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自己定义此处省略
    模板看起来是不是太过冰冷
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号