class Restforce::DB::RecordCache

Restforce::DB::RecordCache serves as a means of caching the collections of recently-updated database and Salesforce instances for passed mappings. The general goal is to avoid making repetitive Salesforce API calls or database queries, and ensure a consistent list of objects during a synchronization run.

Public Class Methods

new() click to toggle source

Public: Initialize a new Restforce::DB::RecordCache.

# File lib/restforce/db/record_cache.rb, line 13
def initialize
  reset
end

Public Instance Methods

collection(mapping, record_type, options = {}) click to toggle source

Public: Iterate through the recently-updated instances of the specified type for the passed mapping. Memoizes the records if the collection has not previously been cached.

mapping - A Restforce::DB::Mapping. record_type - A Symbol naming a mapping record type. Valid values are

:salesforce_record_type or :database_record_type.

options - A Hash of options to pass to `all` (optional).

Returns an Array of Restforce::DB::Instances::Base.

# File lib/restforce/db/record_cache.rb, line 27
def collection(mapping, record_type, options = {})
  return cached_value(mapping, record_type) if cached?(mapping, record_type)
  cache(mapping, record_type, mapping.send(record_type).all(options))
end
reset() click to toggle source

Public: Reset the cache. Should be invoked between runs to ensure that new options are respected.

Returns nothing.

# File lib/restforce/db/record_cache.rb, line 36
def reset
  @cache = Hash.new { |h, k| h[k] = {} }
end

Private Instance Methods

cache(mapping, record_type, value) click to toggle source

Internal: Store the supplied value in the cache for the passed mapping and record type.

mapping - A Restforce::DB::Mapping. record_type - A Symbol naming a mapping record type. Valid values are

:salesforce_record_type or :database_record_type.

Returns the cached value.

# File lib/restforce/db/record_cache.rb, line 50
def cache(mapping, record_type, value)
  @cache[record_type][key_for(mapping)] = value
end
cached?(mapping, record_type) click to toggle source

Internal: Have we cached a collection for the passed mapping and record type?

mapping - A Restforce::DB::Mapping. record_type - A Symbol naming a mapping record type. Valid values are

:salesforce_record_type or :database_record_type.

Returns a Boolean.

# File lib/restforce/db/record_cache.rb, line 74
def cached?(mapping, record_type)
  !cached_value(mapping, record_type).nil?
end
cached_value(mapping, record_type) click to toggle source

Internal: Get the cached collection for the passed mapping and record type.

mapping - A Restforce::DB::Mapping. record_type - A Symbol naming a mapping record type. Valid values are

:salesforce_record_type or :database_record_type.

Returns nil or an Array.

# File lib/restforce/db/record_cache.rb, line 62
def cached_value(mapping, record_type)
  @cache[record_type][key_for(mapping)]
end
key_for(mapping) click to toggle source

Internal: Get a unique key with enough information to look up the passed mapping in the cache. Scopes the mapping by its current list of conditions.

mapping - A Restforce::DB::Mapping.

Returns an Object.

# File lib/restforce/db/record_cache.rb, line 85
def key_for(mapping)
  [mapping, mapping.conditions]
end