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
# File 'lib/identity_cache/cache_key_generation.rb', line 10

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


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

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:


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

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:


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

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

#old_values_for_fields(fields) ⇒ Object

:nodoc:


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

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:


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