Module: SecondLevelCache::Mixin

Extended by:
ActiveSupport::Concern
Defined in:
lib/second_level_cache/mixin.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#expire_changed_association_uniq_keysObject



87
88
89
90
91
92
93
# File 'lib/second_level_cache/mixin.rb', line 87

def expire_changed_association_uniq_keys
  reflections = klass.reflections.select { |_, reflection| reflection.belongs_to? }
  changed_keys = reflections.map { |_, reflection| reflection.foreign_key } & previous_changes.keys
  changed_keys.each do |key|
    SecondLevelCache.cache_store.delete(klass.send(:cache_uniq_key, key => previous_changes[key][0]))
  end
end

#expire_second_level_cacheObject



72
73
74
75
# File 'lib/second_level_cache/mixin.rb', line 72

def expire_second_level_cache
  return unless klass.second_level_cache_enabled?
  SecondLevelCache.cache_store.delete(second_level_cache_key)
end

#klassObject



68
69
70
# File 'lib/second_level_cache/mixin.rb', line 68

def klass
  self.class.base_class
end

#second_level_cache_keyObject



64
65
66
# File 'lib/second_level_cache/mixin.rb', line 64

def second_level_cache_key
  klass.second_level_cache_key(id)
end

#write_second_level_cacheObject Also known as: update_second_level_cache



77
78
79
80
81
82
83
# File 'lib/second_level_cache/mixin.rb', line 77

def write_second_level_cache
  return unless klass.second_level_cache_enabled?
  marshal = RecordMarshal.dump(self)
  expires_in = klass.second_level_cache_options[:expires_in]
  expire_changed_association_uniq_keys
  SecondLevelCache.cache_store.write(second_level_cache_key, marshal, expires_in: expires_in)
end