redis-namespace 1.8.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 07470c91421a4a15227f10daff0378fdd85a7ba7abc30357677cca67ca87305f
4
- data.tar.gz: 50e2b6ff0b1db22abe4bbfd77e486d9fd71c09122ba3c01bbfbf90710d21353d
3
+ metadata.gz: 88bd88ddb249a9424c8bcc002b84f097a96b4de6450944bc66096e6524115431
4
+ data.tar.gz: 1b9b4d59744b77a65ec523a77ea438de1f0923494a838c94a8c312893dce1fa6
5
5
  SHA512:
6
- metadata.gz: ec82e340922480f5a48a8bba472babc3385abc84104b56d13f9e94a2654c5a4aaef1d38f1c2a11535a8f5c5ca02aed3ab1c157fb983b7251458f6b8547b08710
7
- data.tar.gz: 129b38893696d94b57aa04d07fe399e59d76fd6b4269531c7dbee9334604041b9a1fca931ff7a4a8e16dee9b57f9756e26a6cf1b3d9f989139dbcd31f6d5b0ce
6
+ metadata.gz: 2007d2f5396aec757c50d77de437dc01630b10de02eaeb852f7858233729476582036cf8214ba6e893f3e26b574c22169530a44bf9fb24c39fa2f2d5958e7ee5
7
+ data.tar.gz: af47dc4b2ce8357f8751c99ed71d05dc213a75a8092a3d54f016cef36b327967445b915bfaed32565639b0b2c16dbd02ef0026fbabcbf07c8965d3bb42b04f16
data/README.md CHANGED
@@ -33,6 +33,13 @@ redis_connection.get('ns:foo')
33
33
  # => nil
34
34
  ```
35
35
 
36
+ Redis::Namespace also supports `Proc` as a namespace and will take the result string as namespace at runtime.
37
+
38
+ ```ruby
39
+ redis_connection = Redis.new
40
+ namespaced_redis = Redis::Namespace.new(Proc.new { Tenant.current_tenant }, redis: redis_connection)
41
+ ```
42
+
36
43
  Installation
37
44
  ============
38
45
 
@@ -79,7 +86,7 @@ namespaced.redis.flushall()
79
86
 
80
87
  As mentioned above, 2.0 will remove blind passthrough and the administrative command passthrough.
81
88
  By default in 1.5+, deprecation warnings are present and enabled;
82
- they can be silenced by initializing `Redis::Namespace` with `warnings: false` or by setting the `REDIS_NAMESPACE_QUIET` environment variable.
89
+ they can be silenced by initializing `Redis::Namespace` with `warning: false` or by setting the `REDIS_NAMESPACE_QUIET` environment variable.
83
90
 
84
91
  Early opt-in
85
92
  ------------
@@ -2,6 +2,6 @@
2
2
 
3
3
  class Redis
4
4
  class Namespace
5
- VERSION = '1.8.0'
5
+ VERSION = '1.9.0'
6
6
  end
7
7
  end
@@ -75,6 +75,7 @@ class Redis
75
75
  "eval" => [ :eval_style ],
76
76
  "evalsha" => [ :eval_style ],
77
77
  "get" => [ :first ],
78
+ "getex" => [ :first ],
78
79
  "getbit" => [ :first ],
79
80
  "getrange" => [ :first ],
80
81
  "getset" => [ :first ],
@@ -151,6 +152,7 @@ class Redis
151
152
  "sinterstore" => [ :all ],
152
153
  "sismember" => [ :first ],
153
154
  "smembers" => [ :first ],
155
+ "smismember" => [ :first ],
154
156
  "smove" => [ :exclude_last ],
155
157
  "sort" => [ :sort ],
156
158
  "spop" => [ :first ],
@@ -242,7 +244,7 @@ class Redis
242
244
 
243
245
  def initialize(namespace, options = {})
244
246
  @namespace = namespace
245
- @redis = options[:redis] || Redis.current
247
+ @redis = options[:redis] || Redis.new
246
248
  @warning = !!options.fetch(:warning) do
247
249
  !ENV['REDIS_NAMESPACE_QUIET']
248
250
  end
@@ -261,7 +263,7 @@ class Redis
261
263
  end
262
264
 
263
265
  def client
264
- warn("The client method is deprecated as of redis-rb 4.0.0, please use the new _client" +
266
+ warn("The client method is deprecated as of redis-rb 4.0.0, please use the new _client " +
265
267
  "method instead. Support for the old method will be removed in redis-namespace 2.0.") if @has_new_client_method && deprecations?
266
268
  _client
267
269
  end
@@ -307,7 +309,7 @@ class Redis
307
309
  :redis => @redis)
308
310
  end
309
311
 
310
- @namespace
312
+ @namespace.respond_to?(:call) ? @namespace.call : @namespace
311
313
  end
312
314
 
313
315
  def full_namespace
@@ -315,7 +317,7 @@ class Redis
315
317
  end
316
318
 
317
319
  def connection
318
- @redis.connection.tap { |info| info[:namespace] = @namespace }
320
+ @redis.connection.tap { |info| info[:namespace] = namespace }
319
321
  end
320
322
 
321
323
  def exec
@@ -430,6 +432,7 @@ class Redis
430
432
  args = add_namespace(args)
431
433
  end
432
434
  when :alternate
435
+ args = args.flatten
433
436
  args.each_with_index { |a, i| args[i] = add_namespace(a) if i.even? }
434
437
  when :sort
435
438
  args[0] = add_namespace(args[0]) if args[0]
@@ -492,6 +495,12 @@ class Redis
492
495
  end
493
496
  ruby2_keywords(:call_with_namespace) if respond_to?(:ruby2_keywords, true)
494
497
 
498
+ protected
499
+
500
+ def redis=(redis)
501
+ @redis = redis
502
+ end
503
+
495
504
  private
496
505
 
497
506
  if Hash.respond_to?(:ruby2_keywords_hash)
@@ -520,18 +529,19 @@ class Redis
520
529
  end
521
530
 
522
531
  def namespaced_block(command, &block)
523
- redis.send(command) do |r|
524
- begin
525
- original, @redis = @redis, r
526
- yield self
527
- ensure
528
- @redis = original
532
+ if block.arity == 0
533
+ redis.send(command, &block)
534
+ else
535
+ redis.send(command) do |r|
536
+ copy = dup
537
+ copy.redis = r
538
+ yield copy
529
539
  end
530
540
  end
531
541
  end
532
542
 
533
543
  def add_namespace(key)
534
- return key unless key && @namespace
544
+ return key unless key && namespace
535
545
 
536
546
  case key
537
547
  when Array
@@ -540,12 +550,12 @@ class Redis
540
550
  key.keys.each {|k| key[add_namespace(k)] = key.delete(k)}
541
551
  key
542
552
  else
543
- "#{@namespace}:#{key}"
553
+ "#{namespace}:#{key}"
544
554
  end
545
555
  end
546
556
 
547
557
  def rem_namespace(key)
548
- return key unless key && @namespace
558
+ return key unless key && namespace
549
559
 
550
560
  case key
551
561
  when Array
@@ -557,7 +567,7 @@ class Redis
557
567
  key.each { |k| yielder.yield rem_namespace(k) }
558
568
  end
559
569
  else
560
- key.to_s.sub(/\A#{@namespace}:/, '')
570
+ key.to_s.sub(/\A#{namespace}:/, '')
561
571
  end
562
572
  end
563
573
 
data/spec/redis_spec.rb CHANGED
@@ -3,28 +3,17 @@
3
3
  require File.dirname(__FILE__) + '/spec_helper'
4
4
 
5
5
  describe "redis" do
6
- @redis_version = Gem::Version.new(Redis.current.info["redis_version"])
6
+ @redis_version = Gem::Version.new(Redis.new.info["redis_version"])
7
7
  let(:redis_client) { @redis.respond_to?(:_client) ? @redis._client : @redis.client}
8
8
 
9
- before(:all) do
9
+ before(:each) do
10
10
  # use database 15 for testing so we dont accidentally step on your real data
11
11
  @redis = Redis.new :db => 15
12
- end
13
-
14
- before(:each) do
15
- @namespaced = Redis::Namespace.new(:ns, :redis => @redis)
16
12
  @redis.flushdb
13
+ @namespaced = Redis::Namespace.new(:ns, :redis => @redis)
17
14
  @redis.set('foo', 'bar')
18
15
  end
19
16
 
20
- after(:each) do
21
- @redis.flushdb
22
- end
23
-
24
- after(:all) do
25
- @redis.quit
26
- end
27
-
28
17
  # redis-rb 3.3.4+
29
18
  it "should inject :namespace into connection info" do
30
19
  info = @redis.connection.merge(:namespace => :ns)
@@ -52,6 +41,17 @@ describe "redis" do
52
41
  expect(@namespaced.type('counter')).to eq('string')
53
42
  end
54
43
 
44
+ it "should work with Proc namespaces" do
45
+ namespace = Proc.new { :dynamic_ns }
46
+ namespaced = Redis::Namespace.new(namespace, redis: @redis)
47
+
48
+ expect(namespaced.get('foo')).to eq(nil)
49
+ namespaced.set('foo', 'chris')
50
+ expect(namespaced.get('foo')).to eq('chris')
51
+ @redis.set('foo', 'bob')
52
+ expect(@redis.get('foo')).to eq('bob')
53
+ end
54
+
55
55
  context 'when sending capital commands (issue 68)' do
56
56
  it 'should be able to use a namespace' do
57
57
  @namespaced.send('SET', 'fubar', 'quux')
@@ -106,6 +106,13 @@ describe "redis" do
106
106
  expect(@namespaced.lrange('bar',0,-1)).to eq(['bar'])
107
107
  end
108
108
 
109
+ it "should be able to use a namespace with getex" do
110
+ expect(@namespaced.set('mykey', 'Hello')).to eq('OK')
111
+ expect(@namespaced.getex('mykey', ex: 50)).to eq('Hello')
112
+ expect(@namespaced.get('mykey')).to eq('Hello')
113
+ expect(@namespaced.ttl('mykey')).to eq(50)
114
+ end
115
+
109
116
  it 'should be able to use a namespace with getbit' do
110
117
  @namespaced.set('foo','bar')
111
118
  expect(@namespaced.getbit('foo',1)).to eq(1)
@@ -181,15 +188,22 @@ describe "redis" do
181
188
  @namespaced.mset('foo', '1000', 'bar', '2000')
182
189
  expect(@namespaced.mapped_mget('foo', 'bar')).to eq({ 'foo' => '1000', 'bar' => '2000' })
183
190
  expect(@namespaced.mapped_mget('foo', 'baz', 'bar')).to eq({ 'foo' => '1000', 'bar' => '2000', 'baz' => nil})
191
+
184
192
  @namespaced.mapped_mset('foo' => '3000', 'bar' => '5000')
185
193
  expect(@namespaced.mapped_mget('foo', 'bar')).to eq({ 'foo' => '3000', 'bar' => '5000' })
186
194
  expect(@namespaced.mapped_mget('foo', 'baz', 'bar')).to eq({ 'foo' => '3000', 'bar' => '5000', 'baz' => nil})
195
+
196
+ @namespaced.mset(['foo', '4000'], ['baz', '6000'])
197
+ expect(@namespaced.mapped_mget('foo', 'bar', 'baz')).to eq({ 'foo' => '4000', 'bar' => '5000', 'baz' => '6000' })
187
198
  end
188
199
 
189
200
  it "should be able to use a namespace with msetnx" do
190
201
  @namespaced.msetnx('foo', '1000', 'bar', '2000')
191
202
  expect(@namespaced.mapped_mget('foo', 'bar')).to eq({ 'foo' => '1000', 'bar' => '2000' })
192
203
  expect(@namespaced.mapped_mget('foo', 'baz', 'bar')).to eq({ 'foo' => '1000', 'bar' => '2000', 'baz' => nil})
204
+
205
+ @namespaced.msetnx(['baz', '4000'])
206
+ expect(@namespaced.mapped_mget('foo', 'baz', 'bar')).to eq({ 'foo' => '1000', 'bar' => '2000', 'baz' => '4000'})
193
207
  end
194
208
 
195
209
  it "should be able to use a namespace with mapped_msetnx" do
@@ -398,6 +412,27 @@ describe "redis" do
398
412
  expect(result).to eq(["bar", "value"])
399
413
  end
400
414
 
415
+ it "is thread safe for multi blocks" do
416
+ mon = Monitor.new
417
+ entered = false
418
+ entered_cond = mon.new_cond
419
+
420
+ thread = Thread.new do
421
+ mon.synchronize do
422
+ entered_cond.wait_until { entered }
423
+ @namespaced.multi
424
+ end
425
+ end
426
+
427
+ @namespaced.multi do |transaction|
428
+ entered = true
429
+ mon.synchronize { entered_cond.signal }
430
+ thread.join(0.1)
431
+ transaction.get("foo")
432
+ end
433
+ thread.join
434
+ end
435
+
401
436
  it "should add namespace to strlen" do
402
437
  @namespaced.set("mykey", "123456")
403
438
  expect(@namespaced.strlen("mykey")).to eq(6)
@@ -677,7 +712,7 @@ describe "redis" do
677
712
  expect(result).to match_array(namespaced_keys)
678
713
  end
679
714
  end
680
- end if Redis.current.respond_to?(:scan)
715
+ end if Redis.new.respond_to?(:scan)
681
716
 
682
717
  context '#scan_each' do
683
718
  context 'when :match supplied' do
@@ -710,7 +745,7 @@ describe "redis" do
710
745
  end
711
746
  end
712
747
  end
713
- end if Redis.current.respond_to?(:scan_each)
748
+ end if Redis.new.respond_to?(:scan_each)
714
749
  end
715
750
 
716
751
  context 'hash scan methods' do
@@ -738,7 +773,7 @@ describe "redis" do
738
773
  expect(results).to match_array(@redis.hgetall('ns:hsh').to_a)
739
774
  end
740
775
  end
741
- end if Redis.current.respond_to?(:hscan)
776
+ end if Redis.new.respond_to?(:hscan)
742
777
 
743
778
  context '#hscan_each' do
744
779
  context 'when :match supplied' do
@@ -771,7 +806,7 @@ describe "redis" do
771
806
  end
772
807
  end
773
808
  end
774
- end if Redis.current.respond_to?(:hscan_each)
809
+ end if Redis.new.respond_to?(:hscan_each)
775
810
  end
776
811
 
777
812
  context 'set scan methods' do
@@ -799,7 +834,7 @@ describe "redis" do
799
834
  expect(results).to match_array(set)
800
835
  end
801
836
  end
802
- end if Redis.current.respond_to?(:sscan)
837
+ end if Redis.new.respond_to?(:sscan)
803
838
 
804
839
  context '#sscan_each' do
805
840
  context 'when :match supplied' do
@@ -832,7 +867,7 @@ describe "redis" do
832
867
  end
833
868
  end
834
869
  end
835
- end if Redis.current.respond_to?(:sscan_each)
870
+ end if Redis.new.respond_to?(:sscan_each)
836
871
  end
837
872
 
838
873
  context 'zset scan methods' do
@@ -862,7 +897,7 @@ describe "redis" do
862
897
  expect(results).to match_array(hash.to_a)
863
898
  end
864
899
  end
865
- end if Redis.current.respond_to?(:zscan)
900
+ end if Redis.new.respond_to?(:zscan)
866
901
 
867
902
  context '#zscan_each' do
868
903
  context 'when :match supplied' do
@@ -895,7 +930,7 @@ describe "redis" do
895
930
  end
896
931
  end
897
932
  end
898
- end if Redis.current.respond_to?(:zscan_each)
933
+ end if Redis.new.respond_to?(:zscan_each)
899
934
  end
900
935
  end
901
936
  end
metadata CHANGED
@@ -1,17 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-namespace
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.0
4
+ version: 1.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Wanstrath
8
8
  - Terence Lee
9
9
  - Steve Klabnik
10
10
  - Ryan Biesemeyer
11
- autorequire:
11
+ - Mike Bianco
12
+ autorequire:
12
13
  bindir: bin
13
14
  cert_chain: []
14
- date: 2020-08-19 00:00:00.000000000 Z
15
+ date: 2022-08-13 00:00:00.000000000 Z
15
16
  dependencies:
16
17
  - !ruby/object:Gem::Dependency
17
18
  name: redis
@@ -19,14 +20,14 @@ dependencies:
19
20
  requirements:
20
21
  - - ">="
21
22
  - !ruby/object:Gem::Version
22
- version: 3.0.4
23
+ version: '4'
23
24
  type: :runtime
24
25
  prerelease: false
25
26
  version_requirements: !ruby/object:Gem::Requirement
26
27
  requirements:
27
28
  - - ">="
28
29
  - !ruby/object:Gem::Version
29
- version: 3.0.4
30
+ version: '4'
30
31
  - !ruby/object:Gem::Dependency
31
32
  name: rake
32
33
  requirement: !ruby/object:Gem::Requirement
@@ -78,6 +79,7 @@ email:
78
79
  - hone02@gmail.com
79
80
  - steve@steveklabnik.com
80
81
  - me@yaauie.com
82
+ - mike@mikebian.co
81
83
  executables: []
82
84
  extensions: []
83
85
  extra_rdoc_files: []
@@ -97,15 +99,16 @@ licenses:
97
99
  metadata:
98
100
  bug_tracker_uri: https://github.com/resque/redis-namespace/issues
99
101
  changelog_uri: https://github.com/resque/redis-namespace/blob/master/CHANGELOG.md
100
- documentation_uri: https://www.rubydoc.info/gems/redis-namespace/1.8.0
101
- source_code_uri: https://github.com/resque/redis-namespace/tree/v1.8.0
102
- post_install_message:
102
+ documentation_uri: https://www.rubydoc.info/gems/redis-namespace/1.9.0
103
+ source_code_uri: https://github.com/resque/redis-namespace/tree/v1.9.0
104
+ rubygems_mfa_required: 'true'
105
+ post_install_message:
103
106
  rdoc_options: []
104
107
  require_paths:
105
108
  - lib
106
109
  required_ruby_version: !ruby/object:Gem::Requirement
107
110
  requirements:
108
- - - "~>"
111
+ - - ">="
109
112
  - !ruby/object:Gem::Version
110
113
  version: '2.4'
111
114
  required_rubygems_version: !ruby/object:Gem::Requirement
@@ -114,8 +117,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
117
  - !ruby/object:Gem::Version
115
118
  version: '0'
116
119
  requirements: []
117
- rubygems_version: 3.1.2
118
- signing_key:
120
+ rubygems_version: 3.1.6
121
+ signing_key:
119
122
  specification_version: 4
120
123
  summary: Namespaces Redis commands.
121
124
  test_files: []