Class: Readthis::Cache
- Inherits:
-
Object
- Object
- Readthis::Cache
- Defined in:
- lib/readthis/cache.rb
Instance Attribute Summary collapse
-
#compress ⇒ Object
(also: #compress?)
readonly
Returns the value of attribute compress.
-
#compression_threshold ⇒ Object
readonly
Returns the value of attribute compression_threshold.
-
#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
Creates a new Readthis::Cache object with the given redis URL. The URL is parsed by the redis client directly.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/readthis/cache.rb', line 45 def initialize(url, = {}) @expires_in = .fetch(:expires_in, nil) @namespace = .fetch(:namespace, nil) @compress = .fetch(:compress, false) @compression_threshold = .fetch(:compression_threshold, 1024) @pool = ConnectionPool.new(()) do Redis.new(url: url, driver: :hiredis) end end |
Instance Attribute Details
#compress ⇒ Object (readonly) Also known as: compress?
Returns the value of attribute compress.
10 11 12 |
# File 'lib/readthis/cache.rb', line 10 def compress @compress end |
#compression_threshold ⇒ Object (readonly)
Returns the value of attribute compression_threshold.
10 11 12 |
# File 'lib/readthis/cache.rb', line 10 def compression_threshold @compression_threshold end |
#expires_in ⇒ Object (readonly)
Returns the value of attribute expires_in.
10 11 12 |
# File 'lib/readthis/cache.rb', line 10 def expires_in @expires_in end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
10 11 12 |
# File 'lib/readthis/cache.rb', line 10 def namespace @namespace end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
10 11 12 |
# File 'lib/readthis/cache.rb', line 10 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.
22 23 24 25 26 27 28 |
# File 'lib/readthis/cache.rb', line 22 def self.notifications if Object.const_defined?('ActiveSupport::Notifications') ActiveSupport::Notifications else Readthis::Notifications end end |
Instance Method Details
#clear ⇒ Object
149 150 151 |
# File 'lib/readthis/cache.rb', line 149 def clear invoke(:clear, '*', &:flushdb) end |
#decrement(key, options = {}) ⇒ Object
100 101 102 103 104 |
# File 'lib/readthis/cache.rb', line 100 def decrement(key, = {}) invoke(:decrement, key) do |store| store.decr(namespaced_key(key, ())) end end |
#delete(key, options = {}) ⇒ Object
77 78 79 80 81 |
# File 'lib/readthis/cache.rb', line 77 def delete(key, = {}) invoke(:delete, key) do |store| store.del(namespaced_key(key, ())) end end |
#exist?(key, options = {}) ⇒ Boolean
143 144 145 146 147 |
# File 'lib/readthis/cache.rb', line 143 def exist?(key, = {}) invoke(:exist?, key) do |store| store.exists(namespaced_key(key, ())) end end |
#fetch(key, options = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/readthis/cache.rb', line 83 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
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/readthis/cache.rb', line 124 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, options = {}) ⇒ Object
94 95 96 97 98 |
# File 'lib/readthis/cache.rb', line 94 def increment(key, = {}) invoke(:incremenet, key) do |store| store.incr(namespaced_key(key, ())) end end |
#read(key, options = {}) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/readthis/cache.rb', line 56 def read(key, = {}) invoke(:read, key) do |store| value = store.get(namespaced_key(key, ())) decompressed(value) end end |
#read_multi(*keys) ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/readthis/cache.rb', line 106 def read_multi(*keys) = ((keys)) mapping = keys.map { |key| namespaced_key(key, ) } invoke(:read_multi, keys) do |store| values = decompressed_multi(store.mget(mapping)) keys.zip(values).to_h end end |
#write(key, value, options = {}) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/readthis/cache.rb', line 64 def write(key, value, = {}) = () namespaced = namespaced_key(key, ) invoke(:write, key) do |store| if expiration = [:expires_in] store.setex(namespaced, expiration, compressed(value)) else store.set(namespaced, compressed(value)) end end end |