Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stuartbates/d26f338775c5b32424eaa2bfc34105b4 to your computer and use it in GitHub Desktop.
Save stuartbates/d26f338775c5b32424eaa2bfc34105b4 to your computer and use it in GitHub Desktop.
Ideas for caching collection counts
# 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