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



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

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



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

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

#klassObject



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

def klass
  self.class.base_class
end

#second_level_cache_keyObject



59
60
61
# File 'lib/second_level_cache/mixin.rb', line 59

def second_level_cache_key
  klass.second_level_cache_key(id)
end

#write_second_level_cacheObject Also known as: update_second_level_cache



72
73
74
75
76
77
78
79
80
81
# File 'lib/second_level_cache/mixin.rb', line 72

def write_second_level_cache
  return unless klass.second_level_cache_enabled?
  # Avoid rewrite cache again, when record has been soft deleted
  return if respond_to?(:deleted?) && send(:deleted?)

  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