ruby-redis 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +9 -0
- data/README +90 -0
- data/bin/ruby-redis +12 -0
- data/bin/ruby-redis.compiled.rbc +243 -0
- data/lib/redis/bin.rb +74 -0
- data/lib/redis/bin.rbc +1325 -0
- data/lib/redis/buftok.rbc +2658 -0
- data/lib/redis/config.rb +46 -0
- data/lib/redis/config.rbc +1009 -0
- data/lib/redis/connection.rb +69 -0
- data/lib/redis/connection.rbc +1354 -0
- data/lib/redis/database.rb +109 -0
- data/lib/redis/database.rbc +2275 -0
- data/lib/redis/hashes.rb +72 -0
- data/lib/redis/hashes.rbc +1843 -0
- data/lib/redis/hiredis.rbc +658 -0
- data/lib/redis/keys.rb +165 -0
- data/lib/redis/keys.rbc +3386 -0
- data/lib/redis/lists.rb +227 -0
- data/lib/redis/lists.rbc +5241 -0
- data/lib/redis/logger.rb +81 -0
- data/lib/redis/logger.rbc +2106 -0
- data/lib/redis/protocol.rb +170 -0
- data/lib/redis/protocol.rbc +3735 -0
- data/lib/redis/pubsub.rb +153 -0
- data/lib/redis/pubsub.rbc +3447 -0
- data/lib/redis/reader.rb +164 -0
- data/lib/redis/reader.rbc +2769 -0
- data/lib/redis/send.rbc +1268 -0
- data/lib/redis/sender.rb +49 -0
- data/lib/redis/sender.rbc +1057 -0
- data/lib/redis/server.rb +62 -0
- data/lib/redis/server.rbc +1177 -0
- data/lib/redis/sets.rb +105 -0
- data/lib/redis/sets.rbc +2800 -0
- data/lib/redis/strict.rb +67 -0
- data/lib/redis/strict.rbc +1419 -0
- data/lib/redis/strings.rb +144 -0
- data/lib/redis/strings.rbc +3338 -0
- data/lib/redis/synchrony.rb +58 -0
- data/lib/redis/synchrony.rbc +1397 -0
- data/lib/redis/version.rb +7 -0
- data/lib/redis/version.rbc +180 -0
- data/lib/redis/zsets.rb +281 -0
- data/lib/redis/zsets.rbc +6596 -0
- data/lib/redis.rb +215 -0
- data/lib/redis.rbc +4391 -0
- metadata +117 -0
@@ -0,0 +1,109 @@
|
|
1
|
+
require File.expand_path '../redis', File.dirname(__FILE__)
|
2
|
+
|
3
|
+
class Redis
|
4
|
+
|
5
|
+
def self.databases
|
6
|
+
@@databases ||= [Database.new]
|
7
|
+
end
|
8
|
+
|
9
|
+
class Database
|
10
|
+
|
11
|
+
# Redis databases are volatile dictionaries.
|
12
|
+
|
13
|
+
attr_reader :blocked_pops, :watchers
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@dict = {}
|
17
|
+
@expiry = {}
|
18
|
+
@blocked_pops = {}
|
19
|
+
@watchers = {}
|
20
|
+
end
|
21
|
+
|
22
|
+
def touch key
|
23
|
+
(@watchers[key]||[]).each do |watcher|
|
24
|
+
watcher.succeed self, key
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def expire key, seconds
|
29
|
+
return false unless @dict.has_key? key
|
30
|
+
touch key
|
31
|
+
@expiry[key] = Time.now + seconds
|
32
|
+
return true
|
33
|
+
end
|
34
|
+
|
35
|
+
def expire_at key, unixtime
|
36
|
+
return false unless @dict.has_key? key
|
37
|
+
touch key
|
38
|
+
@expiry[key] = Time.at unixtime
|
39
|
+
return true
|
40
|
+
end
|
41
|
+
|
42
|
+
def ttl key
|
43
|
+
check_expiry key
|
44
|
+
time = @expiry[key]
|
45
|
+
return -1 unless time
|
46
|
+
(time - Time.now).round
|
47
|
+
end
|
48
|
+
|
49
|
+
def persist key
|
50
|
+
result = @expiry.has_key? key
|
51
|
+
touch key if result
|
52
|
+
@expiry.delete key
|
53
|
+
result
|
54
|
+
end
|
55
|
+
|
56
|
+
def random_key
|
57
|
+
@dict.keys[rand @dict.size]
|
58
|
+
end
|
59
|
+
|
60
|
+
def [] key
|
61
|
+
check_expiry key
|
62
|
+
@dict[key]
|
63
|
+
end
|
64
|
+
|
65
|
+
def []= key, value
|
66
|
+
touch key
|
67
|
+
@expiry.delete key
|
68
|
+
@dict[key] = value
|
69
|
+
end
|
70
|
+
|
71
|
+
def has_key? key
|
72
|
+
check_expiry key
|
73
|
+
@dict.has_key? key
|
74
|
+
end
|
75
|
+
|
76
|
+
def delete key
|
77
|
+
touch key
|
78
|
+
@dict.delete key
|
79
|
+
@expiry.delete key
|
80
|
+
end
|
81
|
+
|
82
|
+
def reduce *args, &block
|
83
|
+
@dict.reduce *args, &block
|
84
|
+
end
|
85
|
+
|
86
|
+
def size
|
87
|
+
@dict.size
|
88
|
+
end
|
89
|
+
|
90
|
+
def clear
|
91
|
+
# We don't trigger watchers of unset records
|
92
|
+
@dict.each_key { |key| touch key }
|
93
|
+
@dict.clear
|
94
|
+
@expiry.clear
|
95
|
+
end
|
96
|
+
|
97
|
+
def empty?
|
98
|
+
@dict.empty?
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
def check_expiry key
|
104
|
+
expires_at = @expiry[key]
|
105
|
+
delete key if expires_at and Time.now > expires_at
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
end
|