Class: Rabl::CacheEngine
- Inherits:
-
Object
- Object
- Rabl::CacheEngine
- Defined in:
- lib/rabl/cache_engine.rb
Instance Method Summary collapse
-
#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.
-
#initialize ⇒ CacheEngine
constructor
A new instance of CacheEngine.
- #read_multi(*keys) ⇒ Object
- #write(key, value, options = {}) ⇒ Object
Constructor Details
#initialize ⇒ CacheEngine
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, , &block) if defined?(Rails) Rails.cache.fetch(key, , &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) = keys. if defined?(Rails) Rails.cache.read_multi(*keys, ) 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, = {}) if defined?(Rails) Rails.cache.write(key, value, ) else @cache[key] = yield end end |