Created
July 28, 2016 08:07
-
-
Save stuartbates/d26f338775c5b32424eaa2bfc34105b4 to your computer and use it in GitHub Desktop.
Ideas for caching collection counts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Hexdigest the params hash down to a string representation | |
params_digest = Digest::SHA1.hexdigest(params.to_s) | |
# Use the key in combination with namespace to create cache key | |
cache_key = "collection_counts.#{params_digest}" | |
# Cache the result using Redis.cache.fetch | |
# Set a sane default expiration period | |
Rails.cache.fetch(cache_key, expires_in: 12.hours) { scope.size } | |
# Later on we can use Redis as the cache store |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment