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.
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
This must be done in two separate blocks.
- #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
Creates a new Readthis::Cache object with the given redis URL. The URL is parsed by the redis client directly.
10 11 12 13 14 15 16 17 |
# File 'lib/readthis/cache.rb', line 10 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.
6 7 8 |
# File 'lib/readthis/cache.rb', line 6 def expires_in @expires_in end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
6 7 8 |
# File 'lib/readthis/cache.rb', line 6 def namespace @namespace end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
6 7 8 |
# File 'lib/readthis/cache.rb', line 6 def pool @pool end |
Instance Method Details
#clear ⇒ Object
107 108 109 110 111 |
# File 'lib/readthis/cache.rb', line 107 def clear with do |store| store.flushdb end end |
#decrement(key, options = {}) ⇒ Object
61 62 63 64 65 |
# File 'lib/readthis/cache.rb', line 61 def decrement(key, = {}) with do |store| store.decr(namespaced_key(key, ())) end end |
#delete(key, options = {}) ⇒ Object
38 39 40 41 42 |
# File 'lib/readthis/cache.rb', line 38 def delete(key, = {}) with do |store| store.del(namespaced_key(key, ())) end end |
#exist?(key, options = {}) ⇒ Boolean
101 102 103 104 105 |
# File 'lib/readthis/cache.rb', line 101 def exist?(key, = {}) with do |store| store.exists(namespaced_key(key, ())) end end |
#fetch(key, options = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/readthis/cache.rb', line 44 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
This must be done in two separate blocks. Redis pipelines return futures, which can not be resolved until the pipeline has exited.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/readthis/cache.rb', line 82 def fetch_multi(*keys) results = read_multi(*keys) = ((keys)) 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 |
#increment(key, options = {}) ⇒ Object
55 56 57 58 59 |
# File 'lib/readthis/cache.rb', line 55 def increment(key, = {}) with do |store| store.incr(namespaced_key(key, ())) end end |
#read(key, options = {}) ⇒ Object
19 20 21 22 23 |
# File 'lib/readthis/cache.rb', line 19 def read(key, = {}) with do |store| store.get(namespaced_key(key, ())) end end |
#read_multi(*keys) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/readthis/cache.rb', line 67 def read_multi(*keys) = ((keys)) results = [] with do |store| results = store.pipelined do keys.each { |key| store.get(namespaced_key(key, )) } end end keys.zip(results).to_h end |
#write(key, value, options = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/readthis/cache.rb', line 25 def write(key, value, = {}) = () namespaced = namespaced_key(key, ) with do |store| if expiration = [:expires_in] store.setex(namespaced, expiration, value) else store.set(namespaced, value) end end end |