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

get cache key for decorated functions with object arguments #575

Closed
jmmaa opened this issue Jul 27, 2022 · 3 comments
Closed

get cache key for decorated functions with object arguments #575

jmmaa opened this issue Jul 27, 2022 · 3 comments

Comments

@jmmaa
Copy link

jmmaa commented Jul 27, 2022

How to get the cache key if I have a cached function like this?

@cached(ttl=30, serializer=PickleSerializer())
async def fetchrow_from_db(pool: asyncpg.Pool, id: int):

    record = await pool.fetchrow("SELECT * FROM my_table WHERE id = $1", id)
    return record


async def get_cache(pool: asyncpg.Pool, id: int):
    record = await fetchrow_from_db.cache.get(...) # how to get in here?
@Dreamsorcerer
Copy link
Member

What's the use case for this?

Why not just do await fetchrow_from_db(pool, id)? That'll obviously get the result from the cache.

@jmmaa
Copy link
Author

jmmaa commented Jan 3, 2023

What's the use case for this?

Why not just do await fetchrow_from_db(pool, id)? That'll obviously get the result from the cache.

Actually, I went into a deep rabbit hole in this one.

I really wanted a flexible way of invalidating cache in a simple, functional way in my discord bot. Turns out that invalidating cache in an atomic way is way too complex.

So yeah I just made my own cache class which is a singleton that I can reuse it in other parts of my bot

@Dreamsorcerer
Copy link
Member

Right, for invalidating the cache, #625 might be of interest to you.

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

No branches or pull requests

2 participants