Class: Readthis::Cache
- Inherits:
-
Object
- Object
- Readthis::Cache
- Defined in:
- lib/readthis/cache.rb
Instance Attribute Summary collapse
-
#entity ⇒ Object
readonly
Returns the value of attribute entity.
-
#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, amount = 1, options = {}) ⇒ Object
Decrement a key in the store.
- #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, amount = 1, options = {}) ⇒ Object
Increment a key in the store.
-
#initialize(url, options = {}) ⇒ Cache
constructor
Creates a new Readthis::Cache object with the given redis URL.
-
#read(key, options = {}) ⇒ Object
Fetches data from the cache, using the given key.
- #read_multi(*keys) ⇒ Object
- #write(key, value, options = {}) ⇒ Object
Constructor Details
#initialize(url, options = {}) ⇒ Cache
Creates a new Readthis::Cache object with the given redis URL. The URL is parsed by the redis client directly.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/readthis/cache.rb', line 43 def initialize(url, = {}) @expires_in = .fetch(:expires_in, nil) @namespace = .fetch(:namespace, nil) @entity = Readthis::Entity.new( marshal: .fetch(:marshal, Marshal), compress: .fetch(:compress, false), threshold: .fetch(:compression_threshold, 1024) ) @pool = ConnectionPool.new(()) do Redis.new(url: url, driver: :hiredis) end end |
Instance Attribute Details
#entity ⇒ Object (readonly)
Returns the value of attribute entity.
11 12 13 |
# File 'lib/readthis/cache.rb', line 11 def entity @entity end |
#expires_in ⇒ Object (readonly)
Returns the value of attribute expires_in.
11 12 13 |
# File 'lib/readthis/cache.rb', line 11 def expires_in @expires_in end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
11 12 13 |
# File 'lib/readthis/cache.rb', line 11 def namespace @namespace end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
11 12 13 |
# File 'lib/readthis/cache.rb', line 11 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.
17 18 19 20 21 22 23 |
# File 'lib/readthis/cache.rb', line 17 def self.notifications if Object.const_defined?('ActiveSupport::Notifications') ActiveSupport::Notifications else Readthis::Notifications end end |
Instance Method Details
#clear ⇒ Object
193 194 195 |
# File 'lib/readthis/cache.rb', line 193 def clear invoke(:clear, '*', &:flushdb) end |
#decrement(key, amount = 1, options = {}) ⇒ Object
Decrement a key in the store.
If the key doesn’t exist it will be initialized at 0. If the key exists but it isn’t a Fixnum it will be initialized at 0.
144 145 146 147 148 |
# File 'lib/readthis/cache.rb', line 144 def decrement(key, amount = 1, = {}) invoke(:decrement, key) do |store| alter(key, amount * -1, ) end end |
#delete(key, options = {}) ⇒ Object
91 92 93 94 95 |
# File 'lib/readthis/cache.rb', line 91 def delete(key, = {}) invoke(:delete, key) do |store| store.del(namespaced_key(key, ())) end end |
#exist?(key, options = {}) ⇒ Boolean
187 188 189 190 191 |
# File 'lib/readthis/cache.rb', line 187 def exist?(key, = {}) invoke(:exist?, key) do |store| store.exists(namespaced_key(key, ())) end end |
#fetch(key, options = {}) ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/readthis/cache.rb', line 97 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
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/readthis/cache.rb', line 168 def fetch_multi(*keys) results = read_multi(*keys) = ((keys)) invoke(:fetch_multi, keys) 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 results end end |
#increment(key, amount = 1, options = {}) ⇒ Object
Increment a key in the store.
If the key doesn’t exist it will be initialized at 0. If the key exists but it isn’t a Fixnum it will be initialized at 0.
123 124 125 126 127 |
# File 'lib/readthis/cache.rb', line 123 def increment(key, amount = 1, = {}) invoke(:incremenet, key) do |store| alter(key, amount, ) end end |
#read(key, options = {}) ⇒ Object
Fetches data from the cache, using the given key. If there is data in the cache with the given key, then that data is returned. Otherwise, nil is returned.
70 71 72 73 74 75 76 |
# File 'lib/readthis/cache.rb', line 70 def read(key, = {}) invoke(:read, key) do |store| value = store.get(namespaced_key(key, ())) entity.load(value) end end |
#read_multi(*keys) ⇒ Object
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/readthis/cache.rb', line 150 def read_multi(*keys) = ((keys)) mapping = keys.map { |key| namespaced_key(key, ) } invoke(:read_multi, keys) do |store| values = store.mget(mapping).map { |value| entity.load(value) } keys.zip(values).to_h end end |
#write(key, value, options = {}) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/readthis/cache.rb', line 78 def write(key, value, = {}) = () namespaced = namespaced_key(key, ) invoke(:write, key) do |store| if expiration = [:expires_in] store.setex(namespaced, expiration, entity.dump(value)) else store.set(namespaced, entity.dump(value)) end end end |