django-redis无效的DB索引错误和redis url的含义

时间:2018-03-28 14:36:26

标签: python django redis

我正在使用django-redis:https://niwinz.github.io/django-redis/latest/

在设置和测试缓存时,我有一个问题。

从此:https://niwinz.github.io/django-redis/latest/#_installation

有这样的代码:

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    }
}

当我将此LOCATION代码中的任何一个代码改为:"LOCATION": "redis://127.0.0.1:6379/1"时,它表现良好。甚至"LOCATION": "redis://127.0.0.1:6379/2"它似乎运作良好。

但如果我改为"LOCATION": "redis://127.0.0.1:6379/220"

返回invalid DB index error

问题:

  1. 为什么会这样?

  2. 参数对"LOCATION": "redis://127.0.0.1:6379/parameter"的意义是什么?

  3. 这是追溯:

    Environment:
    
    
    Request Method: GET
    Request URL: http://127.0.0.1:8000/test/
    
    Django Version: 2.0.2
    Python Version: 3.6.3
    Installed Applications:
    ['django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'post',
     'website']
    Installed Middleware:
    ['django.middleware.security.SecurityMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware',
     'django.middleware.cache.UpdateCacheMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.cache.FetchFromCacheMiddleware']
    
    
    
    Traceback:
    
    File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\django_redis\cache.py" in _decorator
      32.             return method(self, *args, **kwargs)
    
    File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\django_redis\cache.py" in get
      81.                                    client=client)
    
    File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\django_redis\client\default.py" in get
      204.             raise ConnectionInterrupted(connection=client, parent=e)
    
    During handling of the above exception (Redis ResponseError: invalid DB index), another exception occurred:
    
    File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\django\core\handlers\exception.py" in inner
      35.             response = get_response(request)
    
    File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\django\utils\deprecation.py" in __call__
      93.             response = self.process_request(request)
    
    File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\django\middleware\cache.py" in process_request
      133.         cache_key = get_cache_key(request, self.key_prefix, 'GET', cache=self.cache)
    
    File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\django\utils\cache.py" in get_cache_key
      360.     headerlist = cache.get(cache_key)
    
    File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\django_redis\cache.py" in _decorator
      39.             raise e.parent
    
    File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\django_redis\client\default.py" in get
      202.             value = client.get(key)
    
    File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\redis\client.py" in get
      976.         return self.execute_command('GET', name)
    
    File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\redis\client.py" in execute_command
      667.             connection.send_command(*args)
    
    File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\redis\connection.py" in send_command
      610.         self.send_packed_command(self.pack_command(*args))
    
    File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\redis\connection.py" in send_packed_command
      585.             self.connect()
    
    File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\redis\connection.py" in connect
      493.             self.on_connect()
    
    File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\redis\connection.py" in on_connect
      567.             if nativestr(self.read_response()) != 'OK':
    
    File "D:\pythonDev\interpreters\forMultichat\lib\site-packages\redis\connection.py" in read_response
      629.             raise response
    
    Exception Type: ResponseError at /test/
    Exception Value: invalid DB index
    

1 个答案:

答案 0 :(得分:6)

默认情况下,Redis服务器配置有16(0..15)个共享数据库。使用除数据库0以外的任何东西通常被认为是不好的做法。