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
data/lib/redis/pubsub.rb
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
require File.expand_path '../redis', File.dirname(__FILE__)
|
2
|
+
require 'eventmachine'
|
3
|
+
|
4
|
+
class Redis
|
5
|
+
|
6
|
+
module PubSub
|
7
|
+
|
8
|
+
class Subscription
|
9
|
+
include EventMachine::Deferrable
|
10
|
+
|
11
|
+
@@channels ||= {}
|
12
|
+
@@pchannels ||= {}
|
13
|
+
|
14
|
+
def self.publish channel, message
|
15
|
+
subs = @@channels[channel] || []
|
16
|
+
subs.each do |sub|
|
17
|
+
sub.call channel, message
|
18
|
+
end
|
19
|
+
sent = subs.size
|
20
|
+
(@@pchannels || []).each do |pchannel, subs|
|
21
|
+
next unless File.fnmatch(pchannel, channel)
|
22
|
+
subs.each do |sub|
|
23
|
+
sub.pcall pchannel, channel, message
|
24
|
+
end
|
25
|
+
sent += subs.size
|
26
|
+
end
|
27
|
+
sent
|
28
|
+
end
|
29
|
+
|
30
|
+
def initialize connection
|
31
|
+
@connection = connection
|
32
|
+
@subs = []
|
33
|
+
@psubs = []
|
34
|
+
end
|
35
|
+
|
36
|
+
def bound
|
37
|
+
size > 0
|
38
|
+
end
|
39
|
+
|
40
|
+
def size
|
41
|
+
@subs.size + @psubs.size
|
42
|
+
end
|
43
|
+
|
44
|
+
def unbind
|
45
|
+
unbind_channels
|
46
|
+
unbind_patterns
|
47
|
+
end
|
48
|
+
|
49
|
+
def unbind_channels
|
50
|
+
@subs.each do |channel|
|
51
|
+
(@@channels[channel] || []).delete self
|
52
|
+
end
|
53
|
+
@subs.clear
|
54
|
+
end
|
55
|
+
|
56
|
+
def unbind_patterns
|
57
|
+
@psubs.each do |channel|
|
58
|
+
(@@pchannels[channel] || []).delete self
|
59
|
+
end
|
60
|
+
@psubs.clear
|
61
|
+
end
|
62
|
+
|
63
|
+
def subscribe channel
|
64
|
+
c = (@@channels[channel] ||= [])
|
65
|
+
unless c.include? self
|
66
|
+
c << self
|
67
|
+
@subs << channel
|
68
|
+
end
|
69
|
+
@connection.send_redis ['subscribe', channel, size]
|
70
|
+
end
|
71
|
+
|
72
|
+
def psubscribe channel
|
73
|
+
c = (@@pchannels[channel] ||= [])
|
74
|
+
unless c.include? self
|
75
|
+
c << self
|
76
|
+
@psubs << channel
|
77
|
+
end
|
78
|
+
@connection.send_redis ['psubscribe', channel, size]
|
79
|
+
end
|
80
|
+
|
81
|
+
def unsubscribe channel
|
82
|
+
c = (@@channels[channel] || [])
|
83
|
+
@subs.delete channel if c.delete self
|
84
|
+
@connection.send_redis ['unsubscribe', channel, size]
|
85
|
+
end
|
86
|
+
|
87
|
+
def punsubscribe channel
|
88
|
+
c = (@@pchannels[channel] || [])
|
89
|
+
@psubs.delete channel if c.delete self
|
90
|
+
@connection.send_redis ['punsubscribe', channel, size]
|
91
|
+
end
|
92
|
+
|
93
|
+
def call channel, message
|
94
|
+
@connection.send_redis ['message', channel, message]
|
95
|
+
end
|
96
|
+
|
97
|
+
def pcall pchannel, channel, message
|
98
|
+
@connection.send_redis ['pmessage', pchannel, channel, message]
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
def redis_SUBSCRIBE *channels
|
104
|
+
sub = @deferred
|
105
|
+
sub = Subscription.new self unless Subscription === sub
|
106
|
+
channels.each do |channel|
|
107
|
+
sub.subscribe channel
|
108
|
+
end
|
109
|
+
sub
|
110
|
+
end
|
111
|
+
|
112
|
+
def redis_UNSUBSCRIBE *channels
|
113
|
+
sub = @deferred
|
114
|
+
sub = Subscription.new self unless Subscription === sub
|
115
|
+
if channels.empty?
|
116
|
+
sub.unbind_channels
|
117
|
+
else
|
118
|
+
channels.each do |channel|
|
119
|
+
sub.unsubscribe channel
|
120
|
+
end
|
121
|
+
end
|
122
|
+
sub
|
123
|
+
end
|
124
|
+
|
125
|
+
def redis_PSUBSCRIBE *channels
|
126
|
+
sub = @deferred
|
127
|
+
sub = Subscription.new self unless Subscription === sub
|
128
|
+
channels.each do |channel|
|
129
|
+
sub.psubscribe channel
|
130
|
+
end
|
131
|
+
sub
|
132
|
+
end
|
133
|
+
|
134
|
+
def redis_PUNSUBSCRIBE *channels
|
135
|
+
sub = @deferred
|
136
|
+
sub = Subscription.new self unless Subscription === sub
|
137
|
+
if channels.empty?
|
138
|
+
sub.unbind_patterns
|
139
|
+
else
|
140
|
+
channels.each do |channel|
|
141
|
+
sub.punsubscribe channel
|
142
|
+
end
|
143
|
+
end
|
144
|
+
sub
|
145
|
+
end
|
146
|
+
|
147
|
+
def redis_PUBLISH channel, message
|
148
|
+
Subscription.publish channel, message
|
149
|
+
end
|
150
|
+
|
151
|
+
|
152
|
+
end
|
153
|
+
end
|