Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zystudios opened this issue Sep 29, 2023 · 3 comments

Comments

@zystudios
Copy link

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 zystudios changed the title key_prefix can support func ? : Expected type 'str', got '() -> str' instead #208 key_prefix can support func ? : Expected type 'str', got '() -> str' instead Sep 29, 2023
@zystudios
Copy link
Author

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

@dbascoules
Copy link
Contributor

dbascoules commented Jan 12, 2024

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

@zystudios
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants