readthis 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f71b3b7fac83a8203aa382cb87a4684240d427df
4
- data.tar.gz: 8e87900ed9f5fb0199d20fb674ef7b8c988886b6
3
+ metadata.gz: 55fdafde7fa30d64c803d3952bf663d08c8c3f7a
4
+ data.tar.gz: 4f69451b0992644c1bf160bdd4d2a1b68bf3e495
5
5
  SHA512:
6
- metadata.gz: 47a9479f67d9550b32798f8122209c569281e16d1d85402cf47af4ae7676de0fc65e6d6f5ffa87c99cc18606774f9d30a4ced4f8867ede0bdf8bb38872fbc50e
7
- data.tar.gz: 2fe39b8a2f99bc1b4a55865cd114f49280311593ac157511874934f198f104b70ff04f3f4cdd90c1fd95794dd1b1d83965014db0afe69c60523fe7052e74549a
6
+ metadata.gz: 3602ac1b8a9296aef0c802b3cbd60b9689b496fe0efa9086024e4cbd69afa91dd59f57c1f0882ad4be933ddb678bd4039642dc679f55c65547366426d09f0cba
7
+ data.tar.gz: 121272db5ae46a67209ef7644bd2a352b197373222f608b5d53d981369f37fea410fd9d5e4a20e313432ac1dda12b95d7323cb606f6db44d169caed8405dc794
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## v0.6.0 2015-03-09
2
+
3
+ - Fixed: Safely handle calling `read_multi` without any keys. [Michael Rykov]
4
+ - Fixed: Pointed `redis-activesupport` at master. Only effected development and
5
+ testing.
6
+ - Added: A `write_multi` method is no available to bulk set keys and values. It
7
+ is used by `fetch_multi` internally to ensure that there are at most two Redis
8
+ calls.
9
+
1
10
  ## v0.5.2 2015-01-09
2
11
 
3
12
  - Fixed: Remove the `pipeline` around `fetch_multi` writing. This will slow down
data/Gemfile CHANGED
@@ -8,5 +8,5 @@ group :benchmarking do
8
8
  gem 'benchmark-ips'
9
9
  gem 'dalli'
10
10
  gem 'oj'
11
- gem 'redis-activesupport', github: 'sorentwo/redis-activesupport'
11
+ gem 'redis-activesupport'
12
12
  end
data/README.md CHANGED
@@ -80,16 +80,17 @@ Readthis uses Ruby's `Marshal` module for dumping and loading all values by
80
80
  default. This isn't always the fastest option, depending on your use case it may
81
81
  be desirable to use a faster but less flexible marshaller.
82
82
 
83
- Use Oj for JSON marshalling, extremely fast, limited types:
83
+ Use Oj for JSON marshalling, extremely fast, but supports limited types:
84
84
 
85
85
  ```ruby
86
- Readthis::Cache.new(marshal: Oj)
86
+ Readthis::Cache.new(url, marshal: Oj)
87
87
  ```
88
88
 
89
- If you don't mind everything being a string then use the Passthrough marshal:
89
+ If you don't mind everything handles as a string then use the pass-through
90
+ marshaller:
90
91
 
91
92
  ```ruby
92
- Readthis::Cache.new(marshal: Readthis::Passthrough)
93
+ Readthis::Cache.new(url, marshal: Readthis::Passthrough)
93
94
  ```
94
95
 
95
96
  ## Differences
@@ -216,6 +216,8 @@ module Readthis
216
216
  options = merged_options(extract_options!(keys))
217
217
  mapping = keys.map { |key| namespaced_key(key, options) }
218
218
 
219
+ return {} if keys.empty?
220
+
219
221
  invoke(:read_multi, keys) do |store|
220
222
  values = store.mget(mapping).map { |value| entity.load(value) }
221
223
 
@@ -223,6 +225,31 @@ module Readthis
223
225
  end
224
226
  end
225
227
 
228
+ # Write multiple key value pairs simultaneously. This is an atomic
229
+ # operation that will always succeed and will overwrite existing
230
+ # values.
231
+ #
232
+ # This is a non-standard, but useful, cache method.
233
+ #
234
+ # @param [Hash] Key value hash to write
235
+ # @param [Hash] Optional overrides
236
+ #
237
+ # @example
238
+ #
239
+ # cache.write_multi('a', 1, 'b', 2) # => true
240
+ #
241
+ def write_multi(hash, options = {})
242
+ options = merged_options(options)
243
+ values = hash.each_with_object([]) do |(key, value), memo|
244
+ memo << namespaced_key(key, options)
245
+ memo << entity.dump(value)
246
+ end
247
+
248
+ invoke(:write_multi, values) do |store|
249
+ store.mset(values)
250
+ end
251
+ end
252
+
226
253
  # Fetches multiple keys from the cache using a single call to the server
227
254
  # and filling in any cache misses. All read and write operations are
228
255
  # executed atomically.
@@ -243,20 +270,23 @@ module Readthis
243
270
  # end
244
271
  #
245
272
  def fetch_multi(*keys)
246
- results = read_multi(*keys)
247
- options = merged_options(extract_options!(keys))
273
+ results = read_multi(*keys)
274
+ extracted = extract_options!(keys)
275
+ missing = {}
248
276
 
249
277
  invoke(:fetch_multi, keys) do |store|
250
278
  results.each do |key, value|
251
279
  if value.nil?
252
280
  value = yield(key)
253
- write_entity(key, value, store, options)
281
+ missing[key] = value
254
282
  results[key] = value
255
283
  end
256
284
  end
257
-
258
- results
259
285
  end
286
+
287
+ write_multi(missing, extracted) if missing.any?
288
+
289
+ results
260
290
  end
261
291
 
262
292
  # Returns `true` if the cache contains an entry for the given key.
@@ -1,3 +1,3 @@
1
1
  module Readthis
2
- VERSION = '0.5.2'
2
+ VERSION = '0.6.0'
3
3
  end
@@ -144,6 +144,27 @@ RSpec.describe Readthis::Cache do
144
144
  'e' => 2,
145
145
  )
146
146
  end
147
+
148
+ it 'returns {} with no keys' do
149
+ expect(cache.read_multi(namespace: 'cache')).to eq({})
150
+ end
151
+ end
152
+
153
+ describe '#write_multi' do
154
+ it 'writes multiple key value pairs simultaneously' do
155
+ response = cache.write_multi('a' => 1, 'b' => 2)
156
+
157
+ expect(response).to be_truthy
158
+ expect(cache.read('a')).to eq(1)
159
+ expect(cache.read('b')).to eq(2)
160
+ end
161
+
162
+ it 'respects passed options' do
163
+ cache.write_multi({ 'a' => 1, 'b' => 2 }, namespace: 'multi')
164
+
165
+ expect(cache.read('a')).to be_nil
166
+ expect(cache.read('a', namespace: 'multi')).to eq(1)
167
+ end
147
168
  end
148
169
 
149
170
  describe '#fetch_multi' do
@@ -175,6 +196,11 @@ RSpec.describe Readthis::Cache do
175
196
  expect(cache.read('b')).to be_nil
176
197
  expect(cache.read('b', namespace: 'alph')).not_to be_nil
177
198
  end
199
+
200
+ it 'return empty results without keys' do
201
+ results = cache.fetch_multi(namespace: 'alph') { |key| key }
202
+ expect(results).to eq({})
203
+ end
178
204
  end
179
205
 
180
206
  describe '#exist?' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: readthis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Parker Selbert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-09 00:00:00.000000000 Z
11
+ date: 2015-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -151,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
151
  version: '0'
152
152
  requirements: []
153
153
  rubyforge_project:
154
- rubygems_version: 2.2.2
154
+ rubygems_version: 2.4.5
155
155
  signing_key:
156
156
  specification_version: 4
157
157
  summary: Pooled active support compliant caching with redis