Class: Redis::Connection::Memory
- Inherits:
-
Object
- Object
- Redis::Connection::Memory
show all
- Includes:
- BitopCommand, CommandExecutor, FakeRedis, GeoCommands, CommandHelper, SortMethod, TransactionCommands
- Defined in:
- lib/redis/connection/memory.rb
Constant Summary
Constants included
from FakeRedis
FakeRedis::Redis, FakeRedis::TRANSACTION_COMMANDS, FakeRedis::VERSION
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#append(key, value) ⇒ Object
-
#auth(*args) ⇒ Object
-
#bgrewriteaof ⇒ Object
-
#bgsave ⇒ Object
-
#bitcount(key, start_index = 0, end_index = -1)) ⇒ Object
-
#bitpos(key, bit, start_index = 0, end_index = -1)) ⇒ Object
-
#blpop(keys, timeout = 0) ⇒ Object
-
#brpop(keys, timeout = 0) ⇒ Object
-
#brpoplpush(key1, key2, opts = {}) ⇒ Object
-
#bzpopmax(*args, timeout: 0) ⇒ Object
-
#bzpopmin(*args, timeout: 0) ⇒ Object
-
#client(command, _options = {}) ⇒ Object
-
#connect_unix(path, timeout) ⇒ Object
-
#connected? ⇒ Boolean
-
#data ⇒ Object
-
#database_instance_key ⇒ Object
-
#databases ⇒ Object
-
#dbsize ⇒ Object
-
#decr(key) ⇒ Object
-
#decrby(key, by) ⇒ Object
-
#del(*keys) ⇒ Object
-
#disconnect ⇒ Object
-
#dump(key) ⇒ Object
-
#echo(string) ⇒ Object
-
#eval ⇒ Object
-
#evalsha ⇒ Object
-
#exists(*keys) ⇒ Object
-
#exists?(key) ⇒ Boolean
-
#expire(key, ttl) ⇒ Object
-
#expireat(key, timestamp) ⇒ Object
-
#find_database(id = database_id) ⇒ Object
-
#flushall ⇒ Object
-
#flushdb ⇒ Object
-
#get(key) ⇒ Object
-
#getbit(key, offset) ⇒ Object
-
#getrange(key, start, ending) ⇒ Object
(also: #substr)
-
#getset(key, value) ⇒ Object
-
#hdel(key, field) ⇒ Object
-
#hexists(key, field) ⇒ Object
-
#hget(key, field) ⇒ Object
-
#hgetall(key) ⇒ Object
-
#hincrby(key, field, increment) ⇒ Object
-
#hincrbyfloat(key, field, increment) ⇒ Object
-
#hkeys(key) ⇒ Object
-
#hlen(key) ⇒ Object
-
#hmget(key, *fields) ⇒ Object
-
#hmset(key, *fields) ⇒ Object
-
#hscan(key, start_cursor, *args) ⇒ Object
-
#hset(key, *fields) ⇒ Object
-
#hsetnx(key, field, value) ⇒ Object
-
#hstrlen(key, field) ⇒ Object
-
#hvals(key) ⇒ Object
-
#incr(key) ⇒ Object
-
#incrby(key, by) ⇒ Object
-
#incrbyfloat(key, by) ⇒ Object
-
#info ⇒ Object
-
#initialize(options = {}) ⇒ Memory
constructor
A new instance of Memory.
-
#keys(pattern = "*") ⇒ Object
-
#lastsave ⇒ Object
-
#lindex(key, index) ⇒ Object
-
#linsert(key, where, pivot, value) ⇒ Object
-
#llen(key) ⇒ Object
-
#lpop(key) ⇒ Object
-
#lpush(key, value) ⇒ Object
-
#lpushx(key, value) ⇒ Object
-
#lrange(key, startidx, endidx) ⇒ Object
-
#lrem(key, count, value) ⇒ Object
-
#lset(key, index, value) ⇒ Object
-
#ltrim(key, start, stop) ⇒ Object
-
#mget(*keys) ⇒ Object
-
#monitor ⇒ Object
-
#move(key, destination_id) ⇒ Object
-
#mset(*pairs) ⇒ Object
-
#msetnx(*pairs) ⇒ Object
-
#non_integer_expirations?(options) ⇒ Boolean
-
#persist(key) ⇒ Object
-
#pexpire(key, ttl) ⇒ Object
-
#pfadd(key, member) ⇒ Object
-
#pfcount(*keys) ⇒ Object
-
#pfmerge(destination, *sources) ⇒ Object
-
#ping ⇒ Object
-
#psetex(key, milliseconds, value) ⇒ Object
-
#psubscribe(*patterns) ⇒ Object
-
#pttl(key) ⇒ Object
-
#publish(channel, message) ⇒ Object
-
#punsubscribe(*patterns) ⇒ Object
-
#quit ⇒ Object
-
#randomkey ⇒ Object
-
#read ⇒ Object
-
#rename(key, new_key) ⇒ Object
-
#renamenx(key, new_key) ⇒ Object
-
#restore(key, ttl, serialized_value) ⇒ Object
-
#rpop(key) ⇒ Object
-
#rpoplpush(key1, key2) ⇒ Object
-
#rpush(key, value) ⇒ Object
-
#rpushx(key, value) ⇒ Object
-
#sadd(key, value) ⇒ Object
-
#sadd?(key, value) ⇒ Boolean
-
#save ⇒ Object
-
#scan(start_cursor, *args) ⇒ Object
-
#scard(key) ⇒ Object
-
#sdiff(key1, *keys) ⇒ Object
-
#sdiffstore(destination, key1, *keys) ⇒ Object
-
#select(index) ⇒ Object
-
#set(key, value, *array_options) ⇒ Object
-
#setbit(key, offset, bit) ⇒ Object
-
#setex(key, seconds, value) ⇒ Object
-
#setnx(key, value) ⇒ Object
-
#setrange(key, offset, value) ⇒ Object
-
#shutdown ⇒ Object
-
#sinter(*keys) ⇒ Object
-
#sinterstore(destination, *keys) ⇒ Object
-
#sismember(key, value) ⇒ Object
-
#slaveof(host, port) ⇒ Object
-
#smembers(key) ⇒ Object
-
#smove(source, destination, value) ⇒ Object
-
#spop(key, count = nil) ⇒ Object
-
#srandmember(key, number = nil) ⇒ Object
-
#srem(key, value) ⇒ Object
-
#sscan(key, start_cursor, *args) ⇒ Object
-
#strlen(key) ⇒ Object
-
#subscribe(*channels) ⇒ Object
-
#sunion(*keys) ⇒ Object
-
#sunionstore(destination, *keys) ⇒ Object
-
#sync ⇒ Object
-
#time ⇒ Object
-
#timeout=(usecs) ⇒ Object
-
#ttl(key) ⇒ Object
-
#type(key) ⇒ Object
-
#unlink(*keys) ⇒ Object
-
#unsubscribe(*channels) ⇒ Object
-
#zadd(key, *args) ⇒ Object
-
#zcard(key) ⇒ Object
-
#zcount(key, min, max) ⇒ Object
-
#zincrby(key, num, value) ⇒ Object
-
#zinterstore(out, *args) ⇒ Object
-
#zpopmax(key, count = nil) ⇒ Object
-
#zpopmin(key, count = nil) ⇒ Object
-
#zrange(key, start, stop, with_scores = nil) ⇒ Object
-
#zrangebylex(key, start, stop, *opts) ⇒ Object
-
#zrangebyscore(key, min, max, *opts) ⇒ Object
-
#zrank(key, value) ⇒ Object
-
#zrem(key, value) ⇒ Object
-
#zremrangebyrank(key, start, stop) ⇒ Object
-
#zremrangebyscore(key, min, max) ⇒ Object
-
#zrevrange(key, start, stop, with_scores = nil) ⇒ Object
-
#zrevrangebylex(key, start, stop, *args) ⇒ Object
-
#zrevrangebyscore(key, max, min, *opts) ⇒ Object
-
#zrevrank(key, value) ⇒ Object
-
#zscan(key, start_cursor, *args) ⇒ Object
-
#zscan_each(key, *args, &block) ⇒ Object
Originally from redis-rb.
-
#zscore(key, value) ⇒ Object
-
#zunionstore(out, *args) ⇒ Object
Methods included from FakeRedis
disable, disabling, enable, enabled?, enabling
Constructor Details
#initialize(options = {}) ⇒ Memory
Returns a new instance of Memory.
57
58
59
|
# File 'lib/redis/connection/memory.rb', line 57
def initialize(options = {})
self.options = self.options ? self.options.merge(options) : options
end
|
Instance Attribute Details
#database_id ⇒ Object
61
62
63
|
# File 'lib/redis/connection/memory.rb', line 61
def database_id
@database_id ||= 0
end
|
#options ⇒ Object
Returns the value of attribute options.
28
29
30
|
# File 'lib/redis/connection/memory.rb', line 28
def options
@options
end
|
#replies ⇒ Object
83
84
85
|
# File 'lib/redis/connection/memory.rb', line 83
def replies
@replies ||= []
end
|
Class Method Details
.channels ⇒ Object
45
46
47
|
# File 'lib/redis/connection/memory.rb', line 45
def self.channels
@channels ||= Hash.new {|h,k| h[k] = [] }
end
|
.connect(options = {}) ⇒ Object
53
54
55
|
# File 'lib/redis/connection/memory.rb', line 53
def self.connect(options = {})
new(options)
end
|
.databases ⇒ Object
Tracks all databases for all instances across the current process. We have to be able to handle two clients with the same host/port accessing different databases at once without overwriting each other. So we store our “data” outside the client instances, in this class level instance method. Client instances access it with a key made up of their host/port, and then select which DB out of the array of them they want. Allows the access we need.
36
37
38
|
# File 'lib/redis/connection/memory.rb', line 36
def self.databases
@databases ||= Hash.new {|h,k| h[k] = [] }
end
|
.reset_all_channels ⇒ Object
49
50
51
|
# File 'lib/redis/connection/memory.rb', line 49
def self.reset_all_channels
@channels = nil
end
|
.reset_all_databases ⇒ Object
Used for resetting everything in specs
41
42
43
|
# File 'lib/redis/connection/memory.rb', line 41
def self.reset_all_databases
@databases = nil
end
|
Instance Method Details
#append(key, value) ⇒ Object
250
251
252
253
|
# File 'lib/redis/connection/memory.rb', line 250
def append(key, value)
data[key] = (data[key] || "")
data[key] = data[key] + value.to_s
end
|
#auth(*args) ⇒ Object
125
126
127
|
# File 'lib/redis/connection/memory.rb', line 125
def auth(*args)
"OK"
end
|
#bgrewriteaof ⇒ Object
156
|
# File 'lib/redis/connection/memory.rb', line 156
def bgrewriteaof; end
|
#bgsave ⇒ Object
154
|
# File 'lib/redis/connection/memory.rb', line 154
def bgsave; end
|
#bitcount(key, start_index = 0, end_index = -1)) ⇒ Object
220
221
222
223
|
# File 'lib/redis/connection/memory.rb', line 220
def bitcount(key, start_index = 0, end_index = -1)
return 0 unless data[key]
data[key][start_index..end_index].unpack('B*')[0].count("1")
end
|
#bitpos(key, bit, start_index = 0, end_index = -1)) ⇒ Object
225
226
227
228
|
# File 'lib/redis/connection/memory.rb', line 225
def bitpos(key, bit, start_index = 0, end_index = -1)
value = data[key] || ""
value[0..end_index].unpack('B*')[0].index(bit.to_s, start_index * 8) || -1
end
|
#blpop(keys, timeout = 0) ⇒ Object
524
525
526
527
528
529
530
531
532
533
534
|
# File 'lib/redis/connection/memory.rb', line 524
def blpop(keys, timeout=0)
keys = Array(keys)
keys.each do |key|
if data[key] && data[key].size > 0
return [key, data[key].shift]
end
end
sleep(timeout.to_f)
nil
end
|
#brpop(keys, timeout = 0) ⇒ Object
492
493
494
495
496
497
498
499
500
501
502
|
# File 'lib/redis/connection/memory.rb', line 492
def brpop(keys, timeout=0)
keys = Array(keys)
keys.each do |key|
if data[key] && data[key].size > 0
return [key, data[key].pop]
end
end
sleep(timeout.to_f)
nil
end
|
#brpoplpush(key1, key2, opts = {}) ⇒ Object
511
512
513
514
515
516
|
# File 'lib/redis/connection/memory.rb', line 511
def brpoplpush(key1, key2, opts={})
data_type_check(key1, Array)
_key, elem = brpop(key1)
lpush(key2, elem) unless elem.nil?
elem
end
|
#bzpopmax(*args, timeout: 0) ⇒ Object
1159
1160
1161
|
# File 'lib/redis/connection/memory.rb', line 1159
def bzpopmax(*args, timeout: 0)
bzpop(:bzpopmax, args, timeout)
end
|
#bzpopmin(*args, timeout: 0) ⇒ Object
1163
1164
1165
|
# File 'lib/redis/connection/memory.rb', line 1163
def bzpopmin(*args, timeout: 0)
bzpop(:bzpopmin, args, timeout)
end
|
#client(command, _options = {}) ⇒ Object
99
100
101
102
103
104
105
106
|
# File 'lib/redis/connection/memory.rb', line 99
def client(command, _options = {})
case command
when :setname then "OK"
when :getname then nil
else
raise Redis::CommandError, "ERR unknown command '#{command}'"
end
end
|
#connect_unix(path, timeout) ⇒ Object
92
93
|
# File 'lib/redis/connection/memory.rb', line 92
def connect_unix(path, timeout)
end
|
#connected? ⇒ Boolean
88
89
90
|
# File 'lib/redis/connection/memory.rb', line 88
def connected?
defined?(@disconnected) ? false : true
end
|
#data ⇒ Object
79
80
81
|
# File 'lib/redis/connection/memory.rb', line 79
def data
find_database
end
|
#database_instance_key ⇒ Object
67
68
69
|
# File 'lib/redis/connection/memory.rb', line 67
def database_instance_key
[options[:host], options[:port]].hash
end
|
#databases ⇒ Object
71
72
73
|
# File 'lib/redis/connection/memory.rb', line 71
def databases
self.class.databases[database_instance_key]
end
|
#dbsize ⇒ Object
358
359
360
|
# File 'lib/redis/connection/memory.rb', line 358
def dbsize
data.keys.count
end
|
#decr(key) ⇒ Object
999
1000
1001
1002
|
# File 'lib/redis/connection/memory.rb', line 999
def decr(key)
data.merge!({ key => (data[key].to_i - 1).to_s || "-1"})
data[key].to_i
end
|
#decrby(key, by) ⇒ Object
1004
1005
1006
1007
|
# File 'lib/redis/connection/memory.rb', line 1004
def decrby(key, by)
data.merge!({ key => ((data[key].to_i - by.to_i) || (by.to_i * -1)).to_s })
data[key].to_i
end
|
#del(*keys) ⇒ Object
711
712
713
|
# File 'lib/redis/connection/memory.rb', line 711
def del(*keys)
delete_keys(keys, 'del')
end
|
#disconnect ⇒ Object
95
96
97
|
# File 'lib/redis/connection/memory.rb', line 95
def disconnect
@disconnected = true
end
|
#dump(key) ⇒ Object
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/redis/connection/memory.rb', line 171
def dump(key)
return nil if exists(key).zero?
value = data[key]
Marshal.dump(
value: value,
version: FakeRedis::VERSION, )
end
|
#echo(string) ⇒ Object
341
342
343
|
# File 'lib/redis/connection/memory.rb', line 341
def echo(string)
string
end
|
#eval ⇒ Object
160
|
# File 'lib/redis/connection/memory.rb', line 160
def eval; end
|
#evalsha ⇒ Object
158
|
# File 'lib/redis/connection/memory.rb', line 158
def evalsha; end
|
#exists(*keys) ⇒ Object
362
363
364
|
# File 'lib/redis/connection/memory.rb', line 362
def exists(*keys)
keys.count { |key| data.key?(key) }
end
|
#exists?(key) ⇒ Boolean
366
367
368
|
# File 'lib/redis/connection/memory.rb', line 366
def exists?(key)
data.key?(key)
end
|
#expire(key, ttl) ⇒ Object
744
745
746
747
748
|
# File 'lib/redis/connection/memory.rb', line 744
def expire(key, ttl)
return 0 unless data[key]
data.expires[key] = Time.now + ttl
1
end
|
#expireat(key, timestamp) ⇒ Object
772
773
774
775
|
# File 'lib/redis/connection/memory.rb', line 772
def expireat(key, timestamp)
data.expires[key] = Time.at(timestamp)
true
end
|
#find_database(id = database_id) ⇒ Object
75
76
77
|
# File 'lib/redis/connection/memory.rb', line 75
def find_database id=database_id
databases[id] ||= ExpiringHash.new
end
|
#flushall ⇒ Object
120
121
122
123
|
# File 'lib/redis/connection/memory.rb', line 120
def flushall
self.class.databases[database_instance_key] = []
"OK"
end
|
#flushdb ⇒ Object
115
116
117
118
|
# File 'lib/redis/connection/memory.rb', line 115
def flushdb
databases.delete_at(database_id)
"OK"
end
|
#get(key) ⇒ Object
210
211
212
213
|
# File 'lib/redis/connection/memory.rb', line 210
def get(key)
data_type_check(key, String)
data[key]
end
|
#getbit(key, offset) ⇒ Object
215
216
217
218
|
# File 'lib/redis/connection/memory.rb', line 215
def getbit(key, offset)
return unless data[key]
data[key].unpack('B*')[0].split("")[offset].to_i
end
|
#getrange(key, start, ending) ⇒ Object
Also known as:
substr
230
231
232
233
|
# File 'lib/redis/connection/memory.rb', line 230
def getrange(key, start, ending)
return unless data[key]
data[key][start..ending]
end
|
#getset(key, value) ⇒ Object
236
237
238
239
240
241
|
# File 'lib/redis/connection/memory.rb', line 236
def getset(key, value)
data_type_check(key, String)
data[key].tap do
set(key, value)
end
end
|
#hdel(key, field) ⇒ Object
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
# File 'lib/redis/connection/memory.rb', line 270
def hdel(key, field)
data_type_check(key, Hash)
return 0 unless data[key]
if field.is_a?(Array)
old_keys_count = data[key].size
fields = field.map(&:to_s)
data[key].delete_if { |k, v| fields.include? k }
deleted = old_keys_count - data[key].size
else
field = field.to_s
deleted = data[key].delete(field) ? 1 : 0
end
remove_key_for_empty_collection(key)
deleted
end
|
#hexists(key, field) ⇒ Object
895
896
897
898
899
|
# File 'lib/redis/connection/memory.rb', line 895
def hexists(key, field)
data_type_check(key, Hash)
return false unless data[key]
data[key].key?(field.to_s)
end
|
#hget(key, field) ⇒ Object
265
266
267
268
|
# File 'lib/redis/connection/memory.rb', line 265
def hget(key, field)
data_type_check(key, Hash)
data[key] && data[key][field.to_s]
end
|
#hgetall(key) ⇒ Object
260
261
262
263
|
# File 'lib/redis/connection/memory.rb', line 260
def hgetall(key)
data_type_check(key, Hash)
data[key].to_a.flatten || {}
end
|
#hincrby(key, field, increment) ⇒ Object
873
874
875
876
877
878
879
880
881
882
|
# File 'lib/redis/connection/memory.rb', line 873
def hincrby(key, field, increment)
data_type_check(key, Hash)
field = field.to_s
if data[key]
data[key][field] = (data[key][field].to_i + increment.to_i).to_s
else
data[key] = { field => increment.to_s }
end
data[key][field].to_i
end
|
#hincrbyfloat(key, field, increment) ⇒ Object
884
885
886
887
888
889
890
891
892
893
|
# File 'lib/redis/connection/memory.rb', line 884
def hincrbyfloat(key, field, increment)
data_type_check(key, Hash)
field = field.to_s
if data[key]
data[key][field] = (data[key][field].to_f + increment.to_f).to_s
else
data[key] = { field => increment.to_s }
end
data[key][field]
end
|
#hkeys(key) ⇒ Object
289
290
291
292
293
|
# File 'lib/redis/connection/memory.rb', line 289
def hkeys(key)
data_type_check(key, Hash)
return [] if data[key].nil?
data[key].keys
end
|
#hlen(key) ⇒ Object
855
856
857
858
859
|
# File 'lib/redis/connection/memory.rb', line 855
def hlen(key)
data_type_check(key, Hash)
return 0 unless data[key]
data[key].size
end
|
#hmget(key, *fields) ⇒ Object
841
842
843
844
845
846
847
848
849
850
851
852
853
|
# File 'lib/redis/connection/memory.rb', line 841
def hmget(key, *fields)
raise_argument_error('hmget') if fields.empty? || fields.flatten.empty?
data_type_check(key, Hash)
fields.flatten.map do |field|
field = field.to_s
if data[key]
data[key][field]
else
nil
end
end
end
|
#hmset(key, *fields) ⇒ Object
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
|
# File 'lib/redis/connection/memory.rb', line 816
def hmset(key, *fields)
fields = fields[0] if mapped_param?(fields)
raise_argument_error('hmset') if fields.empty?
is_list_of_arrays = fields.all?{|field| field.instance_of?(Array)}
raise_argument_error('hmset') if fields.size.odd? and !is_list_of_arrays
raise_argument_error('hmset') if is_list_of_arrays and !fields.all?{|field| field.length == 2}
data_type_check(key, Hash)
data[key] ||= {}
if is_list_of_arrays
fields.each do |pair|
data[key][pair[0].to_s] = pair[1].to_s
end
else
fields.each_slice(2) do |field|
data[key][field[0].to_s] = field[1].to_s
end
end
"OK"
end
|
#hscan(key, start_cursor, *args) ⇒ Object
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
# File 'lib/redis/connection/memory.rb', line 295
def hscan(key, start_cursor, *args)
data_type_check(key, Hash)
return ["0", []] unless data[key]
match = "*"
count = 10
if args.size.odd?
raise_argument_error('hscan')
end
if idx = args.index("MATCH")
match = args[idx + 1]
end
if idx = args.index("COUNT")
count = args[idx + 1]
end
start_cursor = start_cursor.to_i
cursor = start_cursor
next_keys = []
if start_cursor + count >= data[key].length
next_keys = (data[key].to_a)[start_cursor..-1]
cursor = 0
else
cursor = start_cursor + count
next_keys = (data[key].to_a)[start_cursor..cursor-1]
end
filtered_next_keys = next_keys.select{|k,v| File.fnmatch(match, k)}
result = filtered_next_keys.flatten.map(&:to_s)
return ["#{cursor}", result]
end
|
#hset(key, *fields) ⇒ Object
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
|
# File 'lib/redis/connection/memory.rb', line 781
def hset(key, *fields)
fields = fields.first if fields.size == 1 && fields.first.is_a?(Hash)
raise_argument_error('hset') if fields.empty?
is_list_of_arrays = fields.all?{|field| field.instance_of?(Array)}
raise_argument_error('hmset') if fields.size.odd? and !is_list_of_arrays
raise_argument_error('hmset') if is_list_of_arrays and !fields.all?{|field| field.length == 2}
data_type_check(key, Hash)
insert_count = 0
data[key] ||= {}
if fields.is_a?(Hash)
insert_count = fields.keys.size - (data[key].keys & fields.keys).size
data[key].merge!(fields)
else
fields.each_slice(2) do |field|
insert_count += 1 if data[key][field[0].to_s].nil?
data[key][field[0].to_s] = field[1].to_s
end
end
insert_count
end
|
#hsetnx(key, field, value) ⇒ Object
809
810
811
812
813
814
|
# File 'lib/redis/connection/memory.rb', line 809
def hsetnx(key, field, value)
data_type_check(key, Hash)
field = field.to_s
return false if data[key] && data[key][field]
hset(key, field, value)
end
|
#hstrlen(key, field) ⇒ Object
861
862
863
864
865
|
# File 'lib/redis/connection/memory.rb', line 861
def hstrlen(key, field)
data_type_check(key, Hash)
return 0 if data[key].nil? || data[key][field].nil?
data[key][field].size
end
|
#hvals(key) ⇒ Object
867
868
869
870
871
|
# File 'lib/redis/connection/memory.rb', line 867
def hvals(key)
data_type_check(key, Hash)
return [] unless data[key]
data[key].values
end
|
#incr(key) ⇒ Object
984
985
986
987
|
# File 'lib/redis/connection/memory.rb', line 984
def incr(key)
data.merge!({ key => (data[key].to_i + 1).to_s || "1"})
data[key].to_i
end
|
#incrby(key, by) ⇒ Object
989
990
991
992
|
# File 'lib/redis/connection/memory.rb', line 989
def incrby(key, by)
data.merge!({ key => (data[key].to_i + by.to_i).to_s || by })
data[key].to_i
end
|
#incrbyfloat(key, by) ⇒ Object
994
995
996
997
|
# File 'lib/redis/connection/memory.rb', line 994
def incrbyfloat(key, by)
data.merge!({ key => (data[key].to_f + by.to_f).to_s || by })
data[key]
end
|
#info ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/redis/connection/memory.rb', line 135
def info
{
"redis_version" => options[:version] || DEFAULT_REDIS_VERSION,
"connected_clients" => "1",
"connected_slaves" => "0",
"used_memory" => "3187",
"changes_since_last_save" => "0",
"last_save_time" => "1237655729",
"total_connections_received" => "1",
"total_commands_processed" => "1",
"uptime_in_seconds" => "36000",
"uptime_in_days" => 0
}
end
|
#keys(pattern = "*") ⇒ Object
333
334
335
|
# File 'lib/redis/connection/memory.rb', line 333
def keys(pattern = "*")
data.keys.select { |key| File.fnmatch(pattern, key) }
end
|
#lastsave ⇒ Object
349
350
351
|
# File 'lib/redis/connection/memory.rb', line 349
def lastsave
Time.now.to_i
end
|
#lindex(key, index) ⇒ Object
405
406
407
408
|
# File 'lib/redis/connection/memory.rb', line 405
def lindex(key, index)
data_type_check(key, Array)
data[key] && data[key][index]
end
|
#linsert(key, where, pivot, value) ⇒ Object
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
# File 'lib/redis/connection/memory.rb', line 410
def linsert(key, where, pivot, value)
data_type_check(key, Array)
return unless data[key]
value = value.to_s
index = data[key].index(pivot.to_s)
return -1 if index.nil?
case where.to_s
when /\Abefore\z/i then data[key].insert(index, value)
when /\Aafter\z/i then data[key].insert(index + 1, value)
else raise_syntax_error
end
end
|
#llen(key) ⇒ Object
370
371
372
373
374
|
# File 'lib/redis/connection/memory.rb', line 370
def llen(key)
data_type_check(key, Array)
return 0 unless data[key]
data[key].size
end
|
#lpop(key) ⇒ Object
518
519
520
521
522
|
# File 'lib/redis/connection/memory.rb', line 518
def lpop(key)
data_type_check(key, Array)
return unless data[key]
data[key].shift
end
|
#lpush(key, value) ⇒ Object
469
470
471
472
473
474
475
476
477
|
# File 'lib/redis/connection/memory.rb', line 469
def lpush(key, value)
raise_argument_error('lpush') if value.respond_to?(:each) && value.empty?
data_type_check(key, Array)
data[key] ||= []
[value].flatten.each do |val|
data[key].unshift(val.to_s)
end
data[key].size
end
|
#lpushx(key, value) ⇒ Object
479
480
481
482
483
484
|
# File 'lib/redis/connection/memory.rb', line 479
def lpushx(key, value)
raise_argument_error('lpushx') if value.respond_to?(:each) && value.empty?
data_type_check(key, Array)
return unless data[key]
lpush(key, value)
end
|
#lrange(key, startidx, endidx) ⇒ Object
376
377
378
379
380
381
382
383
384
385
386
|
# File 'lib/redis/connection/memory.rb', line 376
def lrange(key, startidx, endidx)
data_type_check(key, Array)
if data[key]
startidx = 0 if startidx < 0 && startidx.abs > data[key].size
data[key][startidx..endidx] || []
else
[]
end
end
|
#lrem(key, count, value) ⇒ Object
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
# File 'lib/redis/connection/memory.rb', line 432
def lrem(key, count, value)
data_type_check(key, Array)
return 0 unless data[key]
value = value.to_s
old_size = data[key].size
diff =
if count == 0
data[key].delete(value)
old_size - data[key].size
else
array = count > 0 ? data[key].dup : data[key].reverse
count.abs.times{ array.delete_at(array.index(value) || array.length) }
data[key] = count > 0 ? array.dup : array.reverse
old_size - data[key].size
end
remove_key_for_empty_collection(key)
diff
end
|
#lset(key, index, value) ⇒ Object
425
426
427
428
429
430
|
# File 'lib/redis/connection/memory.rb', line 425
def lset(key, index, value)
data_type_check(key, Array)
return unless data[key]
raise Redis::CommandError, "ERR index out of range" if index >= data[key].size
data[key][index] = value.to_s
end
|
#ltrim(key, start, stop) ⇒ Object
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
# File 'lib/redis/connection/memory.rb', line 388
def ltrim(key, start, stop)
data_type_check(key, Array)
return unless data[key]
unless start < 0 && data[key].count < start.abs
data[key] = data[key][start..stop]
end
"OK"
end
|
#mget(*keys) ⇒ Object
243
244
245
246
247
248
|
# File 'lib/redis/connection/memory.rb', line 243
def mget(*keys)
keys = keys[0] if flatten?(keys)
raise_argument_error('mget') if keys.empty?
keys.each { |key| data_type_check(key, String) }
data.values_at(*keys)
end
|
#monitor ⇒ Object
150
|
# File 'lib/redis/connection/memory.rb', line 150
def monitor; end
|
#move(key, destination_id) ⇒ Object
162
163
164
165
166
167
168
169
|
# File 'lib/redis/connection/memory.rb', line 162
def move key, destination_id
raise Redis::CommandError, "ERR source and destination objects are the same" if destination_id == database_id
destination = find_database(destination_id)
return false unless data.has_key?(key)
return false if destination.has_key?(key)
destination[key] = data.delete(key)
true
end
|
#mset(*pairs) ⇒ Object
961
962
963
964
965
966
967
968
969
970
971
972
|
# File 'lib/redis/connection/memory.rb', line 961
def mset(*pairs)
pairs = pairs[0] if mapped_param?(pairs)
raise_argument_error('mset') if pairs.empty? || pairs.size == 1
raise_argument_error("mset", "mset_odd") if pairs.size.odd?
pairs.each_slice(2) do |pair|
data[pair[0].to_s] = pair[1].to_s
end
"OK"
end
|
#msetnx(*pairs) ⇒ Object
974
975
976
977
978
979
980
981
982
|
# File 'lib/redis/connection/memory.rb', line 974
def msetnx(*pairs)
pairs = pairs[0] if mapped_param?(pairs)
keys = []
pairs.each_with_index{|item, index| keys << item.to_s if index % 2 == 0}
return false if keys.any?{|key| data.key?(key) }
mset(*pairs)
true
end
|
#non_integer_expirations?(options) ⇒ Boolean
924
925
926
927
|
# File 'lib/redis/connection/memory.rb', line 924
def non_integer_expirations?(options)
(options["EX"] && !options["EX"].is_a?(Integer)) ||
(options["PX"] && !options["PX"].is_a?(Integer))
end
|
#persist(key) ⇒ Object
777
778
779
|
# File 'lib/redis/connection/memory.rb', line 777
def persist(key)
!!data.expires.delete(key)
end
|
#pexpire(key, ttl) ⇒ Object
750
751
752
753
754
|
# File 'lib/redis/connection/memory.rb', line 750
def pexpire(key, ttl)
return 0 unless data[key]
data.expires[key] = Time.now + (ttl / 1000.0)
1
end
|
#pfadd(key, member) ⇒ Object
1342
1343
1344
1345
1346
1347
1348
|
# File 'lib/redis/connection/memory.rb', line 1342
def pfadd(key, member)
data_type_check(key, Set)
data[key] ||= Set.new
previous_size = data[key].size
data[key] |= Array(member)
data[key].size != previous_size
end
|
#pfcount(*keys) ⇒ Object
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
|
# File 'lib/redis/connection/memory.rb', line 1350
def pfcount(*keys)
keys = keys.flatten
raise_argument_error("pfcount") if keys.empty?
keys.each { |key| data_type_check(key, Set) }
if keys.count == 1
(data[keys.first] || Set.new).size
else
union = keys.map { |key| data[key] }.compact.reduce(&:|)
union.size
end
end
|
#pfmerge(destination, *sources) ⇒ Object
1362
1363
1364
1365
1366
1367
|
# File 'lib/redis/connection/memory.rb', line 1362
def pfmerge(destination, *sources)
sources.each { |source| data_type_check(source, Set) }
union = sources.map { |source| data[source] || Set.new }.reduce(&:|)
data[destination] = union
"OK"
end
|
#ping ⇒ Object
345
346
347
|
# File 'lib/redis/connection/memory.rb', line 345
def ping
"PONG"
end
|
#psetex(key, milliseconds, value) ⇒ Object
948
949
950
951
952
953
|
# File 'lib/redis/connection/memory.rb', line 948
def psetex(key, milliseconds, value)
raise_command_error('ERR value is not an integer or out of range') unless milliseconds.is_a?(Integer)
data[key] = value.to_s
expire(key, milliseconds / 1000.0)
"OK"
end
|
#psubscribe(*patterns) ⇒ Object
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
|
# File 'lib/redis/connection/memory.rb', line 1391
def psubscribe(*patterns)
raise_argument_error('psubscribe') if patterns.empty?()
channel_replies = self.class.channels.keys.map do |channel|
pattern = patterns.find{|p| File.fnmatch(p, channel) }
unless pattern.nil?()
self.class.channels[channel].slice!(0..-1).map!{|v| ["pmessage", pattern, channel, v]}
end
end
channel_replies.flatten!(1)
channel_replies.compact!()
patterns.each_with_index do |pattern,index|
replies << ["psubscribe", pattern, index+1]
end
replies.push(*channel_replies)
replies.push(self.punsubscribe())
replies.pop() end
|
#pttl(key) ⇒ Object
764
765
766
767
768
769
770
|
# File 'lib/redis/connection/memory.rb', line 764
def pttl(key)
if data.expires.include?(key) && (ttl = data.expires[key].to_f - Time.now.to_f) > 0
ttl * 1000
else
exists(key) > 0 ? -1 : -2
end
end
|
#publish(channel, message) ⇒ Object
1416
1417
1418
1419
|
# File 'lib/redis/connection/memory.rb', line 1416
def publish(channel, message)
self.class.channels[channel] << message
0 end
|
#punsubscribe(*patterns) ⇒ Object
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
|
# File 'lib/redis/connection/memory.rb', line 1432
def punsubscribe(*patterns)
if patterns.empty?()
replies << ["punsubscribe", nil, 0]
else
patterns.each do |pattern|
replies << ["punsubscribe", pattern, 0]
end
end
replies.pop() end
|
#quit ⇒ Object
1020
|
# File 'lib/redis/connection/memory.rb', line 1020
def quit ; end
|
#randomkey ⇒ Object
337
338
339
|
# File 'lib/redis/connection/memory.rb', line 337
def randomkey
data.keys[rand(dbsize)]
end
|
#read ⇒ Object
111
112
113
|
# File 'lib/redis/connection/memory.rb', line 111
def read
replies.shift
end
|
#rename(key, new_key) ⇒ Object
728
729
730
731
732
733
|
# File 'lib/redis/connection/memory.rb', line 728
def rename(key, new_key)
return unless data[key]
data[new_key] = data[key]
data.expires[new_key] = data.expires[key] if data.expires.include?(key)
data.delete(key)
end
|
#renamenx(key, new_key) ⇒ Object
735
736
737
738
739
740
741
742
|
# File 'lib/redis/connection/memory.rb', line 735
def renamenx(key, new_key)
if exists(new_key) > 0
false
else
rename(key, new_key)
true
end
end
|
#restore(key, ttl, serialized_value) ⇒ Object
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
# File 'lib/redis/connection/memory.rb', line 182
def restore(key, ttl, serialized_value)
raise Redis::CommandError, "ERR Target key name is busy." if exists(key) > 0
raise Redis::CommandError, "ERR DUMP payload version or checksum are wrong" if serialized_value.nil?
parsed_value = begin
Marshal.load(serialized_value)
rescue TypeError
raise Redis::CommandError, "ERR DUMP payload version or checksum are wrong"
end
if parsed_value[:version] != FakeRedis::VERSION
raise Redis::CommandError, "ERR DUMP payload version or checksum are wrong"
end
data[key] = parsed_value[:value]
ttl = ttl.to_i expire(key, ttl / 1000) unless ttl.zero?
"OK"
end
|
#rpop(key) ⇒ Object
486
487
488
489
490
|
# File 'lib/redis/connection/memory.rb', line 486
def rpop(key)
data_type_check(key, Array)
return unless data[key]
data[key].pop
end
|
#rpoplpush(key1, key2) ⇒ Object
504
505
506
507
508
509
|
# File 'lib/redis/connection/memory.rb', line 504
def rpoplpush(key1, key2)
data_type_check(key1, Array)
rpop(key1).tap do |elem|
lpush(key2, elem) unless elem.nil?
end
end
|
#rpush(key, value) ⇒ Object
452
453
454
455
456
457
458
459
460
|
# File 'lib/redis/connection/memory.rb', line 452
def rpush(key, value)
raise_argument_error('rpush') if value.respond_to?(:each) && value.empty?
data_type_check(key, Array)
data[key] ||= []
[value].flatten.each do |val|
data[key].push(val.to_s)
end
data[key].size
end
|
#rpushx(key, value) ⇒ Object
462
463
464
465
466
467
|
# File 'lib/redis/connection/memory.rb', line 462
def rpushx(key, value)
raise_argument_error('rpushx') if value.respond_to?(:each) && value.empty?
data_type_check(key, Array)
return unless data[key]
rpush(key, value)
end
|
#sadd(key, value) ⇒ Object
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
|
# File 'lib/redis/connection/memory.rb', line 548
def sadd(key, value)
data_type_check(key, ::Set)
should_return_int = value.is_a? Array
value = Array(value)
raise_argument_error('sadd') if value.empty?
result = if data[key]
old_set = data[key].dup
data[key].merge(value.map(&:to_s))
(data[key] - old_set).size
else
data[key] = ::Set.new(value.map(&:to_s))
data[key].size
end
if Redis.sadd_returns_boolean && !should_return_int
return result == 1
end
result
end
|
#sadd?(key, value) ⇒ Boolean
572
573
574
|
# File 'lib/redis/connection/memory.rb', line 572
def sadd?(key, value)
sadd(key, value) == 1
end
|
#save ⇒ Object
152
|
# File 'lib/redis/connection/memory.rb', line 152
def save; end
|
#scan(start_cursor, *args) ⇒ Object
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
|
# File 'lib/redis/connection/memory.rb', line 1026
def scan(start_cursor, *args)
match = "*"
count = 10
if idx = args.index("MATCH")
match = args[idx + 1]
end
if idx = args.index("COUNT")
count = args[idx + 1]
end
start_cursor = start_cursor.to_i
data_type_check(start_cursor, Integer)
cursor = start_cursor
returned_keys = []
final_page = start_cursor + count >= keys(match).length
if final_page
previous_keys_been_deleted = (count >= keys(match).length)
start_index = previous_keys_been_deleted ? 0 : cursor
returned_keys = keys(match)[start_index..-1]
cursor = 0
else
end_index = start_cursor + (count - 1)
returned_keys = keys(match)[start_cursor..end_index]
cursor = start_cursor + count
end
return "#{cursor}", returned_keys
end
|
#scard(key) ⇒ Object
611
612
613
614
615
|
# File 'lib/redis/connection/memory.rb', line 611
def scard(key)
data_type_check(key, ::Set)
return 0 unless data[key]
data[key].size
end
|
#sdiff(key1, *keys) ⇒ Object
652
653
654
655
656
657
658
659
|
# File 'lib/redis/connection/memory.rb', line 652
def sdiff(key1, *keys)
keys = keys[0] if flatten?(keys)
[key1, *keys].each { |k| data_type_check(k, ::Set) }
keys = keys.map { |k| data[k] || ::Set.new }
keys.inject(data[key1] || Set.new) do |memo, set|
memo - set
end.to_a
end
|
#sdiffstore(destination, key1, *keys) ⇒ Object
661
662
663
664
665
666
667
|
# File 'lib/redis/connection/memory.rb', line 661
def sdiffstore(destination, key1, *keys)
data_type_check(destination, ::Set)
result = sdiff(key1, *keys)
data[destination] = ::Set.new(result)
result.size
end
|
#select(index) ⇒ Object
129
130
131
132
133
|
# File 'lib/redis/connection/memory.rb', line 129
def select(index)
data_type_check(index, Integer)
self.database_id = index
"OK"
end
|
#set(key, value, *array_options) ⇒ Object
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
|
# File 'lib/redis/connection/memory.rb', line 903
def set(key, value, *array_options)
option_nx = array_options.delete("NX")
option_xx = array_options.delete("XX")
return nil if option_nx && option_xx
return nil if option_nx && exists(key) > 0
return nil if option_xx && exists(key).zero?
data[key] = value.to_s
options = Hash[array_options.each_slice(2).to_a]
raise_command_error('ERR value is not an integer or out of range') if non_integer_expirations?(options)
ttl_in_seconds = options["EX"] if options["EX"]
ttl_in_seconds = options["PX"] / 1000.0 if options["PX"]
expire(key, ttl_in_seconds) if ttl_in_seconds
"OK"
end
|
#setbit(key, offset, bit) ⇒ Object
929
930
931
932
933
934
935
936
937
938
939
|
# File 'lib/redis/connection/memory.rb', line 929
def setbit(key, offset, bit)
old_val = data[key] ? data[key].unpack('B*')[0].split("") : []
size_increment = [((offset/8)+1)*8-old_val.length, 0].max
old_val += Array.new(size_increment).map{"0"}
original_val = old_val[offset].to_i
old_val[offset] = bit.to_s
new_val = ""
old_val.each_slice(8){|b| new_val = new_val + b.join("").to_i(2).chr }
data[key] = new_val
original_val
end
|
#setex(key, seconds, value) ⇒ Object
941
942
943
944
945
946
|
# File 'lib/redis/connection/memory.rb', line 941
def setex(key, seconds, value)
raise_command_error('ERR value is not an integer or out of range') unless seconds.is_a?(Integer)
data[key] = value.to_s
expire(key, seconds)
"OK"
end
|
#setnx(key, value) ⇒ Object
719
720
721
722
723
724
725
726
|
# File 'lib/redis/connection/memory.rb', line 719
def setnx(key, value)
if exists(key) > 0
0
else
set(key, value)
1
end
end
|
#setrange(key, offset, value) ⇒ Object
955
956
957
958
959
|
# File 'lib/redis/connection/memory.rb', line 955
def setrange(key, offset, value)
return unless data[key]
s = data[key][offset,value.size]
data[key][s] = value
end
|
#shutdown ⇒ Object
1022
|
# File 'lib/redis/connection/memory.rb', line 1022
def shutdown; end
|
#sinter(*keys) ⇒ Object
617
618
619
620
621
622
623
624
625
626
627
|
# File 'lib/redis/connection/memory.rb', line 617
def sinter(*keys)
keys = keys[0] if flatten?(keys)
raise_argument_error('sinter') if keys.empty?
keys.each { |k| data_type_check(k, ::Set) }
return ::Set.new if keys.any? { |k| data[k].nil? }
keys = keys.map { |k| data[k] || ::Set.new }
keys.inject do |set, key|
set & key
end.to_a
end
|
#sinterstore(destination, *keys) ⇒ Object
629
630
631
632
633
|
# File 'lib/redis/connection/memory.rb', line 629
def sinterstore(destination, *keys)
data_type_check(destination, ::Set)
result = sinter(*keys)
data[destination] = ::Set.new(result)
end
|
#sismember(key, value) ⇒ Object
542
543
544
545
546
|
# File 'lib/redis/connection/memory.rb', line 542
def sismember(key, value)
data_type_check(key, ::Set)
return false unless data[key]
data[key].include?(value.to_s)
end
|
#slaveof(host, port) ⇒ Object
1024
|
# File 'lib/redis/connection/memory.rb', line 1024
def slaveof(host, port) ; end
|
#smembers(key) ⇒ Object
536
537
538
539
540
|
# File 'lib/redis/connection/memory.rb', line 536
def smembers(key)
data_type_check(key, ::Set)
return [] unless data[key]
data[key].to_a.reverse
end
|
#smove(source, destination, value) ⇒ Object
594
595
596
597
598
599
|
# File 'lib/redis/connection/memory.rb', line 594
def smove(source, destination, value)
data_type_check(destination, ::Set)
result = self.srem(source, value)
self.sadd(destination, value) if result
result
end
|
#spop(key, count = nil) ⇒ Object
601
602
603
604
605
606
607
608
609
|
# File 'lib/redis/connection/memory.rb', line 601
def spop(key, count = nil)
data_type_check(key, ::Set)
results = (count || 1).times.map do
elem = srandmember(key)
srem(key, elem) if elem
elem
end.compact
count.nil? ? results.first : results
end
|
#srandmember(key, number = nil) ⇒ Object
669
670
671
|
# File 'lib/redis/connection/memory.rb', line 669
def srandmember(key, number=nil)
number.nil? ? srandmember_single(key) : srandmember_multiple(key, number)
end
|
#srem(key, value) ⇒ Object
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
# File 'lib/redis/connection/memory.rb', line 576
def srem(key, value)
data_type_check(key, ::Set)
raise_argument_error('srem') if Array(value).empty?
return false unless data[key]
if value.is_a?(Array)
old_size = data[key].size
values = value.map(&:to_s)
values.each { |v| data[key].delete(v) }
deleted = old_size - data[key].size
else
deleted = !!data[key].delete?(value.to_s)
end
remove_key_for_empty_collection(key)
deleted
end
|
#sscan(key, start_cursor, *args) ⇒ Object
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
|
# File 'lib/redis/connection/memory.rb', line 673
def sscan(key, start_cursor, *args)
data_type_check(key, ::Set)
return ["0", []] unless data[key]
match = "*"
count = 10
if args.size.odd?
raise_argument_error('sscan')
end
if idx = args.index("MATCH")
match = args[idx + 1]
end
if idx = args.index("COUNT")
count = args[idx + 1]
end
start_cursor = start_cursor.to_i
cursor = start_cursor
next_keys = []
if start_cursor + count >= data[key].length
next_keys = (data[key].to_a)[start_cursor..-1]
cursor = 0
else
cursor = start_cursor + count
next_keys = (data[key].to_a)[start_cursor..cursor-1]
end
filtered_next_keys = next_keys.select{ |k,v| File.fnmatch(match, k)}
result = filtered_next_keys.flatten.map(&:to_s)
return ["#{cursor}", result]
end
|
#strlen(key) ⇒ Object
255
256
257
258
|
# File 'lib/redis/connection/memory.rb', line 255
def strlen(key)
return unless data[key]
data[key].size
end
|
#subscribe(*channels) ⇒ Object
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
|
# File 'lib/redis/connection/memory.rb', line 1369
def subscribe(*channels)
raise_argument_error('subscribe') if channels.empty?()
channel_replies = channels.map do |channel|
self.class.channels[channel].slice!(0..-1).map!{|v| ["message", channel, v]}
end
channel_replies.flatten!(1)
channel_replies.compact!()
channels.each_with_index do |channel,index|
replies << ["subscribe", channel, index+1]
end
replies.push(*channel_replies)
replies.push(self.unsubscribe())
replies.pop() end
|
#sunion(*keys) ⇒ Object
635
636
637
638
639
640
641
642
643
644
|
# File 'lib/redis/connection/memory.rb', line 635
def sunion(*keys)
keys = keys[0] if flatten?(keys)
raise_argument_error('sunion') if keys.empty?
keys.each { |k| data_type_check(k, ::Set) }
keys = keys.map { |k| data[k] || ::Set.new }
keys.inject(::Set.new) do |set, key|
set | key
end.to_a
end
|
#sunionstore(destination, *keys) ⇒ Object
646
647
648
649
650
|
# File 'lib/redis/connection/memory.rb', line 646
def sunionstore(destination, *keys)
data_type_check(destination, ::Set)
result = sunion(*keys)
data[destination] = ::Set.new(result)
end
|
#sync ⇒ Object
901
|
# File 'lib/redis/connection/memory.rb', line 901
def sync ; end
|
#time ⇒ Object
353
354
355
356
|
# File 'lib/redis/connection/memory.rb', line 353
def time
microseconds = (Time.now.to_f * 1000000).to_i
[ microseconds / 1000000, microseconds % 1000000 ]
end
|
#timeout=(usecs) ⇒ Object
108
109
|
# File 'lib/redis/connection/memory.rb', line 108
def timeout=(usecs)
end
|
#ttl(key) ⇒ Object
756
757
758
759
760
761
762
|
# File 'lib/redis/connection/memory.rb', line 756
def ttl(key)
if data.expires.include?(key) && (ttl = data.expires[key].to_i - Time.now.to_i) > 0
ttl
else
exists(key) > 0 ? -1 : -2
end
end
|
#type(key) ⇒ Object
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
|
# File 'lib/redis/connection/memory.rb', line 1009
def type(key)
case data[key]
when nil then "none"
when String then "string"
when ZSet then "zset"
when Hash then "hash"
when Array then "list"
when ::Set then "set"
end
end
|
#unlink(*keys) ⇒ Object
715
716
717
|
# File 'lib/redis/connection/memory.rb', line 715
def unlink(*keys)
delete_keys(keys, 'unlink')
end
|
#unsubscribe(*channels) ⇒ Object
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
|
# File 'lib/redis/connection/memory.rb', line 1421
def unsubscribe(*channels)
if channels.empty?()
replies << ["unsubscribe", nil, 0]
else
channels.each do |channel|
replies << ["unsubscribe", channel, 0]
end
end
replies.pop() end
|
#zadd(key, *args) ⇒ Object
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
|
# File 'lib/redis/connection/memory.rb', line 1060
def zadd(key, *args)
option_xx = args.delete("XX")
option_nx = args.delete("NX")
option_ch = args.delete("CH")
option_incr = args.delete("INCR")
if option_xx && option_nx
raise_options_error("XX", "NX")
end
if option_incr && args.size > 2
raise_options_error("INCR")
end
if !args.first.is_a?(Array)
if args.size < 2
raise_argument_error('zadd')
elsif args.size.odd?
raise_syntax_error
end
else
unless args.all? {|pair| pair.size == 2 }
raise_syntax_error
end
end
data_type_check(key, ZSet)
data[key] ||= ZSet.new
args = args.each_slice(2).to_a unless args.first.is_a?(Array)
changed = 0
exists = args.map(&:last).count { |el| !hexists(key, el.to_s) }
args.each do |score, value|
if option_nx && hexists(key, value.to_s)
next
end
if option_xx && !hexists(key, value.to_s)
exists -= 1
next
end
if option_incr
data[key][value.to_s] ||= 0
return data[key].increment(value, score).to_s
end
if option_ch && data[key][value.to_s] != score
changed += 1
end
data[key][value.to_s] = score
end
if option_incr
changed = changed.zero? ? nil : changed
exists = exists.zero? ? nil : exists
end
option_ch ? changed : exists
end
|
#zcard(key) ⇒ Object
1167
1168
1169
1170
|
# File 'lib/redis/connection/memory.rb', line 1167
def zcard(key)
data_type_check(key, ZSet)
data[key] ? data[key].size : 0
end
|
#zcount(key, min, max) ⇒ Object
1184
1185
1186
1187
1188
|
# File 'lib/redis/connection/memory.rb', line 1184
def zcount(key, min, max)
data_type_check(key, ZSet)
return 0 unless data[key]
data[key].select_by_score(min, max).size
end
|
#zincrby(key, num, value) ⇒ Object
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
|
# File 'lib/redis/connection/memory.rb', line 1190
def zincrby(key, num, value)
data_type_check(key, ZSet)
data[key] ||= ZSet.new
data[key][value.to_s] ||= 0
data[key].increment(value.to_s, num)
if num.is_a?(String) && num =~ /^\+?inf/
"inf"
elsif num == "-inf"
"-inf"
else
data[key][value.to_s].to_s
end
end
|
#zinterstore(out, *args) ⇒ Object
#zpopmax(key, count = nil) ⇒ Object
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
|
# File 'lib/redis/connection/memory.rb', line 1137
def zpopmax(key, count = nil)
data_type_check(key, ZSet)
return [] unless data[key]
sorted_members = sort_keys(data[key])
results = sorted_members.last(count || 1).reverse!
results.each do |member|
zrem(key, member.first)
end
count.nil? ? results.first : results.flatten
end
|
#zpopmin(key, count = nil) ⇒ Object
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
|
# File 'lib/redis/connection/memory.rb', line 1148
def zpopmin(key, count = nil)
data_type_check(key, ZSet)
return [] unless data[key]
sorted_members = sort_keys(data[key])
results = sorted_members.first(count || 1)
results.each do |member|
zrem(key, member.first)
end
count.nil? ? results.first : results.flatten
end
|
#zrange(key, start, stop, with_scores = nil) ⇒ Object
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
|
# File 'lib/redis/connection/memory.rb', line 1219
def zrange(key, start, stop, with_scores = nil)
data_type_check(key, ZSet)
return [] unless data[key]
results = sort_keys(data[key])
results = results.map(&:first) unless with_scores
start = [start, -results.size].max
(results[start..stop] || []).flatten.map(&:to_s)
end
|
#zrangebylex(key, start, stop, *opts) ⇒ Object
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
|
# File 'lib/redis/connection/memory.rb', line 1230
def zrangebylex(key, start, stop, *opts)
data_type_check(key, ZSet)
return [] unless data[key]
zset = data[key]
sorted = if zset.identical_scores?
zset.keys.sort { |x, y| x.to_s <=> y.to_s }
else
zset.keys
end
range = get_range start, stop, sorted.first, sorted.last
filtered = []
sorted.each do |element|
filtered << element if (range[0][:value]..range[1][:value]).cover?(element)
end
filtered.shift if filtered[0] == range[0][:value] && !range[0][:inclusive]
filtered.pop if filtered.last == range[1][:value] && !range[1][:inclusive]
limit = get_limit(opts, filtered)
if limit
filtered = filtered[limit[0]..-1].take(limit[1])
end
filtered
end
|
#zrangebyscore(key, min, max, *opts) ⇒ Object
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
|
# File 'lib/redis/connection/memory.rb', line 1273
def zrangebyscore(key, min, max, *opts)
data_type_check(key, ZSet)
return [] unless data[key]
range = data[key].select_by_score(min, max)
vals = if opts.include?('WITHSCORES')
range.sort_by {|_,v| v }
else
range.keys.sort_by {|k| range[k] }
end
limit = get_limit(opts, vals)
vals = vals[*limit] if limit
vals.flatten.map(&:to_s)
end
|
#zrank(key, value) ⇒ Object
1205
1206
1207
1208
1209
1210
|
# File 'lib/redis/connection/memory.rb', line 1205
def zrank(key, value)
data_type_check(key, ZSet)
z = data[key]
return unless z
z.keys.sort_by {|k| z[k] }.index(value.to_s)
end
|
#zrem(key, value) ⇒ Object
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
|
# File 'lib/redis/connection/memory.rb', line 1124
def zrem(key, value)
data_type_check(key, ZSet)
values = Array(value)
return 0 unless data[key]
response = values.map do |v|
data[key].delete(v.to_s) if data[key].has_key?(v.to_s)
end.compact.size
remove_key_for_empty_collection(key)
response
end
|
#zremrangebyrank(key, start, stop) ⇒ Object
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
|
# File 'lib/redis/connection/memory.rb', line 1317
def zremrangebyrank(key, start, stop)
data_type_check(key, ZSet)
return 0 unless data[key]
sorted_elements = data[key].sort_by { |k, v| v }
start = sorted_elements.length if start > sorted_elements.length
elements_to_delete = sorted_elements[start..stop]
elements_to_delete.each { |elem, rank| data[key].delete(elem) }
elements_to_delete.size
end
|
#zremrangebyscore(key, min, max) ⇒ Object
1308
1309
1310
1311
1312
1313
1314
1315
|
# File 'lib/redis/connection/memory.rb', line 1308
def zremrangebyscore(key, min, max)
data_type_check(key, ZSet)
return 0 unless data[key]
range = data[key].select_by_score(min, max)
range.each {|k,_| data[key].delete(k) }
range.size
end
|
#zrevrange(key, start, stop, with_scores = nil) ⇒ Object
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
|
# File 'lib/redis/connection/memory.rb', line 1262
def zrevrange(key, start, stop, with_scores = nil)
data_type_check(key, ZSet)
return [] unless data[key]
if with_scores
data[key].sort_by {|_,v| -v }
else
data[key].keys.sort_by {|k| -data[key][k] }
end[start..stop].flatten.map(&:to_s)
end
|
#zrevrangebylex(key, start, stop, *args) ⇒ Object
1258
1259
1260
|
# File 'lib/redis/connection/memory.rb', line 1258
def zrevrangebylex(key, start, stop, *args)
zrangebylex(key, stop, start, args).reverse
end
|
#zrevrangebyscore(key, max, min, *opts) ⇒ Object
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
|
# File 'lib/redis/connection/memory.rb', line 1290
def zrevrangebyscore(key, max, min, *opts)
opts = opts.flatten
data_type_check(key, ZSet)
return [] unless data[key]
range = data[key].select_by_score(min, max)
vals = if opts.include?('WITHSCORES')
range.sort_by {|_,v| -v }
else
range.keys.sort_by {|k| -range[k] }
end
limit = get_limit(opts, vals)
vals = vals[*limit] if limit
vals.flatten.map(&:to_s)
end
|
#zrevrank(key, value) ⇒ Object
1212
1213
1214
1215
1216
1217
|
# File 'lib/redis/connection/memory.rb', line 1212
def zrevrank(key, value)
data_type_check(key, ZSet)
z = data[key]
return unless z
z.keys.sort_by {|k| -z[k] }.index(value.to_s)
end
|
#zscan(key, start_cursor, *args) ⇒ Object
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
|
# File 'lib/redis/connection/memory.rb', line 1443
def zscan(key, start_cursor, *args)
data_type_check(key, ZSet)
return [] unless data[key]
match = "*"
count = 10
if args.size.odd?
raise_argument_error('zscan')
end
if idx = args.index("MATCH")
match = args[idx + 1]
end
if idx = args.index("COUNT")
count = args[idx + 1]
end
start_cursor = start_cursor.to_i
data_type_check(start_cursor, Integer)
cursor = start_cursor
next_keys = []
sorted_keys = sort_keys(data[key])
if start_cursor + count >= sorted_keys.length
next_keys = sorted_keys.to_a.select { |k| File.fnmatch(match, k[0]) } [start_cursor..-1]
cursor = 0
else
cursor = start_cursor + count
next_keys = sorted_keys.to_a.select { |k| File.fnmatch(match, k[0]) } [start_cursor..cursor-1]
end
return "#{cursor}", next_keys.flatten.map(&:to_s)
end
|
#zscan_each(key, *args, &block) ⇒ Object
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
|
# File 'lib/redis/connection/memory.rb', line 1481
def zscan_each(key, *args, &block)
data_type_check(key, ZSet)
return [] unless data[key]
return to_enum(:zscan_each, key, options) unless block_given?
cursor = 0
loop do
cursor, values = zscan(key, cursor, options)
values.each(&block)
break if cursor == "0"
end
end
|
#zscore(key, value) ⇒ Object
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
|
# File 'lib/redis/connection/memory.rb', line 1172
def zscore(key, value)
data_type_check(key, ZSet)
value = data[key] && data[key][value.to_s]
if value == Float::INFINITY
"inf"
elsif value == -Float::INFINITY
"-inf"
elsif value
value.to_s
end
end
|
#zunionstore(out, *args) ⇒ Object