question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

invalidate cache by key

See original GitHub issue

What did I do:

@cache.invalidate("foo:client_id:{client_id}", args_map={"client_id": "client_id"})
async def bar(self, client_id):
    ...

What do I expect:

It works ! =)

What do I receive:

File ".../python3.9/site-packages/cashews/validation.py", line 40, in _wrap
    backend.delete_match(target.format({k: str(v) if v is not None else "" for k, v in _args.items()}))
KeyError: 'client_id'

What do I suggest:

backend.delete_match(target.format(**{k: str(v) if v is not None else "" for k, v in _args.items()}))

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
AIGeneratedUsernamecommented, Nov 21, 2022

@MasterGroosha Decorating is overkill in your example. Just call delete_match.

KEY_PREFIX: str = "simple"

@app.get("/invalidate/{number}")
async def invalidate_cache(number: int):
    await cache.delete_match(f"{KEY_PREFIX}:{number}")

If you still really want to decorate something, then perhaps read https://github.com/tiangolo/fastapi/issues/2662 and https://github.com/tiangolo/fastapi/issues/1743

Your question rather belongs to discussions, than to issues.

0reactions
Krukovcommented, Nov 21, 2022

@MasterGroosha Thanks for reporting

Yes the order order of decoration matter - and it is not an issue of cashews. It is related how decorators works, and how frameworks registers handler functions.

About args_map - in you case you should reverse you map:

@cache.invalidate("simple:{num}", args_map={"num": "number"})

But more right way is just use right variable in key

@cache.invalidate("simple:{number}")

args_map - is a solution when you don’t want to use the key directly

@cache(ttl="9999d")
async def simple_cached_function(num: int) -> int:
    return num
    
....
@app.get("/invalidate/{number}")
@cache.invalidate(simple_cached_function, args_map={"num": "number"})
async def invalidate_cache(number: int):
    return {"message": f"Invalidated {number}"}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cache made consistent: Meta's cache invalidation solution
Cache invalidation describes the process of actively invalidating stale cache entries when data in the source of truth mutates. If a cache ......
Read more >
When and How to Invalidate Cache - Lu's blog
In this post, I will talk about one way to figure out when to invalidate cache entries. I will use a specific setup...
Read more >
Redis: Cache Invalidation Done Better | Ackee blog
How we reduced Redis usage during cache invalidation? ... We have all keys in one Redis database, using the GET/SET commands as expected....
Read more >
InvalidateCache policy | Apigee Edge
Specifies how to construct a cache key when a Prefix element value is not specified, or to clear cache entries added by another...
Read more >
Invalidating several grouped cache keys - Stack Overflow
Use a common prefix for all cache keys which are based on TicketType. Then invalidate all cache keys in a post save signal...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found