Skip to content

Commit b0a0c11

Browse files
add generic unlink,touch,wait*,type commands
Signed-off-by: Mohsen Alizadeh <mohsen@alizadeh.us>
1 parent 9c77cd2 commit b0a0c11

3 files changed

Lines changed: 65 additions & 9 deletions

File tree

lib/valkey/commands/generic_commands.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ def del(*keys)
262262
#
263263
# @param [String, Array<String>] keys
264264
# @return [Integer] number of keys that were unlinked
265-
# def unlink(*keys)
266-
# send_command([:unlink] + keys)
267-
# end
265+
def unlink(*keys)
266+
send_command(RequestType::UNLINK, keys.flatten)
267+
end
268268

269269
# Determine how many of the keys exists.
270270
#
@@ -425,13 +425,25 @@ def sort(key, by: nil, limit: nil, get: nil, order: nil, store: nil)
425425
end
426426
end
427427

428+
def touch(*keys)
429+
send_command(RequestType::TOUCH, keys.flatten)
430+
end
431+
432+
def wait(*keys)
433+
send_command(RequestType::WAIT, keys.flatten)
434+
end
435+
436+
def waitof(*keys)
437+
send_command(RequestType::WAIT_AOF, keys.flatten)
438+
end
439+
428440
# Determine the type stored at key.
429441
#
430442
# @param [String] key
431443
# @return [String] `string`, `list`, `set`, `zset`, `hash` or `none`
432-
# def type(key)
433-
# send_command([:type, key])
434-
# end
444+
def type(key)
445+
send_command(RequestType::TYPE, [key])
446+
end
435447

436448
def _scan(command, cursor, args, match: nil, count: nil, type: nil, &block)
437449
# SSCAN/ZSCAN/HSCAN already prepend the key to +args+.

test/lint/generic_commands.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
module Lint
44
module GenericCommands
5+
def set_some_keys
6+
valkey.set('key1', 'Hello')
7+
valkey.set('key2', 'World')
8+
9+
valkey.set('{key}1', 'Hello')
10+
valkey.set('{key}2', 'World')
11+
end
12+
513
def test_copy
614
target_version("6.2") do
715
with_db(14) do
@@ -312,5 +320,37 @@ def test_renamenx
312320
assert_equal "s1", r.get("foo")
313321
assert_equal "s2", r.get("bar")
314322
end
323+
324+
def test_scan
325+
set_some_keys
326+
327+
cursor = 0
328+
all_keys = []
329+
loop do
330+
cursor, keys = valkey.scan(cursor, match: '{key}*')
331+
all_keys += keys
332+
break if cursor == '0'
333+
end
334+
335+
assert_equal 2, all_keys.uniq.size
336+
end
337+
338+
def test_type
339+
assert_equal "none", r.type("foo")
340+
341+
r.set("foo", "s1")
342+
343+
assert_equal "string", r.type("foo")
344+
end
345+
346+
def test_ttl
347+
r.set("foo", "s1")
348+
r.expire("foo", 2)
349+
assert_in_range 0..2, r.ttl("foo")
350+
end
351+
352+
def test_wait
353+
assert_equal r.wait(0, 0), 0
354+
end
315355
end
316356
end

test/test_helper.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,20 @@ def target_version(target)
125125
end
126126
end
127127

128-
def all_keys
128+
def keys(pattern = "*")
129129
list = []
130130

131131
loop do
132-
cursor, keys = r.scan(0, match: "*", count: 100)
132+
cursor, keys = r.scan(0, match: pattern, count: 100)
133133
list.concat(keys)
134134
break if cursor == "0"
135135
end
136136

137-
list.sort
137+
list
138+
end
139+
140+
def all_keys
141+
keys.sort
138142
end
139143

140144
def with_db(index)

0 commit comments

Comments
 (0)