mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-30 10:58:31 -05:00
FEATURE: fragment cache for use with serializers
This commit is contained in:
parent
722e1fc4d0
commit
3b4e52cb36
1 changed files with 25 additions and 0 deletions
|
@ -1,3 +1,28 @@
|
|||
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)
|
||||
ApplicationSerializer.fragment_cache[name] ||= yield
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue