2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
require "distributed_cache"
|
2015-09-28 01:40:27 -05:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
class ApplicationSerializer < ActiveModel::Serializer
|
2013-03-23 10:02:59 -05:00
|
|
|
embed :ids, include: true
|
2015-09-28 01:40:27 -05:00
|
|
|
|
|
|
|
class CachedFragment
|
|
|
|
def initialize(json)
|
|
|
|
@json = json
|
|
|
|
end
|
|
|
|
def as_json(*_args)
|
|
|
|
@json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-03 02:18:28 -05:00
|
|
|
def self.expire_cache_fragment!(name_or_regexp)
|
|
|
|
case name_or_regexp
|
|
|
|
when String
|
|
|
|
fragment_cache.delete(name_or_regexp)
|
|
|
|
when Regexp
|
2023-01-09 06:20:10 -06:00
|
|
|
fragment_cache
|
|
|
|
.hash
|
|
|
|
.keys
|
2021-06-03 02:18:28 -05:00
|
|
|
.select { |k| k =~ name_or_regexp }
|
|
|
|
.each { |k| fragment_cache.delete(k) }
|
|
|
|
end
|
2015-09-28 01:40:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.fragment_cache
|
|
|
|
@cache ||= DistributedCache.new("am_serializer_fragment_cache")
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2021-06-03 01:02:40 -05:00
|
|
|
def cache_fragment(name, &block)
|
|
|
|
ApplicationSerializer.fragment_cache.defer_get_set(name, &block)
|
2015-09-28 01:40:27 -05:00
|
|
|
end
|
2019-02-14 17:24:29 -06:00
|
|
|
|
|
|
|
def cache_anon_fragment(name, &blk)
|
|
|
|
if scope.anonymous?
|
|
|
|
cache_fragment(name, &blk)
|
|
|
|
else
|
|
|
|
blk.call
|
|
|
|
end
|
|
|
|
end
|
2013-02-07 09:45:24 -06:00
|
|
|
end
|