fakeredis 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +4 -4
- data/lib/fakeredis/version.rb +1 -1
- data/lib/redis/connection/memory.rb +66 -19
- data/spec/keys_spec.rb +46 -1
- metadata +36 -63
data/.travis.yml
CHANGED
data/lib/fakeredis/version.rb
CHANGED
@@ -4,11 +4,65 @@ require 'redis/connection/command_helper'
|
|
4
4
|
class Redis
|
5
5
|
module Connection
|
6
6
|
class Memory
|
7
|
+
# Represents a normal hash with some additional expiration information
|
8
|
+
# associated with each key
|
9
|
+
class ExpiringHash < Hash
|
10
|
+
attr_reader :expires
|
11
|
+
|
12
|
+
def initialize(*)
|
13
|
+
super
|
14
|
+
@expires = {}
|
15
|
+
end
|
16
|
+
|
17
|
+
def [](key)
|
18
|
+
delete(key) if expired?(key)
|
19
|
+
super
|
20
|
+
end
|
21
|
+
|
22
|
+
def []=(key, val)
|
23
|
+
expire(key)
|
24
|
+
super
|
25
|
+
end
|
26
|
+
|
27
|
+
def delete(key)
|
28
|
+
expire(key)
|
29
|
+
super
|
30
|
+
end
|
31
|
+
|
32
|
+
def expire(key)
|
33
|
+
expires.delete(key)
|
34
|
+
end
|
35
|
+
|
36
|
+
def expired?(key)
|
37
|
+
expires.include?(key) && expires[key] < Time.now
|
38
|
+
end
|
39
|
+
|
40
|
+
def key?(key)
|
41
|
+
delete(key) if expired?(key)
|
42
|
+
super
|
43
|
+
end
|
44
|
+
|
45
|
+
def values_at(*keys)
|
46
|
+
keys.each {|key| delete(key) if expired?(key)}
|
47
|
+
super
|
48
|
+
end
|
49
|
+
|
50
|
+
def keys
|
51
|
+
super.select do |key|
|
52
|
+
if expired?(key)
|
53
|
+
delete(key)
|
54
|
+
false
|
55
|
+
else
|
56
|
+
true
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
7
62
|
include Redis::Connection::CommandHelper
|
8
63
|
|
9
64
|
def initialize
|
10
|
-
@data =
|
11
|
-
@expires = {}
|
65
|
+
@data = ExpiringHash.new
|
12
66
|
@connected = false
|
13
67
|
@replies = []
|
14
68
|
end
|
@@ -62,7 +116,6 @@ class Redis
|
|
62
116
|
# * mapped_mset
|
63
117
|
# * mapped_msetnx
|
64
118
|
# * move
|
65
|
-
# * multi
|
66
119
|
# * subscribe
|
67
120
|
# * psubscribe
|
68
121
|
# * publish
|
@@ -85,8 +138,7 @@ class Redis
|
|
85
138
|
# * zscore
|
86
139
|
# * zunionstore
|
87
140
|
def flushdb
|
88
|
-
@data =
|
89
|
-
@expires = {}
|
141
|
+
@data = ExpiringHash.new
|
90
142
|
end
|
91
143
|
|
92
144
|
def flushall
|
@@ -123,12 +175,10 @@ class Redis
|
|
123
175
|
def bgreriteaof ; end
|
124
176
|
|
125
177
|
def get(key)
|
126
|
-
#return if expired?(key)
|
127
178
|
@data[key]
|
128
179
|
end
|
129
180
|
|
130
181
|
def getbit(key, offset)
|
131
|
-
#return if expired?(key)
|
132
182
|
return unless @data[key]
|
133
183
|
@data[key].unpack('B8')[0].split("")[offset]
|
134
184
|
end
|
@@ -423,7 +473,6 @@ class Redis
|
|
423
473
|
old_count = @data.keys.size
|
424
474
|
keys.flatten.each do |key|
|
425
475
|
@data.delete(key)
|
426
|
-
@expires.delete(key)
|
427
476
|
end
|
428
477
|
deleted_count = old_count - @data.keys.size
|
429
478
|
end
|
@@ -435,9 +484,8 @@ class Redis
|
|
435
484
|
def rename(key, new_key)
|
436
485
|
return unless @data[key]
|
437
486
|
@data[new_key] = @data[key]
|
438
|
-
@expires[new_key] = @expires[key]
|
487
|
+
@data.expires[new_key] = @data.expires[key] if @data.expires.include?(key)
|
439
488
|
@data.delete(key)
|
440
|
-
@expires.delete(key)
|
441
489
|
end
|
442
490
|
|
443
491
|
def renamenx(key, new_key)
|
@@ -446,21 +494,25 @@ class Redis
|
|
446
494
|
|
447
495
|
def expire(key, ttl)
|
448
496
|
return unless @data[key]
|
449
|
-
@expires[key] = ttl
|
497
|
+
@data.expires[key] = Time.now + ttl
|
450
498
|
true
|
451
499
|
end
|
452
500
|
|
453
501
|
def ttl(key)
|
454
|
-
@expires[key]
|
502
|
+
if @data.expires.include?(key) && (ttl = @data.expires[key].to_i - Time.now.to_i) > 0
|
503
|
+
ttl
|
504
|
+
else
|
505
|
+
-1
|
506
|
+
end
|
455
507
|
end
|
456
508
|
|
457
509
|
def expireat(key, timestamp)
|
458
|
-
@expires[key] =
|
510
|
+
@data.expires[key] = Time.at(timestamp)
|
459
511
|
true
|
460
512
|
end
|
461
513
|
|
462
514
|
def persist(key)
|
463
|
-
@expires
|
515
|
+
@data.expires.delete(key)
|
464
516
|
end
|
465
517
|
|
466
518
|
def hset(key, field, value)
|
@@ -643,11 +695,6 @@ class Redis
|
|
643
695
|
def fail_unless_set(key)
|
644
696
|
fail "Not a set" unless is_a_set?(key)
|
645
697
|
end
|
646
|
-
|
647
|
-
def expired?(key)
|
648
|
-
return false if @expires[key] == -1
|
649
|
-
return true if @expires[key] && @expires[key] < Time.now
|
650
|
-
end
|
651
698
|
end
|
652
699
|
end
|
653
700
|
end
|
data/spec/keys_spec.rb
CHANGED
@@ -34,7 +34,52 @@ puts "checking existence"
|
|
34
34
|
@client.set("key1", "1")
|
35
35
|
@client.expireat("key1", Time.now.to_i + 2)
|
36
36
|
|
37
|
-
@client.ttl("key1").should ==
|
37
|
+
@client.ttl("key1").should == 2
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should not have an expiration after re-set' do
|
41
|
+
@client.set("key1", "1")
|
42
|
+
@client.expireat("key1", Time.now.to_i + 2)
|
43
|
+
@client.set("key1", "1")
|
44
|
+
|
45
|
+
@client.ttl("key1").should == -1
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should not have a ttl if expired" do
|
49
|
+
@client.set("key1", "1")
|
50
|
+
@client.expireat("key1", Time.now.to_i)
|
51
|
+
|
52
|
+
@client.ttl("key1").should == -1
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should not find a key if expired" do
|
56
|
+
@client.set("key1", "1")
|
57
|
+
@client.expireat("key1", Time.now.to_i)
|
58
|
+
|
59
|
+
@client.get("key1").should be_nil
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should not find multiple keys if expired" do
|
63
|
+
@client.set("key1", "1")
|
64
|
+
@client.set("key2", "2")
|
65
|
+
@client.expireat("key1", Time.now.to_i)
|
66
|
+
|
67
|
+
@client.mget("key1", "key2").should == [nil, "2"]
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should only find keys that aren't expired" do
|
71
|
+
@client.set("key1", "1")
|
72
|
+
@client.set("key2", "2")
|
73
|
+
@client.expireat("key1", Time.now.to_i)
|
74
|
+
|
75
|
+
@client.keys.should == ["key2"]
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should not exist if expired" do
|
79
|
+
@client.set("key1", "1")
|
80
|
+
@client.expireat("key1", Time.now.to_i)
|
81
|
+
|
82
|
+
@client.exists("key1").should be_false
|
38
83
|
end
|
39
84
|
|
40
85
|
it "should find all keys matching the given pattern" do
|
metadata
CHANGED
@@ -1,65 +1,47 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: fakeredis
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 2
|
9
|
-
- 0
|
10
|
-
version: 0.2.0
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Guillermo Iguaran
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
date: 2011-07-31 00:00:00 -05:00
|
12
|
+
date: 2011-08-18 00:00:00.000000000 -05:00
|
19
13
|
default_executable:
|
20
|
-
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
22
16
|
name: redis
|
23
|
-
|
24
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
17
|
+
requirement: &2152100500 !ruby/object:Gem::Requirement
|
25
18
|
none: false
|
26
|
-
requirements:
|
19
|
+
requirements:
|
27
20
|
- - ~>
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
hash: 7
|
30
|
-
segments:
|
31
|
-
- 2
|
32
|
-
- 2
|
33
|
-
- 0
|
21
|
+
- !ruby/object:Gem::Version
|
34
22
|
version: 2.2.0
|
35
23
|
type: :runtime
|
36
|
-
version_requirements: *id001
|
37
|
-
- !ruby/object:Gem::Dependency
|
38
|
-
name: rspec
|
39
24
|
prerelease: false
|
40
|
-
|
25
|
+
version_requirements: *2152100500
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rspec
|
28
|
+
requirement: &2152098160 !ruby/object:Gem::Requirement
|
41
29
|
none: false
|
42
|
-
requirements:
|
43
|
-
- -
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
hash: 15
|
46
|
-
segments:
|
47
|
-
- 2
|
48
|
-
- 0
|
49
|
-
- 0
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
50
33
|
version: 2.0.0
|
51
34
|
type: :development
|
52
|
-
|
53
|
-
|
54
|
-
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *2152098160
|
37
|
+
description: Fake (In-memory) driver for redis-rb. Useful for testing environment
|
38
|
+
and machines without Redis.
|
39
|
+
email:
|
55
40
|
- guilleiguaran@gmail.com
|
56
41
|
executables: []
|
57
|
-
|
58
42
|
extensions: []
|
59
|
-
|
60
43
|
extra_rdoc_files: []
|
61
|
-
|
62
|
-
files:
|
44
|
+
files:
|
63
45
|
- .gitignore
|
64
46
|
- .rspec
|
65
47
|
- .travis.yml
|
@@ -85,38 +67,29 @@ files:
|
|
85
67
|
has_rdoc: true
|
86
68
|
homepage: https://github.com/guilleiguaran/fakeredis
|
87
69
|
licenses: []
|
88
|
-
|
89
70
|
post_install_message:
|
90
71
|
rdoc_options: []
|
91
|
-
|
92
|
-
require_paths:
|
72
|
+
require_paths:
|
93
73
|
- lib
|
94
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
75
|
none: false
|
96
|
-
requirements:
|
97
|
-
- -
|
98
|
-
- !ruby/object:Gem::Version
|
99
|
-
|
100
|
-
|
101
|
-
- 0
|
102
|
-
version: "0"
|
103
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
81
|
none: false
|
105
|
-
requirements:
|
106
|
-
- -
|
107
|
-
- !ruby/object:Gem::Version
|
108
|
-
|
109
|
-
segments:
|
110
|
-
- 0
|
111
|
-
version: "0"
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
112
86
|
requirements: []
|
113
|
-
|
114
87
|
rubyforge_project: fakeredis
|
115
|
-
rubygems_version: 1.
|
88
|
+
rubygems_version: 1.6.2
|
116
89
|
signing_key:
|
117
90
|
specification_version: 3
|
118
91
|
summary: Fake (In-memory) driver for redis-rb.
|
119
|
-
test_files:
|
92
|
+
test_files:
|
120
93
|
- spec/compatibility_spec.rb
|
121
94
|
- spec/connection_spec.rb
|
122
95
|
- spec/hashes_spec.rb
|