Skip to content

Commit 78b98a0

Browse files
committed
Only cache the user_id when parsing the auth_hash
We don't want stale user models being pulled from the cache. So only save the `user_id`. We don't want to query the database twice. So create a local variable for the user, set it to nil, fetch from cache and only query if there was a cache-hit (thus user is still nil).
1 parent 2ea995c commit 78b98a0

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

lib/kracken/authenticator.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,18 @@ def self.user_with_credentials(email, password)
1616
def self.user_with_token(token)
1717
auth = Kracken::TokenAuthenticator.new.fetch(token)
1818

19-
Rails.cache.fetch("auth/#{token}/#{auth.etag}") do
20-
self.new(auth.body).to_app_user
21-
end
19+
# Don't want stale user models being pulled from the cache. So only
20+
# cache the `user_id`.
21+
#
22+
# Don't want to query the database twice. So create a local variable
23+
# for the user, set it to nil, fetch from cache and only query if there
24+
# was a cache-hit (thus user is still nil).
25+
user = nil
26+
user_id = Rails.cache.fetch("auth/#{token}/#{auth.etag}") {
27+
user = self.new(auth.body).to_app_user
28+
user.id
29+
}
30+
user ||= Kracken.config.user_class.find(user_id)
2231
end
2332

2433
def initialize(response)

0 commit comments

Comments
 (0)