Module: IdentityCache::CacheKeyGeneration

Extended by:
ActiveSupport::Concern
Defined in:
lib/identity_cache/cache_key_generation.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

DEFAULT_NAMESPACE =
"IDC:#{CACHE_VERSION}:".freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.denormalized_schema_hash(klass) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/identity_cache/cache_key_generation.rb', line 10

def self.denormalized_schema_hash(klass)
  schema_string = schema_to_string(klass.columns)
  if !(associations = embedded_associations(klass)).empty?
    embedded_schema = associations.map do |name, options|
      "#{name}:(#{denormalized_schema_hash(options[:association_class])})"
    end.sort.join(',')
    schema_string << "," << embedded_schema
  end
  IdentityCache.memcache_hash(schema_string)
end

.embedded_associations(klass) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/identity_cache/cache_key_generation.rb', line 21

def self.embedded_associations(klass)
  if klass.respond_to?(:all_embedded_associations)
    klass.all_embedded_associations
  else
    {}
  end
end

.schema_to_string(columns) ⇒ Object



6
7
8
# File 'lib/identity_cache/cache_key_generation.rb', line 6

def self.schema_to_string(columns)
  columns.sort_by(&:name).map{|c| "#{c.name}:#{c.type}"}.join(',')
end

Instance Method Details

#attribute_cache_key_for_attribute_and_current_values(attribute, fields) ⇒ Object

:nodoc:



70
71
72
# File 'lib/identity_cache/cache_key_generation.rb', line 70

def attribute_cache_key_for_attribute_and_current_values(attribute, fields) # :nodoc:
  self.class.rails_cache_key_for_attribute_and_fields_and_values(attribute, fields, current_values_for_fields(fields))
end

#attribute_cache_key_for_attribute_and_previous_values(attribute, fields) ⇒ Object

:nodoc:



74
75
76
# File 'lib/identity_cache/cache_key_generation.rb', line 74

def attribute_cache_key_for_attribute_and_previous_values(attribute, fields) # :nodoc:
  self.class.rails_cache_key_for_attribute_and_fields_and_values(attribute, fields, old_values_for_fields(fields))
end

#current_values_for_fields(fields) ⇒ Object

:nodoc:



78
79
80
# File 'lib/identity_cache/cache_key_generation.rb', line 78

def current_values_for_fields(fields) # :nodoc:
  fields.collect {|field| self.send(field)}
end

#old_values_for_fields(fields) ⇒ Object

:nodoc:



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/identity_cache/cache_key_generation.rb', line 82

def old_values_for_fields(fields) # :nodoc:
  fields.map do |field|
    field_string = field.to_s
    if destroyed? && transaction_changed_attributes.has_key?(field_string)
      transaction_changed_attributes[field_string]
    elsif persisted? && transaction_changed_attributes.has_key?(field_string)
      transaction_changed_attributes[field_string]
    else
      self.send(field)
    end
  end
end

#primary_cache_index_key ⇒ Object

:nodoc:



58
59
60
# File 'lib/identity_cache/cache_key_generation.rb', line 58

def primary_cache_index_key # :nodoc:
  self.class.rails_cache_key(id)
end

#secondary_cache_index_key_for_current_values(fields) ⇒ Object

:nodoc:



62
63
64
# File 'lib/identity_cache/cache_key_generation.rb', line 62

def secondary_cache_index_key_for_current_values(fields) # :nodoc:
  self.class.rails_cache_index_key_for_fields_and_values(fields, current_values_for_fields(fields))
end

#secondary_cache_index_key_for_previous_values(fields) ⇒ Object

:nodoc:



66
67
68
# File 'lib/identity_cache/cache_key_generation.rb', line 66

def secondary_cache_index_key_for_previous_values(fields) # :nodoc:
  self.class.rails_cache_index_key_for_fields_and_values(fields, old_values_for_fields(fields))
end