forked from instacart/makara
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.rb
53 lines (40 loc) · 1.21 KB
/
cache.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'active_support/core_ext/object/try'
# The Makara Cache should have access to your centralized cache store.
# It serves the purpose of storing the Makara::Context across requests, servers, etc.
module Makara
module Cache
autoload :MemoryStore, 'makara/cache/memory_store'
autoload :NoopStore, 'makara/cache/noop_store'
class << self
def store=(store)
@store = store
end
def read(key)
store.try(:read, key)
end
def write(key, value, ttl)
store.try(:write, key, value, :expires_in => ttl.to_i)
end
protected
def store
case @store
when :noop, :null
@store = Makara::Cache::NoopStore.new
when :memory
@store = Makara::Cache::MemoryStore.new
else
if defined?(Rails)
# in AR3 RAILS_CACHE may not be loaded if the full env is not present
# Rails.cache will throw an error because of it.
if ActiveRecord::VERSION::MAJOR < 4
@store ||= Rails.cache if defined?(RAILS_CACHE)
else
@store ||= Rails.cache if defined?(Rails)
end
end
end
@store
end
end
end
end