discourse/app/serializers/application_serializer.rb
Alan Guo Xiang Tan a8667b5454 PERF: Defer setting of distributed cache in more spots.
See follow up commit for rational.

Follow-up to 8cfe203383
2021-06-04 09:13:18 +08:00

39 lines
710 B
Ruby

# frozen_string_literal: true
require 'distributed_cache'
class ApplicationSerializer < ActiveModel::Serializer
embed :ids, include: true
class CachedFragment
def initialize(json)
@json = json
end
def as_json(*_args)
@json
end
end
def self.expire_cache_fragment!(name)
fragment_cache.delete(name)
end
def self.fragment_cache
@cache ||= DistributedCache.new("am_serializer_fragment_cache")
end
protected
def cache_fragment(name, &block)
ApplicationSerializer.fragment_cache.defer_get_set(name, &block)
end
def cache_anon_fragment(name, &blk)
if scope.anonymous?
cache_fragment(name, &blk)
else
blk.call
end
end
end