Class: Readthis::Cache
- Inherits:
-
Object
- Object
- Readthis::Cache
- Defined in:
- lib/readthis/cache.rb
Instance Attribute Summary collapse
-
#expires_in ⇒ Object
readonly
Returns the value of attribute expires_in.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
Class Method Summary collapse
-
.notifications ⇒ Object
Provide a class level lookup of the proper notifications module.
Instance Method Summary collapse
- #clear ⇒ Object
- #decrement(key, options = {}) ⇒ Object
- #delete(key, options = {}) ⇒ Object
- #exist?(key, options = {}) ⇒ Boolean
- #fetch(key, options = {}) ⇒ Object
-
#fetch_multi(*keys) ⇒ Object
Fetches multiple keys from the cache using a single call to the server and filling in any cache misses.
- #increment(key, options = {}) ⇒ Object
-
#initialize(url, options = {}) ⇒ Cache
constructor
Creates a new Readthis::Cache object with the given redis URL.
- #read(key, options = {}) ⇒ Object
- #read_multi(*keys) ⇒ Object
- #write(key, value, options = {}) ⇒ Object
Constructor Details
#initialize(url, options = {}) ⇒ Cache
26 27 28 29 30 31 32 33 |
# File 'lib/readthis/cache.rb', line 26 def initialize(url, = {}) @expires_in = .fetch(:expires_in, nil) @namespace = .fetch(:namespace, nil) @pool = ConnectionPool.new(()) do Redis.new(url: url) end end |
Instance Attribute Details
#expires_in ⇒ Object (readonly)
Returns the value of attribute expires_in.
8 9 10 |
# File 'lib/readthis/cache.rb', line 8 def expires_in @expires_in end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
8 9 10 |
# File 'lib/readthis/cache.rb', line 8 def namespace @namespace end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
8 9 10 |
# File 'lib/readthis/cache.rb', line 8 def pool @pool end |
Class Method Details
.notifications ⇒ Object
Provide a class level lookup of the proper notifications module. Instrumention is expected to occur within applications that have ActiveSupport::Notifications available, but needs to work even when it isn’t.
14 15 16 17 18 19 20 |
# File 'lib/readthis/cache.rb', line 14 def self.notifications if Object.const_defined?('ActiveSupport::Notifications') ActiveSupport::Notifications else Readthis::Notifications end end |
Instance Method Details
#clear ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/readthis/cache.rb', line 144 def clear instrument(:clear, '*') do with do |store| store.flushdb end end end |
#decrement(key, options = {}) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/readthis/cache.rb', line 85 def decrement(key, = {}) instrument(:decrement, key) do with do |store| store.decr(namespaced_key(key, ())) end end end |
#delete(key, options = {}) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/readthis/cache.rb', line 58 def delete(key, = {}) instrument(:delete, key) do with do |store| store.del(namespaced_key(key, ())) end end end |
#exist?(key, options = {}) ⇒ Boolean
136 137 138 139 140 141 142 |
# File 'lib/readthis/cache.rb', line 136 def exist?(key, = {}) instrument(:exist?, key) do with do |store| store.exists(namespaced_key(key, ())) end end end |
#fetch(key, options = {}) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/readthis/cache.rb', line 66 def fetch(key, = {}) value = read(key, ) unless [:force] if value.nil? && block_given? value = yield(key) write(key, value, ) end value end |
#fetch_multi(*keys) ⇒ Object
Fetches multiple keys from the cache using a single call to the server and filling in any cache misses. All read and write operations are executed atomically.
cache.fetch_multi('alpha', 'beta') do |key|
"#{key}-was-missing"
end
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/readthis/cache.rb', line 115 def fetch_multi(*keys) results = read_multi(*keys) = ((keys)) instrument(:fetch_multi, keys) do with do |store| store.pipelined do results.each do |key, value| if value.nil? value = yield key write(key, value, ) results[key] = value end end end end results end end |
#increment(key, options = {}) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/readthis/cache.rb', line 77 def increment(key, = {}) instrument(:incremenet, key) do with do |store| store.incr(namespaced_key(key, ())) end end end |
#read(key, options = {}) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/readthis/cache.rb', line 35 def read(key, = {}) instrument(:read, key) do with do |store| store.get(namespaced_key(key, ())) end end end |
#read_multi(*keys) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/readthis/cache.rb', line 93 def read_multi(*keys) = ((keys)) results = [] instrument(:read_multi, keys) do with do |store| results = store.pipelined do keys.each { |key| store.get(namespaced_key(key, )) } end end keys.zip(results).to_h end end |
#write(key, value, options = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/readthis/cache.rb', line 43 def write(key, value, = {}) = () namespaced = namespaced_key(key, ) instrument(:write, key) do with do |store| if expiration = [:expires_in] store.setex(namespaced, expiration, value) else store.set(namespaced, value) end end end end |