Django中Redis的使用?
redis有0-15数据库,现在使用django缓存部分文件,如果分别使用多个不同的是数据库?
例如页面使用0数据库,图片使用1数据库。
回答:
https://github.com/jazzband/d...
CACHES = { "default": {
"BACKEND": "django_redis.cache.RedisCache",
# The hostname in LOCATION is the primary (service / master) name
"LOCATION": "redis://service_name/db",
"OPTIONS": {
# While the default client will work, this will check you
# have configured things correctly, and also create a
# primary and replica pool for the service specified by
# LOCATION rather than requiring two URLs.
"CLIENT_CLASS": "django_redis.client.SentinelClient",
# Sentinels which are passed directly to redis Sentinel.
"SENTINELS": SENTINELS,
# kwargs for redis Sentinel (optional).
"SENTINEL_KWARGS": {},
# You can still override the connection pool (optional).
"CONNECTION_POOL_CLASS": "redis.sentinel.SentinelConnectionPool",
},
},
# A minimal example using the SentinelClient.
"minimal": {
"BACKEND": "django_redis.cache.RedisCache",
# The SentinelClient will use this location for both the primaries
# and replicas.
"LOCATION": "redis://minimal_service_name/db",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.SentinelClient",
"SENTINELS": SENTINELS,
},
},
# A minimal example using the DefaultClient.
"other": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": [
# The DefaultClient is [primary, replicas...], but with the
# SentinelConnectionPool it only requires one "is_master=0".
"redis://other_service_name/db?is_master=1",
"redis://other_service_name/db?is_master=0",
],
"OPTIONS": {"SENTINELS": SENTINELS},
},
# A minimal example only using only replicas in read only mode (and
# the DefaultClient).
"readonly": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://readonly_service_name/db?is_master=0",
"OPTIONS": {"SENTINELS": SENTINELS},
},
}
from django_redis import get_redis_connectionconn=get_redis_connection('minimal')
print(conn.get('name'))
以上是 Django中Redis的使用? 的全部内容, 来源链接: utcz.com/p/938767.html