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

Refactor cache stores #304

Merged
merged 2 commits into from
Oct 19, 2019
Merged

Refactor cache stores #304

merged 2 commits into from
Oct 19, 2019

Conversation

danschultzer
Copy link
Collaborator

@danschultzer danschultzer commented Oct 19, 2019

While working on #287 I came to realize that the credentials cache shouldn't override the user key with a list of sessions, but instead should add another key that can be looked up with the namespace. There is potential race condition issue when overriding the key, and it may only happen when updating the user cache key (since we never guarantee that the user is the most recent updated, but do have to guarantee that all sessions are listed).

This can still be done with a binary keys with namespace, but to improve efficiency I've moved over to Erlang terms for keys for namespace so match spec can be used. In Pow either a binary key or a list consisting of binary, integer or atom is used as key and will always in the cache backend be wrapped in a list with a prepending namespace.

Pow.Store.CredentialsCache now inserts three keys at once, which in ETS and Mnesia is guaranteed to be atomic:

{session_id, {[user_struct, :user, user_id], metadata}}
{[user_struct, :user, user_id}, user}
{{user_struct, :user, user_id, :session, session_id}, inserted_at}

Instead of the previous:

{session_id, {"USER_STUCT_sessions_USER_ID", metadata}}
{"USER_STUCT_sessions_USER_ID", %{user: user, session: [session_id, ...]}}

I've also refactored the code so the cache store and cache backend has separate behaviours.

The Redis guide has be updated, and I've added a bunch of backwards compatibility logic since there are many who uses Redis with binary key in production at the moment.

Any extra 👀 or hands to test this out would be much appreciated!

Resolves #302

@danschultzer danschultzer force-pushed the refactor-credentials-cache branch 3 times, most recently from 77f650e to 3aeb6d8 Compare October 19, 2019 02:40
@danschultzer danschultzer force-pushed the refactor-credentials-cache branch 4 times, most recently from 02d3e62 to 27d6b76 Compare October 19, 2019 18:08
@danschultzer danschultzer merged commit c29269d into master Oct 19, 2019
@danschultzer danschultzer deleted the refactor-credentials-cache branch October 19, 2019 21:03
This was referenced Oct 30, 2019
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.

Unhandled pattern matching in delete_from_session_list/4
2 participants