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.

Parameters:

  • associations (Hash<Symbol, Array>)


20
21
22
23
24
# File 'lib/cache_machine/cache/map.rb', line 20

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.

Examples:

Define timestamp to be updated every hour.

class MyModel < ActiveRecord::Base
  include CacheMachine::Cache
  define_timestamp(:my_timestamp, :expires_in => 1.hour) { my_optional_value }
end

Parameters:

  • timestamp_name (String, Symbol)
  • options (Hash) (defaults to: {})


36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cache_machine/cache/map.rb', line 36

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

  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.zone.now.to_i.to_s
    end
  end
end

#delete_association_cache_on(record, reflection) ⇒ Object

Deletes cache of collection associated via many-to-many.

Parameters:

  • (ActiveRecord::Base)


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

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

Hooks association changes.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cache_machine/cache/map.rb', line 84

def has_and_belongs_to_many(association_id, options = {}) #:nodoc:

  # 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

Hooks association changes.



70
71
72
73
74
75
76
77
78
79
# File 'lib/cache_machine/cache/map.rb', line 70

def has_many(association_id, options = {}) #:nodoc:
  # 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