一.通过django里设置settings文件连接redis
#1.settings添加 CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://192.168.8.102:6379/0",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {"max_connections": 100},
# "PASSWORD": "密码", }
},
"session": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://192.168.8.102:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
},
"sms_code": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://192.168.8.102:6379/15",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
} # 保存 session数据到 Redis中
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "session" 2.导入get_redis_connection连接
from django_redis import get_redis_connection
conn = get_redis_connection("default")
二.通过redis模块
import redis pool = redis.ConnectionPool(host='192.168.8.102', port=6379,password='密码',db="库",)
con = redis.Redis(connection_pool=pool)