Module: CacheMachine::Cache::Map::ClassMethods

Defined in:
lib/cache_machine/cache/map.rb

Instance Method Summary collapse

Instance Method Details

#cache_associated(associations) ⇒ Object

Fills cache map.



18
19
20
21
22
# File 'lib/cache_machine/cache/map.rb', line 18

def cache_associated associations
  [*associations].each do |association|
    self.cache_map.merge! association.is_a?(Hash) ? association : {association => []}
  end
end

#define_timestamp(timestamp_name, options = {}, &block) ⇒ Object

Defines timestamp for object.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cache_machine/cache/map.rb', line 25

def define_timestamp timestamp_name, options = {}, &block
  options[:timestamp] = block if block

  define_method timestamp_name do
    fetch_cache_of(timestamp_key_of(timestamp_name), options) do
      CacheMachine::Logger.info "CACHE_MACHINE (define_timestamp): deleting old timestamp '#{timestamp_name}'."
      delete_cache_of timestamp_name # Case when cache expired by time.
      Time.now.to_i.to_s
    end
  end
end

#delete_association_cache_on(record, reflection) ⇒ Object

Deletes cache of collection with name association_id for each object associated with record Called only when has_many :through collection changed.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cache_machine/cache/map.rb', line 39

def delete_association_cache_on record, reflection
  pk = record.class.primary_key

  joining = unless reflection.options[:source_type]
    reflection.through_reflection ? { reflection.through_reflection.name => reflection.source_reflection.name } : reflection.name
  else
    reflection.name
  end

  self.joins(joining).where(reflection.table_name => { pk => record.send(pk) }).find_each do |cache_source_record|
    cache_source_record.delete_cache_of reflection.name
  end
end

#has_and_belongs_to_many(association_id, options = {}) ⇒ Object

Overwrites has_and_belongs_to_many of ActiveRecord class to hook Cache Machine.



66
67
68
69
70
71
72
73
74
75
# File 'lib/cache_machine/cache/map.rb', line 66

def has_and_belongs_to_many(association_id, options = {})
  # Ensure what collection should be tracked.
  if(should_be_on_hook = self.cache_map.keys.include?(association_id))
    # If relation is _many_to_many_ track collection changes.
    options[:after_add] = \
    options[:before_remove] = :delete_association_cache_on
  end
  super
  hook_cache_machine_on association_id if should_be_on_hook
end

#has_many(association_id, options = {}) ⇒ Object

Overwrites has_many of ActiveRecord class to hook Cache Machine.



54
55
56
57
58
59
60
61
62
63
# File 'lib/cache_machine/cache/map.rb', line 54

def has_many(association_id, options = {})
  # Ensure what collection should be tracked.
  if (should_be_on_hook = self.cache_map.keys.include?(association_id)) && options[:through]
    # If relation is _many_to_many_ track collection changes.
    options[:after_add] = \
    options[:before_remove] = :delete_association_cache_on
  end
  super
  hook_cache_machine_on association_id if should_be_on_hook
end