Class: Rabl::CacheEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/rabl/cache_engine.rb

Instance Method Summary collapse

Constructor Details

#initializeCacheEngine

Returns a new instance of CacheEngine.



31
32
33
34
35
# File 'lib/rabl/cache_engine.rb', line 31

def initialize
  unless defined?(Rails)
    @cache = LRU.new
  end
end

Instance Method Details

#fetch(key, cache_options, &block) ⇒ Object

Fetch given a key and options and a fallback block attempts to find the key in the cache and stores the block result in there if no key is found.

cache = Rabl::CacheEngine.new; cache.fetch(“some_key”) { “fallback data” }



43
44
45
46
47
48
49
# File 'lib/rabl/cache_engine.rb', line 43

def fetch(key, cache_options, &block)
  if defined?(Rails)
    Rails.cache.fetch(key, cache_options, &block)
  else
    @cache[key] ||= yield
  end
end

#read_multi(*keys) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/rabl/cache_engine.rb', line 59

def read_multi(*keys)
  options = keys.extract_options!
  if defined?(Rails)
    Rails.cache.read_multi(*keys, options)
  else
    keys.inject({}) { |hash, key| hash[key] = nil; hash }
  end
end

#write(key, value, options = {}) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/rabl/cache_engine.rb', line 51

def write(key, value, options = {})
  if defined?(Rails)
    Rails.cache.write(key, value, options)
  else
    @cache[key] = yield
  end
end