2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-09-28 14:40:27 +08:00
|
|
|
require "distributed_cache"
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
class ApplicationSerializer < ActiveModel::Serializer
|
2013-03-23 23:02:59 +08:00
|
|
|
embed :ids, include: true
|
2015-09-28 14:40:27 +08:00
|
|
|
|
|
|
|
class CachedFragment
|
|
|
|
def initialize(json)
|
|
|
|
@json = json
|
|
|
|
end
|
2023-12-15 23:46:04 +08:00
|
|
|
|
2015-09-28 14:40:27 +08:00
|
|
|
def as_json(*_args)
|
|
|
|
@json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-03 15:18:28 +08:00
|
|
|
def self.expire_cache_fragment!(name_or_regexp)
|
|
|
|
case name_or_regexp
|
|
|
|
when String
|
|
|
|
fragment_cache.delete(name_or_regexp)
|
|
|
|
when Regexp
|
2024-02-22 04:19:04 +08:00
|
|
|
fragment_cache.clear_regex(name_or_regexp)
|
2021-06-03 15:18:28 +08:00
|
|
|
end
|
2015-09-28 14:40:27 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.fragment_cache
|
|
|
|
@cache ||= DistributedCache.new("am_serializer_fragment_cache")
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2021-06-03 14:02:40 +08:00
|
|
|
def cache_fragment(name, &block)
|
|
|
|
ApplicationSerializer.fragment_cache.defer_get_set(name, &block)
|
2015-09-28 14:40:27 +08:00
|
|
|
end
|
2019-02-15 07:24:29 +08:00
|
|
|
|
|
|
|
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
|