module CachedResource::Caching::ClassMethods

Public Instance Methods

clear_cache() click to toggle source

Clear the cache.

# File lib/cached_resource/caching.rb, line 27
def clear_cache
  cache_clear
end
find_with_cache(*arguments) click to toggle source

Find a resource using the cache or resend the request if :reload is set to true or caching is disabled.

# File lib/cached_resource/caching.rb, line 17
def find_with_cache(*arguments)
  arguments << {} unless arguments.last.is_a?(Hash)
  should_reload = arguments.last.delete(:reload) || !cached_resource.enabled
  arguments.pop if arguments.last.empty?
  key = cache_key(arguments)

  should_reload ? find_via_reload(key, *arguments) : find_via_cache(key, *arguments)
end

Private Instance Methods

cache_clear() click to toggle source

Clear the cache.

# File lib/cached_resource/caching.rb, line 114
def cache_clear
  cached_resource.cache.clear.tap do |result|
    cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} CLEAR")
  end
end
cache_collection_synchronize(object, *arguments) click to toggle source

If this is a pure, unadulterated “all” request write cache entries for all its members otherwise update an existing collection if possible.

# File lib/cached_resource/caching.rb, line 51
def cache_collection_synchronize(object, *arguments)
  if object.is_a? Enumerable
    update_singles_cache(object)
    # update the collection only if this is a subset of it
    update_collection_cache(object) unless is_collection?(*arguments)
  else
    update_collection_cache(object)
  end
end
cache_key(*arguments) click to toggle source

Generate the request cache key.

# File lib/cached_resource/caching.rb, line 121
def cache_key(*arguments)
  "#{name.parameterize.gsub("-", "/")}/#{arguments.join('/')}".downcase.delete(' ')
end
cache_read(key) click to toggle source

Read a entry from the cache for the given key.

# File lib/cached_resource/caching.rb, line 86
def cache_read(key)
  object = cached_resource.cache.read(key).try do |json_cache|

    json = ActiveSupport::JSON.decode(json_cache)

    unless json.nil?
      cache = json_to_object(json)
      if cache.is_a? Enumerable
        restored = cache.map { |record| full_dup(record) }
        next restored unless respond_to?(:collection_parser)
        collection_parser.new(restored)
      else
        full_dup(cache)
      end
    end
  end
  object && cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} READ #{key}")
  object
end
cache_write(key, object) click to toggle source

Write an entry to the cache for the given key and value.

# File lib/cached_resource/caching.rb, line 107
def cache_write(key, object)
  result = cached_resource.cache.write(key, object_to_json(object), :race_condition_ttl => cached_resource.race_condition_ttl, :expires_in => cached_resource.generate_ttl)
  result && cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} WRITE #{key}")
  result
end
find_via_cache(key, *arguments) click to toggle source

Try to find a cached response for the given key. If no cache entry exists, send a new request.

# File lib/cached_resource/caching.rb, line 35
def find_via_cache(key, *arguments)
  cache_read(key) || find_via_reload(key, *arguments)
end
find_via_reload(key, *arguments) click to toggle source

Re/send the request to fetch the resource. Cache the response for the request.

# File lib/cached_resource/caching.rb, line 41
def find_via_reload(key, *arguments)
  object = find_without_cache(*arguments)
  cache_collection_synchronize(object, *arguments) if cached_resource.collection_synchronize
  cache_write(key, object)
  cache_read(key)
end
full_dup(record) click to toggle source

Make a full duplicate of an ActiveResource record. Currently just dups the record then copies the persisted state.

# File lib/cached_resource/caching.rb, line 127
def full_dup(record)
  record.dup.tap do |o|
    o.instance_variable_set(:@persisted, record.persisted?)
  end
end
is_collection?(*arguments) click to toggle source

Determine if the given arguments represent the entire collection of objects.

# File lib/cached_resource/caching.rb, line 81
def is_collection?(*arguments)
  arguments == cached_resource.collection_arguments
end
json_to_object(json) click to toggle source
# File lib/cached_resource/caching.rb, line 133
def json_to_object(json)
  if json.is_a? Array
    json.map { |attrs|
      self.new(attrs["object"], attrs["persistence"]) }
  else
    self.new(json["object"], json["persistence"])
  end
end
object_to_json(object) click to toggle source
# File lib/cached_resource/caching.rb, line 142
def object_to_json(object)
  if object.is_a? Enumerable
     object.map { |o| { :object => o, :persistence => o.persisted? } }.to_json
  elsif object.nil?
    nil.to_json
  else
    { :object => object, :persistence => object.persisted? }.to_json
  end
end
update_collection_cache(updates) click to toggle source

Update the “mother” collection with an array of updates.

# File lib/cached_resource/caching.rb, line 68
def update_collection_cache(updates)
  updates = Array(updates)
  collection = cache_read(cache_key(cached_resource.collection_arguments))

  if collection && !updates.empty?
    index = collection.inject({}) { |hash, object| hash[object.send(primary_key)] = object; hash }
    updates.each { |object| index[object.send(primary_key)] = object }
    cache_write(cache_key(cached_resource.collection_arguments), index.values)
  end
end
update_singles_cache(updates) click to toggle source

Update the cache of singles with an array of updates.

# File lib/cached_resource/caching.rb, line 62
def update_singles_cache(updates)
  updates = Array(updates)
  updates.each { |object| cache_write(cache_key(object.send(primary_key)), object) }
end