Class: MockRedis::Database
Constant Summary
GeospatialMethods::D_R, GeospatialMethods::EARTH_RADIUS_IN_METERS, GeospatialMethods::LAT_RANGE, GeospatialMethods::LNG_RANGE, GeospatialMethods::STEP, GeospatialMethods::UNITS
Constants included
from InfoMethod
InfoMethod::ALL_INFO, InfoMethod::CLIENTS_INFO, InfoMethod::COMMAND_STATS_COMBINED_INFO, InfoMethod::COMMAND_STATS_SOLO_INFO, InfoMethod::CPU_INFO, InfoMethod::DEFAULT_INFO, InfoMethod::KEYSPACE_INFO, InfoMethod::MEMORY_INFO, InfoMethod::PERSISTENCE_INFO, InfoMethod::REPLICATION_INFO, InfoMethod::SERVER_INFO, InfoMethod::STATS_INFO
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#auth(_) ⇒ Object
Redis commands go below this line and above ‘private’.
-
#bgrewriteaof ⇒ Object
-
#bgsave ⇒ Object
-
#connected? ⇒ Boolean
-
#dbsize ⇒ Object
-
#del(*keys) ⇒ Object
(also: #unlink)
-
#disconnect ⇒ Object
-
#echo(msg) ⇒ Object
-
#eval(*args) ⇒ Object
-
#evalsha(*args) ⇒ Object
-
#exists(key) ⇒ Object
-
#expire(key, seconds) ⇒ Object
-
#expire_keys ⇒ Object
This method isn’t private, but it also isn’t a Redis command, so it doesn’t belong up above with all the Redis commands.
-
#expireat(key, timestamp) ⇒ Object
-
#flushdb ⇒ Object
-
#initialize(base, *_args) ⇒ Database
constructor
A new instance of Database.
-
#initialize_copy(_source) ⇒ Object
-
#keys(format = '*') ⇒ Object
-
#lastsave ⇒ Object
-
#persist(key) ⇒ Object
-
#pexpire(key, ms) ⇒ Object
-
#pexpireat(key, timestamp_ms) ⇒ Object
-
#ping ⇒ Object
-
#pttl(key) ⇒ Object
-
#quit ⇒ Object
-
#randomkey ⇒ Object
-
#rename(key, newkey) ⇒ Object
-
#renamenx(key, newkey) ⇒ Object
-
#save ⇒ Object
-
#scan(cursor, opts = {}) ⇒ Object
-
#scan_each(opts = {}, &block) ⇒ Object
-
#script(subcommand, *args) ⇒ Object
-
#ttl(key) ⇒ Object
-
#type(key) ⇒ Object
#xadd, #xlen, #xrange, #xrevrange, #xtrim
#geoadd, #geodist, #geohash, #geopos
Methods included from InfoMethod
#info
Methods included from SortMethod
#sort
#zadd, #zcard, #zcount, #zincrby, #zinterstore, #zrange, #zrangebyscore, #zrank, #zrem, #zremrangebyrank, #zremrangebyscore, #zrevrange, #zrevrangebyscore, #zrevrank, #zscan, #zscan_each, #zscore, #zunionstore
#append, #bitcount, #bitfield, #decr, #decrby, #get, #getbit, #getrange, #getset, #incr, #incrby, #incrbyfloat, #mapped_mget, #mapped_mset, #mapped_msetnx, #mget, #mset, #msetnx, #set, #setbit, #setex, #setnx, #setrange, #strlen
Methods included from SetMethods
#sadd, #scard, #sdiff, #sdiffstore, #sinter, #sinterstore, #sismember, #smembers, #smove, #spop, #srandmember, #srem, #sscan, #sscan_each, #sunion, #sunionstore
#blpop, #brpop, #brpoplpush, #lindex, #linsert, #llen, #lpop, #lpush, #lpushx, #lrange, #lrem, #lset, #ltrim, #rpop, #rpoplpush, #rpush, #rpushx
#hdel, #hexists, #hget, #hgetall, #hincrby, #hincrbyfloat, #hkeys, #hlen, #hmget, #hmset, #hscan, #hscan_each, #hset, #hsetnx, #hvals, #mapped_hmget, #mapped_hmset
Constructor Details
#initialize(base, *_args) ⇒ Database
Returns a new instance of Database.
30
31
32
33
34
|
# File 'lib/mock_redis/database.rb', line 30
def initialize(base, *_args)
@base = base
@data = MockRedis::IndifferentHash.new
@expire_times = []
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
28
29
30
|
# File 'lib/mock_redis/database.rb', line 28
def data
@data
end
|
#expire_times ⇒ Object
Returns the value of attribute expire_times.
28
29
30
|
# File 'lib/mock_redis/database.rb', line 28
def expire_times
@expire_times
end
|
Instance Method Details
#auth(_) ⇒ Object
Redis commands go below this line and above ‘private’
44
45
46
|
# File 'lib/mock_redis/database.rb', line 44
def auth(_)
'OK'
end
|
#bgrewriteaof ⇒ Object
48
49
50
|
# File 'lib/mock_redis/database.rb', line 48
def bgrewriteaof
'Background append only file rewriting started'
end
|
#bgsave ⇒ Object
52
53
54
|
# File 'lib/mock_redis/database.rb', line 52
def bgsave
'Background saving started'
end
|
#connected? ⇒ Boolean
60
61
62
|
# File 'lib/mock_redis/database.rb', line 60
def connected?
true
end
|
#dbsize ⇒ Object
64
65
66
|
# File 'lib/mock_redis/database.rb', line 64
def dbsize
data.keys.length
end
|
#del(*keys) ⇒ Object
Also known as:
unlink
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/mock_redis/database.rb', line 68
def del(*keys)
keys = keys.flatten.map(&:to_s)
assert_has_args(keys, 'del')
keys.
find_all { |key| data[key] }.
each { |k| persist(k) }.
each { |k| data.delete(k) }.
length
end
|
#disconnect ⇒ Object
56
57
58
|
# File 'lib/mock_redis/database.rb', line 56
def disconnect
nil
end
|
#echo(msg) ⇒ Object
80
81
82
|
# File 'lib/mock_redis/database.rb', line 80
def echo(msg)
msg.to_s
end
|
#eval(*args) ⇒ Object
242
|
# File 'lib/mock_redis/database.rb', line 242
def eval(*args); end
|
#evalsha(*args) ⇒ Object
240
|
# File 'lib/mock_redis/database.rb', line 240
def evalsha(*args); end
|
#exists(key) ⇒ Object
115
116
117
|
# File 'lib/mock_redis/database.rb', line 115
def exists(key)
data.key?(key)
end
|
#expire(key, seconds) ⇒ Object
84
85
86
|
# File 'lib/mock_redis/database.rb', line 84
def expire(key, seconds)
pexpire(key, seconds.to_i * 1000)
end
|
#expire_keys ⇒ Object
This method isn’t private, but it also isn’t a Redis command, so it doesn’t belong up above with all the Redis commands.
323
324
325
326
327
328
329
330
331
332
333
|
# File 'lib/mock_redis/database.rb', line 323
def expire_keys
now = @base.now
to_delete = expire_times.take_while do |(time, _key)|
(time.to_r * 1_000).to_i <= (now.to_r * 1_000).to_i
end
to_delete.each do |(_time, key)|
del(key)
end
end
|
#expireat(key, timestamp) ⇒ Object
93
94
95
96
97
98
99
|
# File 'lib/mock_redis/database.rb', line 93
def expireat(key, timestamp)
unless looks_like_integer?(timestamp.to_s)
raise Redis::CommandError, 'ERR value is not an integer or out of range'
end
pexpireat(key, timestamp.to_i * 1000)
end
|
#flushdb ⇒ Object
119
120
121
122
|
# File 'lib/mock_redis/database.rb', line 119
def flushdb
data.keys.each { |k| del(k) }
'OK'
end
|
#initialize_copy(_source) ⇒ Object
36
37
38
39
40
|
# File 'lib/mock_redis/database.rb', line 36
def initialize_copy(_source)
@data = @data.clone
@data.keys.each { |k| @data[k] = @data[k].clone }
@expire_times = @expire_times.map(&:clone)
end
|
#keys(format = '*') ⇒ Object
124
125
126
|
# File 'lib/mock_redis/database.rb', line 124
def keys(format = '*')
data.keys.grep(redis_pattern_to_ruby_regex(format))
end
|
#lastsave ⇒ Object
142
143
144
|
# File 'lib/mock_redis/database.rb', line 142
def lastsave
@base.now.to_i
end
|
#persist(key) ⇒ Object
146
147
148
149
150
151
152
153
|
# File 'lib/mock_redis/database.rb', line 146
def persist(key)
if exists(key) && has_expiration?(key)
remove_expiration(key)
true
else
false
end
end
|
#pexpire(key, ms) ⇒ Object
88
89
90
91
|
# File 'lib/mock_redis/database.rb', line 88
def pexpire(key, ms)
now_ms = (@base.now.to_r * 1000).to_i
pexpireat(key, now_ms + ms.to_i)
end
|
#pexpireat(key, timestamp_ms) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/mock_redis/database.rb', line 101
def pexpireat(key, timestamp_ms)
unless looks_like_integer?(timestamp_ms.to_s)
raise Redis::CommandError, 'ERR value is not an integer or out of range'
end
if exists(key)
timestamp = Rational(timestamp_ms.to_i, 1000)
set_expiration(key, @base.time_at(timestamp))
true
else
false
end
end
|
#ping ⇒ Object
155
156
157
|
# File 'lib/mock_redis/database.rb', line 155
def ping
'PONG'
end
|
#pttl(key) ⇒ Object
210
211
212
213
214
215
216
217
218
|
# File 'lib/mock_redis/database.rb', line 210
def pttl(key)
if !exists(key)
-2
elsif has_expiration?(key)
(expiration(key).to_r * 1000).to_i - (@base.now.to_r * 1000).to_i
else
-1
end
end
|
#quit ⇒ Object
159
160
161
|
# File 'lib/mock_redis/database.rb', line 159
def quit
'OK'
end
|
#randomkey ⇒ Object
163
164
165
|
# File 'lib/mock_redis/database.rb', line 163
def randomkey
data.keys[rand(data.length)]
end
|
#rename(key, newkey) ⇒ Object
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/mock_redis/database.rb', line 167
def rename(key, newkey)
unless data.include?(key)
raise Redis::CommandError, 'ERR no such key'
end
if key != newkey
data[newkey] = data.delete(key)
if has_expiration?(key)
set_expiration(newkey, expiration(key))
remove_expiration(key)
end
end
'OK'
end
|
#renamenx(key, newkey) ⇒ Object
183
184
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/mock_redis/database.rb', line 183
def renamenx(key, newkey)
unless data.include?(key)
raise Redis::CommandError, 'ERR no such key'
end
if exists(newkey)
false
else
rename(key, newkey)
true
end
end
|
#save ⇒ Object
196
197
198
|
# File 'lib/mock_redis/database.rb', line 196
def save
'OK'
end
|
#scan(cursor, opts = {}) ⇒ Object
128
129
130
|
# File 'lib/mock_redis/database.rb', line 128
def scan(cursor, opts = {})
common_scan(data.keys, cursor, opts)
end
|
#scan_each(opts = {}, &block) ⇒ Object
132
133
134
135
136
137
138
139
140
|
# File 'lib/mock_redis/database.rb', line 132
def scan_each(opts = {}, &block)
return to_enum(:scan_each, opts) unless block_given?
cursor = 0
loop do
cursor, keys = scan(cursor, opts)
keys.each(&block)
break if cursor == '0'
end
end
|
#script(subcommand, *args) ⇒ Object
238
|
# File 'lib/mock_redis/database.rb', line 238
def script(subcommand, *args); end
|
#ttl(key) ⇒ Object
200
201
202
203
204
205
206
207
208
|
# File 'lib/mock_redis/database.rb', line 200
def ttl(key)
if !exists(key)
-2
elsif has_expiration?(key)
expiration(key).to_i - @base.now.to_i
else
-1
end
end
|
#type(key) ⇒ Object
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
# File 'lib/mock_redis/database.rb', line 220
def type(key)
if !exists(key)
'none'
elsif hashy?(key)
'hash'
elsif stringy?(key)
'string'
elsif listy?(key)
'list'
elsif sety?(key)
'set'
elsif zsety?(key)
'zset'
else
raise ArgumentError, "Not sure how #{data[key].inspect} got in here"
end
end
|