Slide 22
Slide 22 text
Fragment caching
# app/controllers/api/users_controller.rb
def index
json = cache ['v1', CacheKeyRegistry.users] do
@users = User.all
ActiveModel::Serializer.build_json(self, @users, {}).to_json
end
render json: json
end
# lib/cache_key_registry.rb
module CacheKeyRegistry
class << self
def users; key(User, __method__) end
private
def key(scope, key_prefix)
count = scope.count
max_updated_at =
scope.maximum(:updated_at).try(:utc).try(:to_s, :number)
"#{key_prefix}/all-#{count}-#{max_updated_at}"
end
end
end
“users/all-25-2014-02-23”