Skip to content

fix: .clear method generates namespace even if key is passed #529

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Mdaadm
Copy link

@Mdaadm Mdaadm commented Feb 21, 2025

Fixes #451

calling

FastAPICache.clear(key="value")

method generates

namespace = cls._prefix + (":" + namespace if namespace else "")

even if key is passed. It then goes to

cls._backend.clear(namespace, key)

where key is never used because of this if-else statement:

if namespace:
    lua = f"for i, name in ipairs(redis.call('KEYS', '{namespace}:*')) do redis.call('DEL', name); end"
    return await self.redis.eval(lua, numkeys=0)  # type: ignore[union-attr,no-any-return]
elif key:
    return await self.redis.delete(key)  # type: ignore[union-attr]
return 0

Same for inmemory backend realisation.

With proposed changes clearing cache by key will work correctly, without clearing whole namespace

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

Successfully merging this pull request may close these issues.

Can't clear Redis by key if a prefix has been set
1 participant