Django中Redis的使用?

redis有0-15数据库,现在使用django缓存部分文件,如果分别使用多个不同的是数据库?
例如页面使用0数据库,图片使用1数据库。

阅读 1.7k
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_connection

conn=get_redis_connection('minimal')
print(conn.get('name'))
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题