我正在尝试在我的python代码中使用redis缓存,下面的代码工作正常,并且可以完美地设置密钥。我想在无法连接Redis或端口未打开时设置超时。 不幸的是,我找不到任何有关如何将超时传递给连接参数的文档。
以下是我的代码。
from flask import Flask, render_template
from flask_caching import Cache
app = Flask(__name__, static_url_path='/static')
config = {
"DEBUG": True,
"CACHE_TYPE": "redis",
"CACHE_DEFAULT_TIMEOUT": 300,
"CACHE_KEY_PREFIX": "inventory",
"CACHE_REDIS_HOST": "localhost",
"CACHE_REDIS_PORT": "6379",
"CACHE_REDIS_URL": 'redis://localhost:6379'
}
cache = Cache(app, config=config)
socket_timeout = 5
@app.route('/')
@cache.memoize()
def dev():
# some code
return render_template("index.html", data=json_data, columns=columns)
当它无法连接时,它会等待很长时间并引发以下错误:
Traceback (most recent call last):
File "/Users/amjad/.virtualenvs/inventory/lib/python3.7/site-packages/flask_caching/__init__.py", line 771, in decorated_function
f, *args, **kwargs
File "/Users/amjad/.virtualenvs/inventory/lib/python3.7/site-packages/flask_caching/__init__.py", line 565, in make_cache_key
f, args=args, timeout=_timeout, forced_update=forced_update
File "/Users/amjad/.virtualenvs/inventory/lib/python3.7/site-packages/flask_caching/__init__.py", line 524, in _memoize_version
version_data_list = list(self.cache.get_many(*fetch_keys))
File "/Users/amjad/.virtualenvs/inventory/lib/python3.7/site-packages/flask_caching/backends/rediscache.py", line 101, in get_many
return [self.load_object(x) for x in self._read_clients.mget(keys)]
File "/Users/amjad/.virtualenvs/inventory/lib/python3.7/site-packages/redis/client.py", line 1329, in mget
return self.execute_command('MGET', *args, **options)
File "/Users/amjad/.virtualenvs/inventory/lib/python3.7/site-packages/redis/client.py", line 772, in execute_command
connection = pool.get_connection(command_name, **options)
File "/Users/amjad/.virtualenvs/inventory/lib/python3.7/site-packages/redis/connection.py", line 994, in get_connection
connection.connect()
File "/Users/amjad/.virtualenvs/inventory/lib/python3.7/site-packages/redis/connection.py", line 497, in connect
raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 60 connecting to localhost:6379. Operation timed out.
谢谢。
答案 0 :(得分:2)
这个问题相当老,但是刚才遇到了这个确切的问题,我找到了解决方案。留给后代供以后的读者使用。
根据2.5.2 Time Types的文档,使用CACHE_TYPE
参数:
指定要使用的缓存对象类型。这是将被导入和实例化的导入字符串。假定导入对象是一个函数,它将返回遵循缓存API的缓存对象。
因此,对它们的redis
函数进行修改,就像在flask_caching.backends.cache
中找到的那样:
def redis_with_timeout(app, config, args, kwargs):
try:
from redis import from_url as redis_from_url
except ImportError:
raise RuntimeError("no redis module found")
# [... extra lines skipped for brevity ...]
# kwargs set here are passed through to the underlying Redis client
kwargs["socket_connect_timeout"] = 0.5
kwargs["socket_timeout"] = 0.5
return RedisCache(*args, **kwargs)
并使用它代替默认的redis
,如下所示:
CACHE_TYPE = 'path.to.redis_with_timeout'
该库将改为使用该库,并将自定义kwargs传递给基础Redis客户端。希望有帮助。
答案 1 :(得分:1)
在latest document中,有一个CACHE_OPTIONS
配置作为关键字参数传递给几乎每种类型的缓存后端:
CACHE_OPTIONS中的条目以** kwargs的形式传递给redis客户端
我们可以简单地传递其他设置,例如:
from flask import Flask
from flask_caching import Cache
app = Flask(__name__)
config = {
"CACHE_TYPE": "redis",
...
"CACHE_REDIS_HOST": "localhost",
"CACHE_REDIS_PORT": "6379",
"CACHE_REDIS_URL": 'redis://localhost:6379',
"CACHE_OPTIONS": {
"socket_connect_timeout": 5, # connection timeout in seconds
"socket_timeout": 5, # send/recv timeout in seconds
}
}
cache = Cache(app, config=config)