Class: WarpCore::Cache

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

Overview

A class helper to manage a Moneta cache store that can be used throughout the system.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.storeObject

Returns the value of attribute store.



19
20
21
# File 'lib/warpcore/cache.rb', line 19

def store
  @store
end

Class Method Details

.get(key, opts = {}, block = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/warpcore/cache.rb', line 25

def get(key, opts = {}, block = nil)
  block = Proc.new if block_given? && block.nil?
  valid = block.respond_to?(:call)
  if @store.nil?
    return valid ? block.call(key) : nil
  end
  return @store[key] unless block.respond_to?(:call)
  opts[:expires] = opts[:expires].to_i if opts[:expires].present?
  @store.fetch(key, opts) do |key|
    val = block.call(key)
    self.set(key, val, opts)
  end
rescue Exception => e
  warn "[WarpCore::Cache] Fetch Error => #{e}"
  block.respond_to?(:call) ? block.call(key) : nil
end

.set(key, value, opts = {}) ⇒ Object



42
43
44
45
46
47
# File 'lib/warpcore/cache.rb', line 42

def set(key,value, opts = {})
  @store&.store(key, value, opts) || value
rescue Exception => e
  warn "[WarpCore::Cache] Store Error => #{e}"
  value
end

.setup(moneta, opts = {}) ⇒ Object



21
22
23
# File 'lib/warpcore/cache.rb', line 21

def setup(moneta, opts = {})
  @store = moneta
end