Class: SimpleCache

Inherits:
Object
  • Object
show all
Defined in:
lib/cta_redux/faraday_middleware/simple_cache.rb

Overview

A simplistic in-process cache. Used internally by cta_redux and caches responses for 60 seconds.

Instance Method Summary collapse

Constructor Details

#initialize(cache) ⇒ SimpleCache

Returns a new instance of SimpleCache.



4
5
6
# File 'lib/cta_redux/faraday_middleware/simple_cache.rb', line 4

def initialize(cache)
  @cache = cache
end

Instance Method Details

#fetch(name, options = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cta_redux/faraday_middleware/simple_cache.rb', line 8

def fetch(name, options = nil)
  entry = read(name, options)

  if !entry && block_given?
  	entry = yield
  	write(name, entry)
  elsif !entry
    entry = read(name, options)
  end

  entry
end

#read(name, options = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/cta_redux/faraday_middleware/simple_cache.rb', line 21

def read(name, options = nil)
  entry = @cache[name]

  if entry && entry[:expiration] < Time.now
    entry = @cache[name] = nil
  end

  entry ? entry[:value] : entry
end

#write(name, value) ⇒ Object



31
32
33
# File 'lib/cta_redux/faraday_middleware/simple_cache.rb', line 31

def write(name, value)
  @cache[name] = { :expiration => (Time.now + 60), :value => value }
end