FIX: Clear post action types application serializer fragment cache.

The bug was introduced in dc10bdee3d
This commit is contained in:
Alan Guo Xiang Tan 2021-06-03 15:18:28 +08:00
parent cadf5eafe6
commit 3c1f4d5771
3 changed files with 29 additions and 4 deletions

View File

@ -7,8 +7,8 @@ class PostActionType < ActiveRecord::Base
include AnonCacheInvalidator
def expire_cache
ApplicationSerializer.expire_cache_fragment!("post_action_types")
ApplicationSerializer.expire_cache_fragment!("post_action_flag_types")
ApplicationSerializer.expire_cache_fragment!(/^post_action_types_/)
ApplicationSerializer.expire_cache_fragment!(/^post_action_flag_types_/)
end
class << self

View File

@ -14,8 +14,15 @@ class ApplicationSerializer < ActiveModel::Serializer
end
end
def self.expire_cache_fragment!(name)
fragment_cache.delete(name)
def self.expire_cache_fragment!(name_or_regexp)
case name_or_regexp
when String
fragment_cache.delete(name_or_regexp)
when Regexp
fragment_cache.hash.keys
.select { |k| k =~ name_or_regexp }
.each { |k| fragment_cache.delete(k) }
end
end
def self.fragment_cache

View File

@ -4,6 +4,24 @@ require 'rails_helper'
describe PostActionType do
context "callbacks" do
describe '#expiry_cache' do
it 'should clear the cache on save' do
cache = ApplicationSerializer.fragment_cache
cache["post_action_types_#{I18n.locale}"] = 'test'
cache["post_action_flag_types_#{I18n.locale}"] = 'test2'
PostActionType.new(name_key: 'some_key').save!
expect(cache["post_action_types_#{I18n.locale}"]).to eq(nil)
expect(cache["post_action_flag_types_#{I18n.locale}"]).to eq(nil)
ensure
ApplicationSerializer.fragment_cache.clear
end
end
end
describe '#types' do
context "verify enum sequence" do
before do