flask-caching icon indicating copy to clipboard operation
flask-caching copied to clipboard

key_prefix can support func ? : Expected type 'str', got '() -> str' instead

Open zystudios opened this issue 2 years ago • 3 comments

def key_str():
    key = flask.request.path
    return key

@app.route('/get', methods=['get'])
@cache.cached(timeout=300, key_prefix=key_str)

image

when I use

@cache.cached(timeout=300, key_prefix=key_str())
image

zystudios avatar Sep 29 '23 09:09 zystudios

  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()

zystudios avatar Sep 29 '23 11:09 zystudios

Hi @zystudios,

You expose 2 problems in your issue :

  1. the main one : "key_prefix can support func" -> could be resolved by https://github.com/pallets-eco/cachelib/pull/332.
  2. "Working outside of request context" (your last screenshot) -> you should perhaps create another issue and describe a little more your application context.

David

dbascoules avatar Jan 12 '24 07:01 dbascoules

Hi @zystudios,

You expose 2 problems in your issue :

  1. the main one : "key_prefix can support func" -> could be resolved by Redis(Cache) now (re)supports function as a key_prefix cachelib#332.
  2. "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

zystudios avatar Jan 16 '24 02:01 zystudios