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.
-
#notifications ⇒ Object
readonly
Returns the value of attribute notifications.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
-
#scripts ⇒ Object
readonly
Returns the value of attribute scripts.
Class Method Summary collapse
-
.notifications ⇒ Object
Provide a class level lookup of the proper notifications module.
Instance Method Summary collapse
-
#clear(_options = nil) ⇒ Object
Clear the entire cache.
-
#decrement(key, amount = 1, options = {}) ⇒ Object
Decrement a key in the store.
-
#delete(key, options = {}) ⇒ Object
Delete the value stored at the specified key.
-
#delete_matched(pattern, options = {}) ⇒ Object
Delete all values that match a given pattern.
-
#exist?(key, options = {}) ⇒ Boolean
Returns ‘true` if the cache contains an entry for the given key.
-
#fetch(key, options = {}) {|String| ... } ⇒ Object
Fetches data from the cache, using the given key.
-
#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(options = {}) ⇒ Cache
constructor
Creates a new Readthis::Cache object with the given options.
-
#read(key, options = {}) ⇒ Object
Fetches data from the cache, using the given key.
-
#read_multi(keys) ⇒ Hash
Efficiently read multiple values at once from the cache.
-
#write(key, value, options = {}) ⇒ Object
Writes data to the cache using the given key.
-
#write_multi(hash, options = {}) ⇒ Object
Write multiple key value pairs simultaneously.
Constructor Details
#initialize(options = {}) ⇒ Cache
Creates a new Readthis::Cache object with the given options.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/readthis/cache.rb', line 50 def initialize( = {}) @options = @entity = Readthis::Entity.new( marshal: .fetch(:marshal, Marshal), compress: .fetch(:compress, false), threshold: .fetch(:compression_threshold, 1024) ) @pool = ConnectionPool.new(()) do Redis.new(.fetch(:redis, {})) end @scripts = Readthis::Scripts.new end |
Instance Attribute Details
#entity ⇒ Object (readonly)
Returns the value of attribute entity.
10 11 12 |
# File 'lib/readthis/cache.rb', line 10 def entity @entity end |
#notifications ⇒ Object (readonly)
Returns the value of attribute notifications.
10 11 12 |
# File 'lib/readthis/cache.rb', line 10 def notifications @notifications end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/readthis/cache.rb', line 10 def @options end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
10 11 12 |
# File 'lib/readthis/cache.rb', line 10 def pool @pool end |
#scripts ⇒ Object (readonly)
Returns the value of attribute scripts.
10 11 12 |
# File 'lib/readthis/cache.rb', line 10 def scripts @scripts 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.
16 17 18 |
# File 'lib/readthis/cache.rb', line 16 def self.notifications ActiveSupport::Notifications if defined?(ActiveSupport::Notifications) end |
Instance Method Details
#clear(_options = nil) ⇒ Object
Clear the entire cache. This flushes the current database, no globbing is applied.
377 378 379 |
# File 'lib/readthis/cache.rb', line 377 def clear( = nil) 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.
254 255 256 257 258 |
# File 'lib/readthis/cache.rb', line 254 def decrement(key, amount = 1, = {}) invoke(:decrement, key) do |_store| alter(key, amount * -1, ) end end |
#delete(key, options = {}) ⇒ Object
Delete the value stored at the specified key. Returns ‘true` if anything was deleted, `false` otherwise.
121 122 123 124 125 126 127 |
# File 'lib/readthis/cache.rb', line 121 def delete(key, = {}) namespaced = namespaced_key(key, ()) invoke(:delete, key) do |store| store.del(namespaced) > 0 end end |
#delete_matched(pattern, options = {}) ⇒ Object
Delete all values that match a given pattern. The pattern must be defined using Redis compliant globs. The following examples are borrowed from the ‘KEYS` documentation:
-
‘h?llo` matches hello, hallo and hxllo
-
‘h*llo` matches hllo and heeeello
-
‘hllo` matches hello and hallo, but not hillo
-
‘hllo` matches hallo, hbllo, … but not hello
-
‘hllo` matches hallo and hbllo
Note that ‘delete_matched` does not use the `KEYS` command, making it safe for use in production.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/readthis/cache.rb', line 151 def delete_matched(pattern, = {}) namespaced = namespaced_key(pattern, ()) invoke(:delete, pattern) do |store| cursor = nil count = .fetch(:count, 1000) deleted = 0 until cursor == '0'.freeze cursor, matched = store.scan(cursor || 0, match: namespaced, count: count) if matched.any? store.del(*matched) deleted += matched.length end end deleted end end |
#exist?(key, options = {}) ⇒ Boolean
Returns ‘true` if the cache contains an entry for the given key.
362 363 364 365 366 |
# File 'lib/readthis/cache.rb', line 362 def exist?(key, = {}) invoke(:exist?, key) do |store| store.exists(namespaced_key(key, ())) end end |
#fetch(key, options = {}) {|String| ... } ⇒ 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.
If there is no such data in the cache (a cache miss), then ‘nil` will be returned. However, if a block has been passed, that block will be passed the key and executed in the event of a cache miss. The return value of the block will be written to the cache under the given cache key, and that return value will be returned.
206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/readthis/cache.rb', line 206 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.
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/readthis/cache.rb', line 332 def fetch_multi(*keys) = (keys).merge(retain_nils: true) results = read_multi(*keys, ) missing = {} invoke(:fetch_multi, keys) do |_store| results.each do |key, value| next unless value.nil? value = yield(key) missing[key] = value results[key] = value end end write_multi(missing, ) if missing.any? results 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.
233 234 235 236 237 |
# File 'lib/readthis/cache.rb', line 233 def increment(key, amount = 1, = {}) invoke(:increment, 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.
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/readthis/cache.rb', line 78 def read(key, = {}) = () invoke(:read, key) do |store| key = namespaced_key(key, ) refresh_entity(key, store, ) entity.load(store.get(key)) end end |
#read_multi(keys) ⇒ Hash
Efficiently read multiple values at once from the cache. Options can be passed in the last argument.
275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/readthis/cache.rb', line 275 def read_multi(*keys) = ((keys)) mapping = keys.map { |key| namespaced_key(key, ) } return {} if keys.empty? invoke(:read_multi, keys) do |store| values = store.mget(*mapping).map { |value| entity.load(value) } refresh_entity(mapping, store, ) zipped_results(keys, values, ) end end |
#write(key, value, options = {}) ⇒ Object
Writes data to the cache using the given key. Will overwrite whatever value is already stored at that key.
102 103 104 105 106 107 108 |
# File 'lib/readthis/cache.rb', line 102 def write(key, value, = {}) = () invoke(:write, key) do |store| write_entity(key, value, store, ) end end |
#write_multi(hash, options = {}) ⇒ Object
Write multiple key value pairs simultaneously. This is an atomic operation that will always succeed and will overwrite existing values.
This is a non-standard, but useful, cache method.
303 304 305 306 307 308 309 310 311 |
# File 'lib/readthis/cache.rb', line 303 def write_multi(hash, = {}) = () invoke(:write_multi, hash.keys) do |store| store.multi do hash.each { |key, value| write_entity(key, value, store, ) } end end end |