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

Multi level caching suggestion? #168

Closed
aphillipo opened this issue Oct 4, 2022 · 4 comments
Closed

Multi level caching suggestion? #168

aphillipo opened this issue Oct 4, 2022 · 4 comments

Comments

@aphillipo
Copy link

Hi loving Nebulex it's really fantastic!

@decorate cacheable(cache: Cache, key: {User, sub}, mapped_to: fn user -> {User, user.id} end, opts: [ttl: @ttl])
def get_user_by_sub!(sub),
    do: Repo.get_by!(User, %{sub: sub})

This would mean instead of adding an entry to the cache for that specific {User, sub} it became multi layered and it would store in the main cache {User, id}, updating it there should this be needed and also adding a reference to this value from {User, sub}. This means that we can now just delete {User, id} from the cache with cache_evict and not have to track each type of lookup.

Taking the following as a further example of how mapped_to could be useful we could do the following:

@decorate cacheable(
    cache: Cache, 
    key: {User, ids}, # something clever here maybe?
    mapped_to: fn users -> 
        for u <- users, do: {User, u.id} # mapped to can be an array entry for each item in ids.
    end, 
    opts: [ttl: @ttl]
)
def get_users_by_ids(ids),
    do: from(users in User, where: users.id in ^ids) |> Repo.all()

And I think there is (nearly) a enough info for the above to hit the database only for the values not in the mapped cache list already?

Probably this is a bit complex and I shouldn't worry, just would have been nice with what I'm building to know I wasn't hitting the DB rather than the cache to update the leaderboard as participants enter and leave a classroom. Maybe there is already a way to do this in Nebulex I would not be surprised!

WDYT?

@cabol
Copy link
Owner

cabol commented Oct 7, 2022

Hey 👋 !! That's actually a very good and interesting suggestion, it definitely would be very useful, I think it really worth exploring it. Let me try to come up with a design for this, but also feel free to open a PR or put your thoughts/ideas here. Thanks for the suggestion, it is pretty good! Will try to give you feedback soon!

@aphillipo
Copy link
Author

Yes if I get a chance next week I'll have a look at the source code and have a play...

@cabol
Copy link
Owner

cabol commented Oct 10, 2022

I've created a separate issue describing the feature request, please check it out and let me know if I miss something (#169), if it is ok, perhaps we can close this one. BTW, I opened a new branch with the solution soon so you can try it out: #176. Thanks!

@cabol
Copy link
Owner

cabol commented Jan 29, 2023

Closing this issue since I haven't received feedback for a long time ago. Feel free to reopen it or create a new one if the implemented feature (#169) doesn't solve it.

@cabol cabol closed this as completed Jan 29, 2023
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