Module: IdentityCache::CacheKeyGeneration

Extended by:
ActiveSupport::Concern
Included in:
IdentityCache
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
20
21
22
23
24
# File 'lib/identity_cache/cache_key_generation.rb', line 10

def self.denormalized_schema_hash(klass)
  schema_string = schema_to_string(klass.columns)
  if klass.include?(IdentityCache)
    klass.send(:all_cached_associations).sort.each do |name, options|
      klass.send(:check_association_scope, name)
      case options[:embed]
      when true
        schema_string << ",#{name}:(#{denormalized_schema_hash(options[:association_reflection].klass)})"
      when :ids
        schema_string << ",#{name}:ids"
      end
    end
  end
  IdentityCache.memcache_hash(schema_string)
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, unique) ⇒ Object

:nodoc:



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

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

#attribute_cache_key_for_attribute_and_previous_values(attribute, fields, unique) ⇒ Object

:nodoc:



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

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

#current_values_for_fields(fields) ⇒ Object

:nodoc:



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

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

#old_values_for_fields(fields) ⇒ Object

:nodoc:



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/identity_cache/cache_key_generation.rb', line 75

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_keyObject

:nodoc:



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

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