class MyStuff::Cache::Base

Base implementation.

You probably want a subclass instead:

Attributes

logger[R]
prefix[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/my_stuff/cache/base.rb, line 14
def initialize options = {}
  @logger = options[:logger] || create_logger
  @prefix = options[:prefix]
end

Public Instance Methods

get(keys, options = {}) click to toggle source

Fetch values from cache.

Returns an array of values. If a key is not in the cache, nil is returned instead.

keys is an array of cache keys.

Currently, no options are supported.

# File lib/my_stuff/cache/base.rb, line 78
def get keys, options = {}
  raise NotImplementedError.new
end
get_with_fallback(ids, key_pattern, options = {}) click to toggle source

Try to get ids from cache, falling back to the given block.

See get_with_multi_fallback for documentation.

Unlike get_with_multi_fallback, fallback gets called once for each id. You almost certainly want to use get_with_multi_fallback instead.

# File lib/my_stuff/cache/base.rb, line 26
def get_with_fallback ids, key_pattern, options = {}, &fallback
  get_with_multi_fallback(ids, key_pattern, options) do |ids|
    result = ids.map{|id| fallback.call(id) }
    result
  end
end
get_with_multi_fallback(ids, key_pattern, options = {}) click to toggle source

Try to get ids from cache, falling back to to the given block.

  • ids is an array of ids

  • key_pattern is a printf string to turn an id into a cache key

The following options are supported:

update_cache

If fallback is called, cache the result (default true)

force_update

Fetch from the database, even if there’s a value in cache.

ttl: Keep fetched values in cache for the specified number of

seconds. Defaults to forever (0). May be completely ignored.

The block gets passed the list of ids which missed the cache.

# File lib/my_stuff/cache/base.rb, line 47
def get_with_multi_fallback ids, key_pattern, options = {}, &fallback
  options = {
    :update_cache => true,
  }.merge(options)
  
  to_cache = Hash.new
  if options[:force_update]
    data = Hash[ids.zip([nil] * ids.size)]
  else
    data = Hash[ids.zip(get(ids.map{|x| key_pattern % x}))]
  end
  misses = data.select{|k,v| !v}.keys
  unless misses.empty?
    from_fallback = fallback.call(misses)
    Hash[misses.zip(from_fallback)].each do |k,v|
      to_cache[key_pattern % k] = v
      data[k] = v;
    end
  end
  set(to_cache) unless to_cache.empty? || !options[:update_cache]
  ids.map{|k| data[k]}
end
set(values, options = {}) click to toggle source

Set values in the cache.

values is a hash of cache key => values.

The following options are supported: ttl: Keep fetched values in cache for the specified number of

seconds. Defaults to forever (0). May be completely ignored.
# File lib/my_stuff/cache/base.rb, line 89
def set values, options = {}
  raise NotImplementedError.new
end

Private Instance Methods

create_logger() click to toggle source
# File lib/my_stuff/cache/base.rb, line 95
def create_logger
  # Use MyStuff::Logger if it's already loaded; fall back to standard
  # Logger
  if (MyStuff.const_get(:Logger) rescue nil)
    MyStuff::Logger.new
  else
    require 'logger'
    Logger.new(STDOUT)
  end
end