redis-store 1.1.7 → 1.2.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.

Potentially problematic release.


This version of redis-store might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c34d0f4a72ee0603e5b2ef2b95808baac398b667
4
- data.tar.gz: e4805c9cd1bfd2cffc6504e36bddf00c80b0ce45
3
+ metadata.gz: fdc1ba61b75205e6f17d1bb790c3324d98335abb
4
+ data.tar.gz: 95cf5013e84b74ef20e9041c971cce5773411ec3
5
5
  SHA512:
6
- metadata.gz: 85aa71788df20715c5878ee1b13c047c5904a2b57fb30f4d69e9235dd61692b8f01671ac112f789f9ea6730a8db3cc188226c036188691a563df0e48964a6f90
7
- data.tar.gz: 9bebac3bac2a0ee4dc94db6f08325ce707a65ca289f36ad049769ed0cbc0ea6c6eba0f8ce8c0786560242f7b6ba683567743738c1d7897669b3909fffe854368
6
+ metadata.gz: 42f21ac1f963af2a5e96a145672ec97d670dc7bfe94dba63eeda69a1bd37567effb5f2d60a09d4f0871f9ded839318de092b88221189e955dd91191adb20c260
7
+ data.tar.gz: 488d11620f6f0c2a8ea615825056c199e0267386d374d9d91891c648fe1420739858c7f75cb3c1afafebabe22fa2ffdb99c7c77b33bd46def781a68709c6f15b
data/.travis.yml CHANGED
@@ -6,8 +6,9 @@ rvm:
6
6
  - 2.0.0
7
7
  - 2.1
8
8
  - 2.2
9
+ - 2.3.0
9
10
  - ruby-head
10
- - rbx-19mode
11
+ - rbx-2
11
12
  - jruby-19mode
12
13
  - jruby-head
13
14
  bundler_args: '--path vendor/bundle'
data/README.md CHANGED
@@ -21,7 +21,7 @@ Download and install Redis from [the download page](http://redis.io//download) a
21
21
  ## Running tests
22
22
 
23
23
  ```ruby
24
- git clone git://github.com/jodosha/redis-store.git
24
+ git clone git://github.com/redis-store/redis-store.git
25
25
  cd redis-store
26
26
  gem install bundler
27
27
  bundle exec rake
@@ -46,6 +46,10 @@ class Redis
46
46
  end
47
47
  end
48
48
 
49
+ def setex(key, expiry, value, options = nil)
50
+ node_for(key).setex(key, expiry, value, options)
51
+ end
52
+
49
53
  private
50
54
  def _extend_namespace(options)
51
55
  @namespace = options[:namespace]
@@ -6,7 +6,7 @@ class Redis
6
6
  end
7
7
 
8
8
  def set(key, value, options = nil)
9
- super(key, value)
9
+ super(key, value, options || {})
10
10
  end
11
11
 
12
12
  def setnx(key, value, options = nil)
@@ -2,15 +2,15 @@ class Redis
2
2
  class Store < self
3
3
  module Marshalling
4
4
  def set(key, value, options = nil)
5
- _marshal(value, options) { |value| super encode(key), encode(value), options }
5
+ _marshal(value, options) { |v| super encode(key), encode(v), options }
6
6
  end
7
7
 
8
8
  def setnx(key, value, options = nil)
9
- _marshal(value, options) { |value| super encode(key), encode(value), options }
9
+ _marshal(value, options) { |v| super encode(key), encode(v), options }
10
10
  end
11
11
 
12
12
  def setex(key, expiry, value, options = nil)
13
- _marshal(value, options) { |value| super encode(key), expiry, encode(value), options }
13
+ _marshal(value, options) { |v| super encode(key), expiry, encode(v), options }
14
14
  end
15
15
 
16
16
  def get(key, options = nil)
@@ -1,44 +1,46 @@
1
1
  class Redis
2
2
  class Store < self
3
3
  module Namespace
4
+ FLUSHDB_BATCH_SIZE = 1000
5
+
4
6
  def set(key, val, options = nil)
5
- namespace(key) { |key| super(key, val, options) }
7
+ namespace(key) { |k| super(k, val, options) }
6
8
  end
7
9
 
8
10
  def setex(key, ttl, val, options = nil)
9
- namespace(key) { |key| super(key, ttl, val, options) }
11
+ namespace(key) { |k| super(k, ttl, val, options) }
10
12
  end
11
13
 
12
14
  def setnx(key, val, options = nil)
13
- namespace(key) { |key| super(key, val, options) }
15
+ namespace(key) { |k| super(k, val, options) }
14
16
  end
15
17
 
16
18
  def ttl(key, options = nil)
17
- namespace(key) { |key| super(key) }
19
+ namespace(key) { |k| super(k) }
18
20
  end
19
21
 
20
22
  def get(key, options = nil)
21
- namespace(key) { |key| super(key, options) }
23
+ namespace(key) { |k| super(k, options) }
22
24
  end
23
25
 
24
26
  def exists(key)
25
- namespace(key) { |key| super(key) }
27
+ namespace(key) { |k| super(k) }
26
28
  end
27
29
 
28
30
  def incrby(key, increment)
29
- namespace(key) { |key| super(key, increment) }
31
+ namespace(key) { |k| super(k, increment) }
30
32
  end
31
33
 
32
34
  def decrby(key, increment)
33
- namespace(key) { |key| super(key, increment) }
35
+ namespace(key) { |k| super(k, increment) }
34
36
  end
35
37
 
36
38
  def keys(pattern = "*")
37
- namespace(pattern) { |pattern| super(pattern).map{|key| strip_namespace(key) } }
39
+ namespace(pattern) { |p| super(p).map{|key| strip_namespace(key) } }
38
40
  end
39
41
 
40
42
  def del(*keys)
41
- super *keys.map {|key| interpolate(key) } if keys.any?
43
+ super(*keys.map {|key| interpolate(key) }) if keys.any?
42
44
  end
43
45
 
44
46
  def mget(*keys)
@@ -46,27 +48,35 @@ class Redis
46
48
  if keys.any?
47
49
  # Marshalling gets extended before Namespace does, so we need to pass options further
48
50
  if singleton_class.ancestors.include? Marshalling
49
- super *keys.map {|key| interpolate(key) }, options
51
+ super(*keys.map {|key| interpolate(key) }, options)
50
52
  else
51
- super *keys.map {|key| interpolate(key) }
53
+ super(*keys.map {|key| interpolate(key) })
52
54
  end
53
55
  end
54
56
  end
55
-
57
+
56
58
  def expire(key, ttl)
57
- namespace(key) { |key| super(key, ttl) }
58
- end
59
-
60
- def ttl(key)
61
- namespace(key) { |key| super(key) }
59
+ namespace(key) { |k| super(k, ttl) }
62
60
  end
63
61
 
64
62
  def to_s
65
- "#{super} with namespace #{namespace_str}"
63
+ if namespace_str
64
+ "#{super} with namespace #{namespace_str}"
65
+ else
66
+ super
67
+ end
66
68
  end
67
69
 
68
70
  def flushdb
69
- del *keys
71
+ keys.each_slice(FLUSHDB_BATCH_SIZE) { |key_slice| del(*key_slice) }
72
+ end
73
+
74
+ def with_namespace(ns)
75
+ old_ns = @namespace
76
+ @namespace = ns
77
+ yield self
78
+ ensure
79
+ @namespace = old_ns
70
80
  end
71
81
 
72
82
  private
@@ -79,10 +89,12 @@ class Redis
79
89
  end
80
90
 
81
91
  def interpolate(key)
92
+ return key unless namespace_str
82
93
  key.match(namespace_regexp) ? key : "#{namespace_str}:#{key}"
83
94
  end
84
95
 
85
96
  def strip_namespace(key)
97
+ return key unless namespace_str
86
98
  key.gsub namespace_regexp, ""
87
99
  end
88
100
 
@@ -5,7 +5,7 @@ class Redis
5
5
  if ttl = expires_in(options)
6
6
  setex(key, ttl.to_i, value, :raw => true)
7
7
  else
8
- super(key, value)
8
+ super(key, value, options)
9
9
  end
10
10
  end
11
11
 
@@ -1,5 +1,5 @@
1
1
  class Redis
2
2
  class Store < self
3
- VERSION = '1.1.7'
3
+ VERSION = '1.2.0'
4
4
  end
5
5
  end
data/lib/redis/store.rb CHANGED
@@ -29,7 +29,7 @@ class Redis
29
29
 
30
30
  def _extend_namespace(options)
31
31
  @namespace = options[:namespace]
32
- extend Namespace if @namespace
32
+ extend Namespace
33
33
  end
34
34
  end
35
35
  end
@@ -67,6 +67,24 @@ describe "Redis::Marshalling" do
67
67
  @store.get("rabbit2").must_be_nil
68
68
  end
69
69
 
70
+ it "marshals setex (over a distributed store)" do
71
+ @store = Redis::DistributedStore.new [
72
+ {:host => "localhost", :port => "6380", :db => 0},
73
+ {:host => "localhost", :port => "6381", :db => 0}
74
+ ]
75
+ @store.setex "rabbit", 50, @white_rabbit
76
+ @store.get("rabbit").must_equal(@white_rabbit)
77
+ end
78
+
79
+ it "doesn't marshal setex if raw option is true (over a distributed store)" do
80
+ @store = Redis::DistributedStore.new [
81
+ {:host => "localhost", :port => "6380", :db => 0},
82
+ {:host => "localhost", :port => "6381", :db => 0}
83
+ ]
84
+ @store.setex "rabbit", 50, @white_rabbit, :raw => true
85
+ @store.get("rabbit", :raw => true).must_equal(%(#<OpenStruct color="white">))
86
+ end
87
+
70
88
  it "doesn't unmarshal on multi get" do
71
89
  @store.set "rabbit2", @white_rabbit
72
90
  rabbit, rabbit2 = @store.mget "rabbit", "rabbit2"
@@ -7,7 +7,8 @@ describe "Redis::Store::Namespace" do
7
7
  @client = @store.instance_variable_get(:@client)
8
8
  @rabbit = "bunny"
9
9
  @default_store = Redis::Store.new
10
- @other_store = Redis::Store.new :namespace => 'other'
10
+ @other_namespace = 'other'
11
+ @other_store = Redis::Store.new :namespace => @other_namespace
11
12
  end
12
13
 
13
14
  def teardown
@@ -41,6 +42,18 @@ describe "Redis::Store::Namespace" do
41
42
  @default_store.get('abc').must_equal('cba')
42
43
  end
43
44
 
45
+ it 'should allow to change namespace on the fly' do
46
+ @default_store.set 'abc', 'cba'
47
+ @other_store.set 'foo', 'bar'
48
+
49
+ @default_store.keys.sort.must_equal ['abc', 'other:foo']
50
+
51
+ @default_store.with_namespace(@other_namespace) do
52
+ @default_store.keys.must_equal ['foo']
53
+ @default_store.get('foo').must_equal('bar')
54
+ end
55
+ end
56
+
44
57
  it "should not try to delete missing namespaced keys" do
45
58
  empty_store = Redis::Store.new :namespace => 'empty'
46
59
  empty_store.flushdb
@@ -66,7 +66,7 @@ describe MockTtlStore do
66
66
  describe 'without options' do
67
67
  it 'must call super with key and value' do
68
68
  redis.set(key, mock_value)
69
- redis.has_set?(key, mock_value).must_equal true
69
+ redis.has_set?(key, mock_value, nil).must_equal true
70
70
  end
71
71
  end
72
72
 
@@ -76,6 +76,14 @@ describe MockTtlStore do
76
76
  redis.has_setex?(key, options[:expire_after], mock_value, :raw => true).must_equal true
77
77
  end
78
78
  end
79
+
80
+ describe 'with nx and ex option' do
81
+ it 'must call super with key and value and options' do
82
+ set_options = {nx: true, ex: 3600}
83
+ redis.set(key, mock_value, set_options)
84
+ redis.has_set?(key, mock_value, set_options).must_equal true
85
+ end
86
+ end
79
87
  end
80
88
 
81
89
  describe '#setnx' do
@@ -2,6 +2,6 @@ require 'test_helper'
2
2
 
3
3
  describe Redis::Store::VERSION do
4
4
  it 'returns current version' do
5
- Redis::Store::VERSION.must_equal '1.1.6'
5
+ Redis::Store::VERSION.must_equal '1.1.7'
6
6
  end
7
7
  end
@@ -30,6 +30,28 @@ describe Redis::Store do
30
30
  @store.set('key', 'value', options)
31
31
  end
32
32
  end
33
+
34
+ describe 'with ex and nx' do
35
+ let(:key) { 'key' }
36
+ let(:mock_value) { 'value' }
37
+ let(:options) { { nx: true, ex: 3600 } }
38
+
39
+ it 'must pass on options' do
40
+ Marshal.expects(:dump).times(4)
41
+
42
+ # without options no ex or nx will be set
43
+ @store.del(key)
44
+ @store.set(key, mock_value, {}).must_equal 'OK'
45
+ @store.set(key, mock_value, {}).must_equal 'OK'
46
+ @store.ttl(key).must_equal -1
47
+
48
+ # with ex and nx options, the key can only be set once and a ttl will be set
49
+ @store.del(key)
50
+ @store.set(key, mock_value, options).must_equal true
51
+ @store.set(key, mock_value, options).must_equal false
52
+ @store.ttl(key).must_equal 3600
53
+ end
54
+ end
33
55
  end
34
56
 
35
57
  describe '#setnx' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-store
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.7
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-28 00:00:00.000000000 Z
11
+ date: 2016-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -164,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
164
  version: '0'
165
165
  requirements: []
166
166
  rubyforge_project: redis-store
167
- rubygems_version: 2.4.5.1
167
+ rubygems_version: 2.5.1
168
168
  signing_key:
169
169
  specification_version: 4
170
170
  summary: Redis stores for Ruby frameworks