Skip to content

Instantly share code, notes, and snippets.

@BenMorganIO
Created August 22, 2014 19:57
Show Gist options
  • Save BenMorganIO/0820c95e2dc96fe2a38e to your computer and use it in GitHub Desktop.
Save BenMorganIO/0820c95e2dc96fe2a38e to your computer and use it in GitHub Desktop.
Fragment Caching
def key_for(namespace, *objects)
object_collection = []
objects.each do |object|
if object.respond_to? :pluck
object_ids = object.pluck(:id).join '-'
max_updated_at = object.pluck(:updated_at).max.to_s
elsif object.respond_to? :id
object_ids = object.id
max_updated_at = object.respond_to?(:updated) ? object.updated : object.updated_at
elsif object.respond_to? :map
object_ids = object.map(&:id).join '-'
max_updated_at = object.map(&:updated).max
elsif object.is_a? String
object_ids = object
max_updated_at = 'not found'
elsif object.nil?
fail 'error caching because the object is nil.'
else
fail 'error caching.'
end
object_collection << "#{object_ids}-#{max_updated_at}"
end
result = namespace
result << '/' + object_collection.join('/') unless object_collection.empty?
result.to_s.gsub(/(?:\+|:| )/, '-')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment