Class: MyStuff::Cache::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/my_stuff/cache/base.rb

Overview

Base implementation.

You probably want a subclass instead:

  • MyStuff::Cache::NullCache (not likely)

  • MyStuff::Cache::MemoryCache

  • MyStuff::Cache::MemcachedCache

Direct Known Subclasses

MemcachedCache, MemoryCache, NullCache

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



13
14
15
# File 'lib/my_stuff/cache/base.rb', line 13

def initialize options = {}
  @logger = options[:logger] || create_logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



12
13
14
# File 'lib/my_stuff/cache/base.rb', line 12

def logger
  @logger
end

Instance Method Details

#get(keys, options = {}) ⇒ Object

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.

Raises:

  • (NotImplementedError)


76
77
78
# File 'lib/my_stuff/cache/base.rb', line 76

def get keys, options = {}
  raise NotImplementedError.new
end

#get_with_fallback(ids, key_pattern, options = {}, &fallback) ⇒ Object

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.



24
25
26
27
28
29
# File 'lib/my_stuff/cache/base.rb', line 24

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 = {}, &fallback) ⇒ Object

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.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/my_stuff/cache/base.rb', line 45

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 = {}) ⇒ Object

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.

Raises:

  • (NotImplementedError)


87
88
89
# File 'lib/my_stuff/cache/base.rb', line 87

def set values, options = {}
  raise NotImplementedError.new
end