discourse/app/serializers/application_serializer.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

45 lines
846 B
Ruby
Raw Normal View History

# frozen_string_literal: true
require "distributed_cache"
2013-02-06 03:16:51 +08:00
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_or_regexp)
case name_or_regexp
when String
fragment_cache.delete(name_or_regexp)
when Regexp
fragment_cache.clear_regex(name_or_regexp)
end
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
2013-02-07 23:45:24 +08:00
end