flask-caching
flask-caching copied to clipboard
key_prefix can support func ? : Expected type 'str', got '() -> str' instead
def key_str():
key = flask.request.path
return key
@app.route('/get', methods=['get'])
@cache.cached(timeout=300, key_prefix=key_str)
when I use
@cache.cached(timeout=300, key_prefix=key_str())
def cached(
self,
timeout: Optional[int] = None,
key_prefix: str = "view/%s", # line 240 <------- The str type is declared here, should it be removed?
unless: Optional[Callable] = None,
# ..........
if callable(key_prefix): # line 481 callable means there could be a function, so, Is it inappropriate to declare a str type?
cache_key = key_prefix()
Hi @zystudios,
You expose 2 problems in your issue :
- the main one : "key_prefix can support func" -> could be resolved by https://github.com/pallets-eco/cachelib/pull/332.
- "Working outside of request context" (your last screenshot) -> you should perhaps create another issue and describe a little more your application context.
David
Hi @zystudios,
You expose 2 problems in your issue :
- the main one : "key_prefix can support func" -> could be resolved by Redis(Cache) now (re)supports function as a key_prefix cachelib#332.
- "Working outside of request context" (your last screenshot) -> you should perhaps create another issue and describe a little more your application context.
David
Thanks very much