Module: IdentityCache::QueryAPI

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#expire_attribute_indexes ⇒ Object

:nodoc:



393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/identity_cache/query_api.rb', line 393

def expire_attribute_indexes # :nodoc:
  cache_attributes.try(:each) do |(attribute, fields)|
    unless was_new_record?
      old_cache_attribute_key = attribute_cache_key_for_attribute_and_previous_values(attribute, fields)
      IdentityCache.cache.delete(old_cache_attribute_key)
    end
    new_cache_attribute_key = attribute_cache_key_for_attribute_and_current_values(attribute, fields)
    if new_cache_attribute_key != old_cache_attribute_key
      IdentityCache.cache.delete(new_cache_attribute_key)
    end
  end
end

#expire_cache ⇒ Object

:nodoc:



406
407
408
409
410
411
# File 'lib/identity_cache/query_api.rb', line 406

def expire_cache # :nodoc:
  expire_primary_index
  expire_secondary_indexes
  expire_attribute_indexes
  true
end

#expire_primary_index ⇒ Object

:nodoc:



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/identity_cache/query_api.rb', line 358

def expire_primary_index # :nodoc:
  return unless self.class.primary_cache_index_enabled

  IdentityCache.logger.debug do
    extra_keys =
      if respond_to?(:updated_at)
        old_updated_at = old_values_for_fields([:updated_at]).first
        "expiring_last_updated_at=#{old_updated_at}"
      else
        ""
      end

    "[IdentityCache] expiring=#{self.class.name} expiring_id=#{id} #{extra_keys}"
  end

  IdentityCache.cache.delete(primary_cache_index_key)
end

#expire_secondary_indexes ⇒ Object

:nodoc:



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/identity_cache/query_api.rb', line 376

def expire_secondary_indexes # :nodoc:
  return unless self.class.primary_cache_index_enabled
  cache_indexes.try(:each) do |fields|
    if self.destroyed?
      IdentityCache.cache.delete(secondary_cache_index_key_for_previous_values(fields))
    else
      new_cache_index_key = secondary_cache_index_key_for_current_values(fields)
      IdentityCache.cache.delete(new_cache_index_key)

      if !was_new_record?
        old_cache_index_key = secondary_cache_index_key_for_previous_values(fields)
        IdentityCache.cache.delete(old_cache_index_key) unless old_cache_index_key == new_cache_index_key
      end
    end
  end
end

#fetch_denormalized_cached_association(ivar_name, association_name) ⇒ Object

:nodoc:



336
337
338
339
340
341
342
343
344
345
# File 'lib/identity_cache/query_api.rb', line 336

def fetch_denormalized_cached_association(ivar_name, association_name) # :nodoc:
  ivar_full_name = :"@#{ivar_name}"
  if IdentityCache.should_cache?
    populate_denormalized_cached_association(ivar_name, association_name)
    assoc = IdentityCache.unmap_cached_nil_for(instance_variable_get(ivar_full_name))
    assoc.is_a?(ActiveRecord::Associations::CollectionAssociation) ? assoc.reader : assoc
  else
    send(association_name.to_sym)
  end
end

#populate_association_caches ⇒ Object

:nodoc:



325
326
327
328
329
330
331
332
333
334
# File 'lib/identity_cache/query_api.rb', line 325

def populate_association_caches # :nodoc:
  self.class.all_cached_associations_needing_population.each do |cached_association, options|
    send(options[:population_method_name])
    reflection = options[:embed] && self.class.reflect_on_association(cached_association)
    if reflection && reflection.klass.respond_to?(:cached_has_manys)
      child_objects = Array.wrap(send(options[:cached_accessor_name]))
      child_objects.each(&:populate_association_caches)
    end
  end
end

#populate_denormalized_cached_association(ivar_name, association_name) ⇒ Object

:nodoc:



347
348
349
350
351
352
353
354
355
356
# File 'lib/identity_cache/query_api.rb', line 347

def populate_denormalized_cached_association(ivar_name, association_name) # :nodoc:
  ivar_full_name = :"@#{ivar_name}"

  value = instance_variable_get(ivar_full_name)
  return value unless value.nil?

  loaded_association = send(association_name)

  instance_variable_set(ivar_full_name, IdentityCache.map_cached_nil_for(loaded_association))
end

#was_new_record? ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


413
414
415
# File 'lib/identity_cache/query_api.rb', line 413

def was_new_record? # :nodoc:
  !destroyed? && transaction_changed_attributes.has_key?('id') && transaction_changed_attributes['id'].nil?
end