-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.rb
127 lines (110 loc) · 3.64 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# frozen_string_literal: true
require 'digest/sha1'
require 'object/cache/version'
# Caching of objects in a Redis store
class Cache
@default_ttl = (7 * 24 * 60 * 60) # 1 week
class << self
attr_accessor :backend
attr_accessor :default_ttl
attr_accessor :default_key_prefix
# new
#
# Finds the correct value (based on the provided key) in the cache store, or
# calls the original code, and stores the result in cache.
#
# The TTL of the cached content is provided with the optional `ttl` named
# argument. If left blank, the `DEFAULT_TTL` ttl value will be used.
#
# The caching key will be determined by creating a SHA digest of the
# original code's file location and line number within that file. This makes
# it easier to provide short caching keys like uid's, or ids, and still
# receive a unique caching key under which the data is stored.
#
# The cache key can optionally be left blank. This should **only be done**
# if the provided data by the method will never changes based on some form
# of input.
#
# For example: caching an `Item` should _always_ be done by providing a
# unique item identifier as the caching key, otherwise the cache will return
# the same item every time, even if a different one is stored the second
# time.
#
# good:
#
# Cache.new { 'hello world' } # stored object is always the same
# Cache.new(item.id) { item } # stored item is namespaced using its id
#
# bad:
#
# Cache.new { item } # item is only stored once, and then always
# # retrieved, even if it is a different item
#
def new(key = nil, ttl: default_ttl, key_prefix: default_key_prefix, &block)
return yield unless replica
begin
key = build_key(key, key_prefix, block)
if (cached_value = replica.get(key)).nil?
yield.tap do |value|
begin
update_cache(key, value, ttl: ttl)
rescue TypeError
# if `TypeError` is raised, the data could not be Marshal dumped. In that
# case, delete anything left in the cache store, and get the data without
# caching.
#
delete(key)
end
end
else
begin
Marshal.load(cached_value)
rescue
delete(key)
yield
end
end
end
end
def include?(key)
replica.exists?(key)
rescue
false
end
def delete(key)
return false unless include?(key)
primary.del(key)
true
end
def update_cache(key, value, ttl: default_ttl)
return unless primary && (value = Marshal.dump(value))
ttl.to_i.zero? ? primary.set(key, value) : primary.setex(key, ttl.to_i, value)
end
def primary
backend.is_a?(Hash) ? backend[:primary] : backend
end
def replicas
replicas = backend.is_a?(Hash) ? backend[:replicas] : backend
replicas.respond_to?(:sample) ? replicas : [replicas]
end
def replica
replicas.sample
end
def build_key(key, key_prefix, proc)
hash = Digest::SHA1.hexdigest([key, proc.source_location].flatten.join)[0..11]
prefix = build_key_prefix(key_prefix, proc)
[prefix, hash].compact.join('_')
end
def build_key_prefix(key_prefix, proc)
case key_prefix
when :method_name
location = caller_locations.find { |l| proc.source_location.join == "#{l.path}#{l.lineno}" }
location&.base_label
when :class_name
proc.binding.receiver.class.to_s
else
key_prefix
end
end
end
end