[フレーム]
Docs Pricing
Login Book a meeting Try Redis

SCAN

Syntax
SCAN cursor [MATCH pattern] [COUNT count] [TYPE type]
Available since:
Redis Open Source 2.8.0
Time complexity:
O(1) for every call. O(N) for a complete iteration, including enough command calls for the cursor to return back to 0. N is the number of elements inside the collection.
ACL categories:
@keyspace, @read, @slow,

The SCAN command and the closely related commands SSCAN, HSCAN and ZSCAN are used in order to incrementally iterate over a collection of elements.

Since these commands allow for incremental iteration, returning only a small number of elements per call, they can be used in production without the downside of commands like KEYS or SMEMBERS that may block the server for a long time (even several seconds) when called against big collections of keys or elements.

However while blocking commands like SMEMBERS are able to provide all the elements that are part of a Set in a given moment, The SCAN family of commands only offer limited guarantees about the returned elements since the collection that we incrementally iterate can change during the iteration process.

Note that SCAN, SSCAN, HSCAN and ZSCAN all work very similarly, so this documentation covers all four commands. However an obvious difference is that in the case of SSCAN, HSCAN and ZSCAN the first argument is the name of the key holding the Set, Hash or Sorted Set value. The SCAN command does not need any key name argument as it iterates keys in the current database, so the iterated object is the database itself.

SCAN basic usage

SCAN is a cursor based iterator. This means that at every call of the command, the server returns an updated cursor that the user needs to use as the cursor argument in the next call.

An iteration starts when the cursor is set to 0, and terminates when the cursor returned by the server is 0. The following is an example of SCAN iteration:

> scan 0
1) "17"
2) 1) "key:12"
 2) "key:8"
 3) "key:4"
 4) "key:14"
 5) "key:16"
 6) "key:17"
 7) "key:15"
 8) "key:10"
 9) "key:3"
 10) "key:7"
 11) "key:1"
> scan 17
1) "0"
2) 1) "key:5"
 2) "key:18"
 3) "key:0"
 4) "key:2"
 5) "key:19"
 6) "key:13"
 7) "key:6"
 8) "key:9"
 9) "key:11"

In the example above, the first call uses zero as a cursor, to start the iteration. The second call uses the cursor returned by the previous call as the first element of the reply, that is, 17.

As you can see the SCAN return value is an array of two values: the first value is the new cursor to use in the next call, the second value is an array of elements.

Since in the second call the returned cursor is 0, the server signaled to the caller that the iteration finished, and the collection was completely explored. Starting an iteration with a cursor value of 0, and calling SCAN until the returned cursor is 0 again is called a full iteration.

Return value

SCAN, SSCAN, HSCAN and ZSCAN return a two element multi-bulk reply, where the first element is a string representing an unsigned 64 bit number (the cursor), and the second element is a multi-bulk with an array of elements.

Scan guarantees

The SCAN command, and the other commands in the SCAN family, are able to provide to the user a set of guarantees associated to full iterations.

However because SCAN has very little state associated (just the cursor) it has the following drawbacks:

Number of elements returned at every SCAN call

SCAN family functions do not guarantee that the number of elements returned per call are in a given range. The commands are also allowed to return zero elements, and the client should not consider the iteration complete as long as the returned cursor is not zero.

However the number of returned elements is reasonable, that is, in practical terms SCAN may return a maximum number of elements in the order of a few tens of elements when iterating a large collection, or may return all the elements of the collection in a single call when the iterated collection is small enough to be internally represented as an encoded data structure (this happens for small Sets, Hashes and Sorted Sets).

However there is a way for the user to tune the order of magnitude of the number of returned elements per call using the COUNT option.

The COUNT option

While SCAN does not provide guarantees about the number of elements returned at every iteration, it is possible to empirically adjust the behavior of SCAN using the COUNT option. Basically with COUNT the user specifies the amount of work that should be done at every call in order to retrieve elements from the collection. This is just a hint for the implementation, however generally speaking this is what you could expect most of the times from the implementation.

Important: there is no need to use the same COUNT value for every iteration. The caller is free to change the count from one iteration to the other as required, as long as the cursor passed in the next call is the one obtained in the previous call to the command.

The MATCH option

It is possible to only iterate elements matching a given glob-style pattern, similarly to the behavior of the KEYS command that takes a pattern as its only argument.

To do so, just append the MATCH <pattern> arguments at the end of the SCAN command (it works with all the SCAN family commands).

This is an example of iteration using MATCH:

> sadd myset 1 2 3 foo foobar feelsgood
(integer) 6
> sscan myset 0 match f*
1) "0"
2) 1) "foo"
 2) "feelsgood"
 3) "foobar"
Are you tired of using redis-cli? Try Redis Insight - the developer GUI for Redis.
import redis
r = redis.Redis(decode_responses=True)
res = r.set("key1", "Hello")
print(res)
# >>> True
res = r.set("key2", "World")
print(res)
# >>> True
res = r.delete("key1", "key2", "key3")
print(res)
# >>> 2
res = r.set("key1", "Hello")
print(res)
# >>> True
res = r.exists("key1")
print(res)
# >>> 1
res = r.exists("nosuchkey")
print(res)
# >>> 0
res = r.set("key2", "World")
print(res)
# >>> True
res = r.exists("key1", "key2", "nosuchkey")
print(res)
# >>> 2
res = r.set("mykey", "Hello")
print(res)
# >>> True
res = r.expire("mykey", 10)
print(res)
# >>> True
res = r.ttl("mykey")
print(res)
# >>> 10
res = r.set("mykey", "Hello World")
print(res)
# >>> True
res = r.ttl("mykey")
print(res)
# >>> -1
res = r.expire("mykey", 10, xx=True)
print(res)
# >>> False
res = r.ttl("mykey")
print(res)
# >>> -1
res = r.expire("mykey", 10, nx=True)
print(res)
# >>> True
res = r.ttl("mykey")
print(res)
# >>> 10
res = r.set("mykey", "Hello")
print(res)
# >>> True
res = r.expire("mykey", 10)
print(res)
# >>> True
res = r.ttl("mykey")
print(res)
# >>> 10
res = r.sadd("myset", *set([1, 2, 3, "foo", "foobar", "feelsgood"]))
print(res)
# >>> 6
res = list(r.sscan_iter("myset", match="f*"))
print(res)
# >>> ['foobar', 'foo', 'feelsgood']
cursor, key = r.scan(cursor=0, match='*11*')
print(cursor, key)
cursor, key = r.scan(cursor, match='*11*')
print(cursor, key)
cursor, key = r.scan(cursor, match='*11*')
print(cursor, key)
cursor, key = r.scan(cursor, match='*11*')
print(cursor, key)
cursor, keys = r.scan(cursor, match='*11*', count=1000)
print(cursor, keys)
res = r.geoadd("geokey", (0, 0, "value"))
print(res)
# >>> 1
res = r.zadd("zkey", {"value": 1000})
print(res)
# >>> 1
res = r.type("geokey")
print(res)
# >>> zset
res = r.type("zkey")
print(res)
# >>> zset
cursor, keys = r.scan(cursor=0, _type="zset")
print(keys)
# >>> ['zkey', 'geokey']
res = r.hset("myhash", mapping={"a": 1, "b": 2})
print(res)
# >>> 2
cursor, keys = r.hscan("myhash", 0)
print(keys)
# >>> {'a': '1', 'b': '2'}
cursor, keys = r.hscan("myhash", 0, no_values=True)
print(keys)
# >>> ['a', 'b']
Python Quick-Start
import { createClient } from 'redis';
const client = createClient();
await client.connect().catch(console.error);
const delRes1 = await client.set('key1', 'Hello');
console.log(delRes1); // OK

const delRes2 = await client.set('key2', 'World');
console.log(delRes2); // OK

const delRes3 = await client.del(['key1', 'key2', 'key3']);
console.log(delRes3); // 2

const existsRes1 = await client.set('key1', 'Hello');
console.log(existsRes1); // OK

const existsRes2 = await client.exists('key1');
console.log(existsRes2); // 1

const existsRes3 = await client.exists('nosuchkey');
console.log(existsRes3); // 0

const existsRes4 = await client.set('key2', 'World');
console.log(existsRes4); // OK

const existsRes5 = await client.exists(['key1', 'key2', 'nosuchkey']);
console.log(existsRes5); // 2

const expireRes1 = await client.set('mykey', 'Hello');
console.log(expireRes1); // OK

const expireRes2 = await client.expire('mykey', 10);
console.log(expireRes2); // 1

const expireRes3 = await client.ttl('mykey');
console.log(expireRes3); // 10

const expireRes4 = await client.set('mykey', 'Hello World');
console.log(expireRes4); // OK

const expireRes5 = await client.ttl('mykey');
console.log(expireRes5); // -1

const expireRes6 = await client.expire('mykey', 10, "XX");
console.log(expireRes6); // 0

const expireRes7 = await client.ttl('mykey');
console.log(expireRes7); // -1

const expireRes8 = await client.expire('mykey', 10, "NX");
console.log(expireRes8); // 1

const expireRes9 = await client.ttl('mykey');
console.log(expireRes9); // 10

const ttlRes1 = await client.set('mykey', 'Hello');
console.log(ttlRes1); // OK

const ttlRes2 = await client.expire('mykey', 10);
console.log(ttlRes2); // 1

const ttlRes3 = await client.ttl('mykey');
console.log(ttlRes3); // 10

const scan1Res1 = await client.sAdd('myset', ['1', '2', '3', 'foo', 'foobar', 'feelsgood']);
console.log(scan1Res1); // 6

let scan1Res2 = [];
for await (const values of client.sScanIterator('myset', { MATCH: 'f*' })) {
 scan1Res2 = scan1Res2.concat(values);
}
console.log(scan1Res2); // ['foo', 'foobar', 'feelsgood']

let cursor = '0';
let scanResult;
scanResult = await client.scan(cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*', COUNT: 1000 });
console.log(scanResult.cursor, scanResult.keys);
const scan3Res1 = await client.geoAdd('geokey', { longitude: 0, latitude: 0, member: 'value' });
console.log(scan3Res1); // 1

const scan3Res2 = await client.zAdd('zkey', [{ score: 1000, value: 'value' }]);
console.log(scan3Res2); // 1

const scan3Res3 = await client.type('geokey');
console.log(scan3Res3); // zset

const scan3Res4 = await client.type('zkey');
console.log(scan3Res4); // zset

const scan3Res5 = await client.scan('0', { TYPE: 'zset' });
console.log(scan3Res5.keys); // ['zkey', 'geokey']

const scan4Res1 = await client.hSet('myhash', { a: 1, b: 2 });
console.log(scan4Res1); // 2

const scan4Res2 = await client.hScan('myhash', '0');
console.log(scan4Res2.entries); // [{field: 'a', value: '1'}, {field: 'b', value: '2'}]

const scan4Res3 = await client.hScan('myhash', '0', { COUNT: 10 });
const items = scan4Res3.entries.map((item) => item.field)
console.log(items); // ['a', 'b']

await client.close();
Node.js Quick-Start
importredis.clients.jedis.UnifiedJedis;importredis.clients.jedis.args.ExpiryOption;import staticorg.junit.jupiter.api.Assertions.assertEquals;publicclass CmdsGenericExample{publicvoidrun(){UnifiedJedisjedis=newUnifiedJedis("redis://localhost:6379");StringdelResult1=jedis.set("key1","Hello");System.out.println(delResult1);// >>> OKStringdelResult2=jedis.set("key2","World");System.out.println(delResult2);// >>> OKlongdelResult3=jedis.del("key1","key2","key3");System.out.println(delResult3);// >>> 2// Tests for 'del' step.StringexistsResult1=jedis.set("key1","Hello");System.out.println(existsResult1);// >>> OKbooleanexistsResult2=jedis.exists("key1");System.out.println(existsResult2);// >>> truebooleanexistsResult3=jedis.exists("nosuchkey");System.out.println(existsResult3);// >>> falseStringexistsResult4=jedis.set("key2","World");System.out.println(existsResult4);// >>> OKlongexistsResult5=jedis.exists("key1","key2","nosuchkey");System.out.println(existsResult5);// >>> 2// Tests for 'exists' step.StringexpireResult1=jedis.set("mykey","Hello");System.out.println(expireResult1);// >>> OKlongexpireResult2=jedis.expire("mykey",10);System.out.println(expireResult2);// >>> 1longexpireResult3=jedis.ttl("mykey");System.out.println(expireResult3);// >>> 10StringexpireResult4=jedis.set("mykey","Hello World");System.out.println(expireResult4);// >>> OKlongexpireResult5=jedis.ttl("mykey");System.out.println(expireResult5);// >>> -1longexpireResult6=jedis.expire("mykey",10,ExpiryOption.XX);System.out.println(expireResult6);// >>> 0longexpireResult7=jedis.ttl("mykey");System.out.println(expireResult7);// >>> -1longexpireResult8=jedis.expire("mykey",10,ExpiryOption.NX);System.out.println(expireResult8);// >>> 1longexpireResult9=jedis.ttl("mykey");System.out.println(expireResult9);// >>> 10// Tests for 'expire' step.StringttlResult1=jedis.set("mykey","Hello");System.out.println(ttlResult1);// >>> OKlongttlResult2=jedis.expire("mykey",10);System.out.println(ttlResult2);// >>> 1longttlResult3=jedis.ttl("mykey");System.out.println(ttlResult3);// >>> 10// Tests for 'ttl' step.jedis.close();}}
Java-Sync Quick-Start
packageio.redis.examples.async;importio.lettuce.core.*;importio.lettuce.core.api.async.RedisAsyncCommands;importio.lettuce.core.api.StatefulRedisConnection;importjava.util.concurrent.CompletableFuture;publicclass CmdsGenericExample{publicvoidrun(){CompletableFuture<Void>existsExample=asyncCommands.set("key1","Hello").thenCompose(res1->{System.out.println(res1);// >>> OKreturnasyncCommands.exists("key1");}).thenCompose(res2->{System.out.println(res2);// >>> 1returnasyncCommands.exists("nosuchkey");}).thenCompose(res3->{System.out.println(res3);// >>> 0returnasyncCommands.set("key2","World");}).thenCompose(res4->{System.out.println(res4);// >>> OKreturnasyncCommands.exists("key1","key2","nosuchkey");}).thenAccept(res5->{System.out.println(res5);// >>> 2}).toCompletableFuture();existsExample.join();}finally{redisClient.shutdown();}}}
Java-Async Quick-Start
packageio.redis.examples.reactive;importio.lettuce.core.*;importio.lettuce.core.api.reactive.RedisReactiveCommands;importio.lettuce.core.api.StatefulRedisConnection;importreactor.core.publisher.Mono;publicclass CmdsGenericExample{publicvoidrun(){RedisClientredisClient=RedisClient.create("redis://localhost:6379");try(StatefulRedisConnection<String,String>connection=redisClient.connect()){RedisReactiveCommands<String,String>reactiveCommands=connection.reactive();Mono<Void>existsExample=reactiveCommands.set("key1","Hello").doOnNext(res1->{System.out.println(res1);// >>> OK}).then(reactiveCommands.exists("key1")).doOnNext(res2->{System.out.println(res2);// >>> 1}).then(reactiveCommands.exists("nosuchkey")).doOnNext(res3->{System.out.println(res3);// >>> 0}).then(reactiveCommands.set("key2","World")).doOnNext(res4->{System.out.println(res4);// >>> OK}).then(reactiveCommands.exists("key1","key2","nosuchkey")).doOnNext(res5->{System.out.println(res5);// >>> 2}).then();Mono.when(existsExample).block();}finally{redisClient.shutdown();}}}
Java-Reactive Quick-Start
package example_commands_test
import (
	"context"
	"fmt"
	"math"
	"time"
	"github.com/redis/go-redis/v9"
)
func ExampleClient_del_cmd() {
	ctx := context.Background()
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		Password: "", // no password docs
		DB: 0, // use default DB
	})
	delResult1, err := rdb.Set(ctx, "key1", "Hello", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(delResult1) // >>> OK
	delResult2, err := rdb.Set(ctx, "key2", "World", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(delResult2) // >>> OK
	delResult3, err := rdb.Del(ctx, "key1", "key2", "key3").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(delResult3) // >>> 2
}
func ExampleClient_exists_cmd() {
	ctx := context.Background()
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		Password: "", // no password docs
		DB: 0, // use default DB
	})
	existsResult1, err := rdb.Set(ctx, "key1", "Hello", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult1) // >>> OK
	existsResult2, err := rdb.Exists(ctx, "key1").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult2) // >>> 1
	existsResult3, err := rdb.Exists(ctx, "nosuchkey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult3) // >>> 0
	existsResult4, err := rdb.Set(ctx, "key2", "World", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult4) // >>> OK
	existsResult5, err := rdb.Exists(ctx, "key1", "key2", "nosuchkey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult5) // >>> 2
}
func ExampleClient_expire_cmd() {
	ctx := context.Background()
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		Password: "", // no password docs
		DB: 0, // use default DB
	})
	expireResult1, err := rdb.Set(ctx, "mykey", "Hello", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult1) // >>> OK
	expireResult2, err := rdb.Expire(ctx, "mykey", 10*time.Second).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult2) // >>> true
	expireResult3, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(math.Round(expireResult3.Seconds())) // >>> 10
	expireResult4, err := rdb.Set(ctx, "mykey", "Hello World", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult4) // >>> OK
	expireResult5, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult5) // >>> -1ns
	expireResult6, err := rdb.ExpireXX(ctx, "mykey", 10*time.Second).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult6) // >>> false
	expireResult7, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult7) // >>> -1ns
	expireResult8, err := rdb.ExpireNX(ctx, "mykey", 10*time.Second).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult8) // >>> true
	expireResult9, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(math.Round(expireResult9.Seconds())) // >>> 10
}
func ExampleClient_ttl_cmd() {
	ctx := context.Background()
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		Password: "", // no password docs
		DB: 0, // use default DB
	})
	ttlResult1, err := rdb.Set(ctx, "mykey", "Hello", 10*time.Second).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(ttlResult1) // >>> OK
	ttlResult2, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(math.Round(ttlResult2.Seconds())) // >>> 10
}
Go Quick-Start
using NRedisStack.Tests;
using StackExchange.Redis;
public class CmdsGenericExample
{
 public void Run()
 {
 var muxer = ConnectionMultiplexer.Connect("localhost:6379");
 var db = muxer.GetDatabase();
 // Tests for 'copy' step.
 bool delResult1 = db.StringSet("key1", "Hello");
 Console.WriteLine(delResult1); // >>> true
 bool delResult2 = db.StringSet("key2", "World");
 Console.WriteLine(delResult2); // >>> true
 long delResult3 = db.KeyDelete(["key1", "key2", "key3"]);
 Console.WriteLine(delResult3); // >>> 2
 // Tests for 'del' step.
 // Tests for 'dump' step.
 bool existsResult1 = db.StringSet("key1", "Hello");
 Console.WriteLine(existsResult1); // >>> true
 bool existsResult2 = db.KeyExists("key1");
 Console.WriteLine(existsResult2); // >>> true
 bool existsResult3 = db.KeyExists("nosuchkey");
 Console.WriteLine(existsResult3); // >>> false
 bool existsResult4 = db.StringSet("key2", "World");
 Console.WriteLine(existsResult4); // >>> true
 long existsResult5 = db.KeyExists(["key1", "key2", "nosuchkey"]);
 Console.WriteLine(existsResult5); // >>> 2
 // Tests for 'exists' step.
 bool expireResult1 = db.StringSet("mykey", "Hello");
 Console.WriteLine(expireResult1); // >>> true
 bool expireResult2 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10));
 Console.WriteLine(expireResult2); // >>> true
 TimeSpan expireResult3 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 Console.WriteLine(Math.Round(expireResult3.TotalSeconds)); // >>> 10
 bool expireResult4 = db.StringSet("mykey", "Hello World");
 Console.WriteLine(expireResult4); // >>> true
 TimeSpan expireResult5 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 Console.WriteLine(Math.Round(expireResult5.TotalSeconds).ToString()); // >>> 0
 bool expireResult6 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10), ExpireWhen.HasExpiry);
 Console.WriteLine(expireResult6); // >>> false
 TimeSpan expireResult7 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 Console.WriteLine(Math.Round(expireResult7.TotalSeconds)); // >>> 0
 bool expireResult8 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10), ExpireWhen.HasNoExpiry);
 Console.WriteLine(expireResult8); // >>> true
 TimeSpan expireResult9 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 Console.WriteLine(Math.Round(expireResult9.TotalSeconds)); // >>> 10
 // Tests for 'expire' step.
 // Tests for 'expireat' step.
 // Tests for 'expiretime' step.
 // Tests for 'keys' step.
 // Tests for 'migrate' step.
 // Tests for 'move' step.
 // Tests for 'object_encoding' step.
 // Tests for 'object_freq' step.
 // Tests for 'object_idletime' step.
 // Tests for 'object_refcount' step.
 // Tests for 'persist' step.
 // Tests for 'pexpire' step.
 // Tests for 'pexpireat' step.
 // Tests for 'pexpiretime' step.
 // Tests for 'pttl' step.
 // Tests for 'randomkey' step.
 // Tests for 'rename' step.
 // Tests for 'renamenx' step.
 // Tests for 'restore' step.
 // Tests for 'scan1' step.
 // Tests for 'scan2' step.
 // Tests for 'scan3' step.
 // Tests for 'scan4' step.
 // Tests for 'sort' step.
 // Tests for 'sort_ro' step.
 // Tests for 'touch' step.
 bool ttlResult1 = db.StringSet("mykey", "Hello");
 Console.WriteLine(ttlResult1); // >>> true
 bool ttlResult2 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10));
 Console.WriteLine(ttlResult2);
 TimeSpan ttlResult3 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 string ttlRes = Math.Round(ttlResult3.TotalSeconds).ToString();
 Console.WriteLine(Math.Round(ttlResult3.TotalSeconds)); // >>> 10
 // Tests for 'ttl' step.
 // Tests for 'type' step.
 // Tests for 'unlink' step.
 // Tests for 'wait' step.
 // Tests for 'waitaof' step.
 }
}
C#-Sync Quick-Start
<?php
require_once 'vendor/autoload.php';
use Predis\Client as PredisClient;
class CmdsGenericTest
{
 public function testCmdsGeneric() {
 $r = new PredisClient([
 'scheme' => 'tcp',
 'host' => '127.0.0.1',
 'port' => 6379,
 'password' => '',
 'database' => 0,
 ]);
 $existsResult1 = $r->set('key1', 'Hello');
 echo $existsResult1 . PHP_EOL; // >>> OK

 $existsResult2 = $r->exists('key1');
 echo $existsResult2 . PHP_EOL; // >>> 1

 $existsResult3 = $r->exists('nosuchkey');
 echo $existsResult3 . PHP_EOL; // >>> 0

 $existsResult4 = $r->set('key2', 'World');
 echo $existsResult4 . PHP_EOL; // >>> OK

 $existsResult5 = $r->exists('key1', 'key2', 'nosuchkey');
 echo $existsResult5 . PHP_EOL; // >>> 2

 }
}
PHP Quick-Start

It is important to note that the MATCH filter is applied after elements are retrieved from the collection, just before returning data to the client. This means that if the pattern matches very little elements inside the collection, SCAN will likely return no elements in most iterations. An example is shown below:

> scan 0 MATCH *11*
1) "288"
2) 1) "key:911"
> scan 288 MATCH *11*
1) "224"
2) (empty list or set)
> scan 224 MATCH *11*
1) "80"
2) (empty list or set)
> scan 80 MATCH *11*
1) "176"
2) (empty list or set)
> scan 176 MATCH *11* COUNT 1000
1) "0"
2) 1) "key:611"
 2) "key:711"
 3) "key:118"
 4) "key:117"
 5) "key:311"
 6) "key:112"
 7) "key:111"
 8) "key:110"
 9) "key:113"
 10) "key:211"
 11) "key:411"
 12) "key:115"
 13) "key:116"
 14) "key:114"
 15) "key:119"
 16) "key:811"
 17) "key:511"
 18) "key:11"
Are you tired of using redis-cli? Try Redis Insight - the developer GUI for Redis.
import redis
r = redis.Redis(decode_responses=True)
res = r.set("key1", "Hello")
print(res)
# >>> True
res = r.set("key2", "World")
print(res)
# >>> True
res = r.delete("key1", "key2", "key3")
print(res)
# >>> 2
res = r.set("key1", "Hello")
print(res)
# >>> True
res = r.exists("key1")
print(res)
# >>> 1
res = r.exists("nosuchkey")
print(res)
# >>> 0
res = r.set("key2", "World")
print(res)
# >>> True
res = r.exists("key1", "key2", "nosuchkey")
print(res)
# >>> 2
res = r.set("mykey", "Hello")
print(res)
# >>> True
res = r.expire("mykey", 10)
print(res)
# >>> True
res = r.ttl("mykey")
print(res)
# >>> 10
res = r.set("mykey", "Hello World")
print(res)
# >>> True
res = r.ttl("mykey")
print(res)
# >>> -1
res = r.expire("mykey", 10, xx=True)
print(res)
# >>> False
res = r.ttl("mykey")
print(res)
# >>> -1
res = r.expire("mykey", 10, nx=True)
print(res)
# >>> True
res = r.ttl("mykey")
print(res)
# >>> 10
res = r.set("mykey", "Hello")
print(res)
# >>> True
res = r.expire("mykey", 10)
print(res)
# >>> True
res = r.ttl("mykey")
print(res)
# >>> 10
res = r.sadd("myset", *set([1, 2, 3, "foo", "foobar", "feelsgood"]))
print(res)
# >>> 6
res = list(r.sscan_iter("myset", match="f*"))
print(res)
# >>> ['foobar', 'foo', 'feelsgood']
cursor, key = r.scan(cursor=0, match='*11*')
print(cursor, key)
cursor, key = r.scan(cursor, match='*11*')
print(cursor, key)
cursor, key = r.scan(cursor, match='*11*')
print(cursor, key)
cursor, key = r.scan(cursor, match='*11*')
print(cursor, key)
cursor, keys = r.scan(cursor, match='*11*', count=1000)
print(cursor, keys)
res = r.geoadd("geokey", (0, 0, "value"))
print(res)
# >>> 1
res = r.zadd("zkey", {"value": 1000})
print(res)
# >>> 1
res = r.type("geokey")
print(res)
# >>> zset
res = r.type("zkey")
print(res)
# >>> zset
cursor, keys = r.scan(cursor=0, _type="zset")
print(keys)
# >>> ['zkey', 'geokey']
res = r.hset("myhash", mapping={"a": 1, "b": 2})
print(res)
# >>> 2
cursor, keys = r.hscan("myhash", 0)
print(keys)
# >>> {'a': '1', 'b': '2'}
cursor, keys = r.hscan("myhash", 0, no_values=True)
print(keys)
# >>> ['a', 'b']
Python Quick-Start
import { createClient } from 'redis';
const client = createClient();
await client.connect().catch(console.error);
const delRes1 = await client.set('key1', 'Hello');
console.log(delRes1); // OK

const delRes2 = await client.set('key2', 'World');
console.log(delRes2); // OK

const delRes3 = await client.del(['key1', 'key2', 'key3']);
console.log(delRes3); // 2

const existsRes1 = await client.set('key1', 'Hello');
console.log(existsRes1); // OK

const existsRes2 = await client.exists('key1');
console.log(existsRes2); // 1

const existsRes3 = await client.exists('nosuchkey');
console.log(existsRes3); // 0

const existsRes4 = await client.set('key2', 'World');
console.log(existsRes4); // OK

const existsRes5 = await client.exists(['key1', 'key2', 'nosuchkey']);
console.log(existsRes5); // 2

const expireRes1 = await client.set('mykey', 'Hello');
console.log(expireRes1); // OK

const expireRes2 = await client.expire('mykey', 10);
console.log(expireRes2); // 1

const expireRes3 = await client.ttl('mykey');
console.log(expireRes3); // 10

const expireRes4 = await client.set('mykey', 'Hello World');
console.log(expireRes4); // OK

const expireRes5 = await client.ttl('mykey');
console.log(expireRes5); // -1

const expireRes6 = await client.expire('mykey', 10, "XX");
console.log(expireRes6); // 0

const expireRes7 = await client.ttl('mykey');
console.log(expireRes7); // -1

const expireRes8 = await client.expire('mykey', 10, "NX");
console.log(expireRes8); // 1

const expireRes9 = await client.ttl('mykey');
console.log(expireRes9); // 10

const ttlRes1 = await client.set('mykey', 'Hello');
console.log(ttlRes1); // OK

const ttlRes2 = await client.expire('mykey', 10);
console.log(ttlRes2); // 1

const ttlRes3 = await client.ttl('mykey');
console.log(ttlRes3); // 10

const scan1Res1 = await client.sAdd('myset', ['1', '2', '3', 'foo', 'foobar', 'feelsgood']);
console.log(scan1Res1); // 6

let scan1Res2 = [];
for await (const values of client.sScanIterator('myset', { MATCH: 'f*' })) {
 scan1Res2 = scan1Res2.concat(values);
}
console.log(scan1Res2); // ['foo', 'foobar', 'feelsgood']

let cursor = '0';
let scanResult;
scanResult = await client.scan(cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*', COUNT: 1000 });
console.log(scanResult.cursor, scanResult.keys);
const scan3Res1 = await client.geoAdd('geokey', { longitude: 0, latitude: 0, member: 'value' });
console.log(scan3Res1); // 1

const scan3Res2 = await client.zAdd('zkey', [{ score: 1000, value: 'value' }]);
console.log(scan3Res2); // 1

const scan3Res3 = await client.type('geokey');
console.log(scan3Res3); // zset

const scan3Res4 = await client.type('zkey');
console.log(scan3Res4); // zset

const scan3Res5 = await client.scan('0', { TYPE: 'zset' });
console.log(scan3Res5.keys); // ['zkey', 'geokey']

const scan4Res1 = await client.hSet('myhash', { a: 1, b: 2 });
console.log(scan4Res1); // 2

const scan4Res2 = await client.hScan('myhash', '0');
console.log(scan4Res2.entries); // [{field: 'a', value: '1'}, {field: 'b', value: '2'}]

const scan4Res3 = await client.hScan('myhash', '0', { COUNT: 10 });
const items = scan4Res3.entries.map((item) => item.field)
console.log(items); // ['a', 'b']

await client.close();
Node.js Quick-Start
importredis.clients.jedis.UnifiedJedis;importredis.clients.jedis.args.ExpiryOption;import staticorg.junit.jupiter.api.Assertions.assertEquals;publicclass CmdsGenericExample{publicvoidrun(){UnifiedJedisjedis=newUnifiedJedis("redis://localhost:6379");StringdelResult1=jedis.set("key1","Hello");System.out.println(delResult1);// >>> OKStringdelResult2=jedis.set("key2","World");System.out.println(delResult2);// >>> OKlongdelResult3=jedis.del("key1","key2","key3");System.out.println(delResult3);// >>> 2// Tests for 'del' step.StringexistsResult1=jedis.set("key1","Hello");System.out.println(existsResult1);// >>> OKbooleanexistsResult2=jedis.exists("key1");System.out.println(existsResult2);// >>> truebooleanexistsResult3=jedis.exists("nosuchkey");System.out.println(existsResult3);// >>> falseStringexistsResult4=jedis.set("key2","World");System.out.println(existsResult4);// >>> OKlongexistsResult5=jedis.exists("key1","key2","nosuchkey");System.out.println(existsResult5);// >>> 2// Tests for 'exists' step.StringexpireResult1=jedis.set("mykey","Hello");System.out.println(expireResult1);// >>> OKlongexpireResult2=jedis.expire("mykey",10);System.out.println(expireResult2);// >>> 1longexpireResult3=jedis.ttl("mykey");System.out.println(expireResult3);// >>> 10StringexpireResult4=jedis.set("mykey","Hello World");System.out.println(expireResult4);// >>> OKlongexpireResult5=jedis.ttl("mykey");System.out.println(expireResult5);// >>> -1longexpireResult6=jedis.expire("mykey",10,ExpiryOption.XX);System.out.println(expireResult6);// >>> 0longexpireResult7=jedis.ttl("mykey");System.out.println(expireResult7);// >>> -1longexpireResult8=jedis.expire("mykey",10,ExpiryOption.NX);System.out.println(expireResult8);// >>> 1longexpireResult9=jedis.ttl("mykey");System.out.println(expireResult9);// >>> 10// Tests for 'expire' step.StringttlResult1=jedis.set("mykey","Hello");System.out.println(ttlResult1);// >>> OKlongttlResult2=jedis.expire("mykey",10);System.out.println(ttlResult2);// >>> 1longttlResult3=jedis.ttl("mykey");System.out.println(ttlResult3);// >>> 10// Tests for 'ttl' step.jedis.close();}}
Java-Sync Quick-Start
packageio.redis.examples.async;importio.lettuce.core.*;importio.lettuce.core.api.async.RedisAsyncCommands;importio.lettuce.core.api.StatefulRedisConnection;importjava.util.concurrent.CompletableFuture;publicclass CmdsGenericExample{publicvoidrun(){CompletableFuture<Void>existsExample=asyncCommands.set("key1","Hello").thenCompose(res1->{System.out.println(res1);// >>> OKreturnasyncCommands.exists("key1");}).thenCompose(res2->{System.out.println(res2);// >>> 1returnasyncCommands.exists("nosuchkey");}).thenCompose(res3->{System.out.println(res3);// >>> 0returnasyncCommands.set("key2","World");}).thenCompose(res4->{System.out.println(res4);// >>> OKreturnasyncCommands.exists("key1","key2","nosuchkey");}).thenAccept(res5->{System.out.println(res5);// >>> 2}).toCompletableFuture();existsExample.join();}finally{redisClient.shutdown();}}}
Java-Async Quick-Start
packageio.redis.examples.reactive;importio.lettuce.core.*;importio.lettuce.core.api.reactive.RedisReactiveCommands;importio.lettuce.core.api.StatefulRedisConnection;importreactor.core.publisher.Mono;publicclass CmdsGenericExample{publicvoidrun(){RedisClientredisClient=RedisClient.create("redis://localhost:6379");try(StatefulRedisConnection<String,String>connection=redisClient.connect()){RedisReactiveCommands<String,String>reactiveCommands=connection.reactive();Mono<Void>existsExample=reactiveCommands.set("key1","Hello").doOnNext(res1->{System.out.println(res1);// >>> OK}).then(reactiveCommands.exists("key1")).doOnNext(res2->{System.out.println(res2);// >>> 1}).then(reactiveCommands.exists("nosuchkey")).doOnNext(res3->{System.out.println(res3);// >>> 0}).then(reactiveCommands.set("key2","World")).doOnNext(res4->{System.out.println(res4);// >>> OK}).then(reactiveCommands.exists("key1","key2","nosuchkey")).doOnNext(res5->{System.out.println(res5);// >>> 2}).then();Mono.when(existsExample).block();}finally{redisClient.shutdown();}}}
Java-Reactive Quick-Start
package example_commands_test
import (
	"context"
	"fmt"
	"math"
	"time"
	"github.com/redis/go-redis/v9"
)
func ExampleClient_del_cmd() {
	ctx := context.Background()
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		Password: "", // no password docs
		DB: 0, // use default DB
	})
	delResult1, err := rdb.Set(ctx, "key1", "Hello", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(delResult1) // >>> OK
	delResult2, err := rdb.Set(ctx, "key2", "World", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(delResult2) // >>> OK
	delResult3, err := rdb.Del(ctx, "key1", "key2", "key3").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(delResult3) // >>> 2
}
func ExampleClient_exists_cmd() {
	ctx := context.Background()
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		Password: "", // no password docs
		DB: 0, // use default DB
	})
	existsResult1, err := rdb.Set(ctx, "key1", "Hello", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult1) // >>> OK
	existsResult2, err := rdb.Exists(ctx, "key1").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult2) // >>> 1
	existsResult3, err := rdb.Exists(ctx, "nosuchkey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult3) // >>> 0
	existsResult4, err := rdb.Set(ctx, "key2", "World", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult4) // >>> OK
	existsResult5, err := rdb.Exists(ctx, "key1", "key2", "nosuchkey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult5) // >>> 2
}
func ExampleClient_expire_cmd() {
	ctx := context.Background()
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		Password: "", // no password docs
		DB: 0, // use default DB
	})
	expireResult1, err := rdb.Set(ctx, "mykey", "Hello", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult1) // >>> OK
	expireResult2, err := rdb.Expire(ctx, "mykey", 10*time.Second).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult2) // >>> true
	expireResult3, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(math.Round(expireResult3.Seconds())) // >>> 10
	expireResult4, err := rdb.Set(ctx, "mykey", "Hello World", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult4) // >>> OK
	expireResult5, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult5) // >>> -1ns
	expireResult6, err := rdb.ExpireXX(ctx, "mykey", 10*time.Second).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult6) // >>> false
	expireResult7, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult7) // >>> -1ns
	expireResult8, err := rdb.ExpireNX(ctx, "mykey", 10*time.Second).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult8) // >>> true
	expireResult9, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(math.Round(expireResult9.Seconds())) // >>> 10
}
func ExampleClient_ttl_cmd() {
	ctx := context.Background()
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		Password: "", // no password docs
		DB: 0, // use default DB
	})
	ttlResult1, err := rdb.Set(ctx, "mykey", "Hello", 10*time.Second).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(ttlResult1) // >>> OK
	ttlResult2, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(math.Round(ttlResult2.Seconds())) // >>> 10
}
Go Quick-Start
using NRedisStack.Tests;
using StackExchange.Redis;
public class CmdsGenericExample
{
 public void Run()
 {
 var muxer = ConnectionMultiplexer.Connect("localhost:6379");
 var db = muxer.GetDatabase();
 // Tests for 'copy' step.
 bool delResult1 = db.StringSet("key1", "Hello");
 Console.WriteLine(delResult1); // >>> true
 bool delResult2 = db.StringSet("key2", "World");
 Console.WriteLine(delResult2); // >>> true
 long delResult3 = db.KeyDelete(["key1", "key2", "key3"]);
 Console.WriteLine(delResult3); // >>> 2
 // Tests for 'del' step.
 // Tests for 'dump' step.
 bool existsResult1 = db.StringSet("key1", "Hello");
 Console.WriteLine(existsResult1); // >>> true
 bool existsResult2 = db.KeyExists("key1");
 Console.WriteLine(existsResult2); // >>> true
 bool existsResult3 = db.KeyExists("nosuchkey");
 Console.WriteLine(existsResult3); // >>> false
 bool existsResult4 = db.StringSet("key2", "World");
 Console.WriteLine(existsResult4); // >>> true
 long existsResult5 = db.KeyExists(["key1", "key2", "nosuchkey"]);
 Console.WriteLine(existsResult5); // >>> 2
 // Tests for 'exists' step.
 bool expireResult1 = db.StringSet("mykey", "Hello");
 Console.WriteLine(expireResult1); // >>> true
 bool expireResult2 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10));
 Console.WriteLine(expireResult2); // >>> true
 TimeSpan expireResult3 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 Console.WriteLine(Math.Round(expireResult3.TotalSeconds)); // >>> 10
 bool expireResult4 = db.StringSet("mykey", "Hello World");
 Console.WriteLine(expireResult4); // >>> true
 TimeSpan expireResult5 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 Console.WriteLine(Math.Round(expireResult5.TotalSeconds).ToString()); // >>> 0
 bool expireResult6 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10), ExpireWhen.HasExpiry);
 Console.WriteLine(expireResult6); // >>> false
 TimeSpan expireResult7 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 Console.WriteLine(Math.Round(expireResult7.TotalSeconds)); // >>> 0
 bool expireResult8 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10), ExpireWhen.HasNoExpiry);
 Console.WriteLine(expireResult8); // >>> true
 TimeSpan expireResult9 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 Console.WriteLine(Math.Round(expireResult9.TotalSeconds)); // >>> 10
 // Tests for 'expire' step.
 // Tests for 'expireat' step.
 // Tests for 'expiretime' step.
 // Tests for 'keys' step.
 // Tests for 'migrate' step.
 // Tests for 'move' step.
 // Tests for 'object_encoding' step.
 // Tests for 'object_freq' step.
 // Tests for 'object_idletime' step.
 // Tests for 'object_refcount' step.
 // Tests for 'persist' step.
 // Tests for 'pexpire' step.
 // Tests for 'pexpireat' step.
 // Tests for 'pexpiretime' step.
 // Tests for 'pttl' step.
 // Tests for 'randomkey' step.
 // Tests for 'rename' step.
 // Tests for 'renamenx' step.
 // Tests for 'restore' step.
 // Tests for 'scan1' step.
 // Tests for 'scan2' step.
 // Tests for 'scan3' step.
 // Tests for 'scan4' step.
 // Tests for 'sort' step.
 // Tests for 'sort_ro' step.
 // Tests for 'touch' step.
 bool ttlResult1 = db.StringSet("mykey", "Hello");
 Console.WriteLine(ttlResult1); // >>> true
 bool ttlResult2 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10));
 Console.WriteLine(ttlResult2);
 TimeSpan ttlResult3 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 string ttlRes = Math.Round(ttlResult3.TotalSeconds).ToString();
 Console.WriteLine(Math.Round(ttlResult3.TotalSeconds)); // >>> 10
 // Tests for 'ttl' step.
 // Tests for 'type' step.
 // Tests for 'unlink' step.
 // Tests for 'wait' step.
 // Tests for 'waitaof' step.
 }
}
C#-Sync Quick-Start
<?php
require_once 'vendor/autoload.php';
use Predis\Client as PredisClient;
class CmdsGenericTest
{
 public function testCmdsGeneric() {
 $r = new PredisClient([
 'scheme' => 'tcp',
 'host' => '127.0.0.1',
 'port' => 6379,
 'password' => '',
 'database' => 0,
 ]);
 $existsResult1 = $r->set('key1', 'Hello');
 echo $existsResult1 . PHP_EOL; // >>> OK

 $existsResult2 = $r->exists('key1');
 echo $existsResult2 . PHP_EOL; // >>> 1

 $existsResult3 = $r->exists('nosuchkey');
 echo $existsResult3 . PHP_EOL; // >>> 0

 $existsResult4 = $r->set('key2', 'World');
 echo $existsResult4 . PHP_EOL; // >>> OK

 $existsResult5 = $r->exists('key1', 'key2', 'nosuchkey');
 echo $existsResult5 . PHP_EOL; // >>> 2

 }
}
PHP Quick-Start

As you can see most of the calls returned zero elements, but the last call where a COUNT of 1000 was used in order to force the command to do more scanning for that iteration.

When using Redis Cluster, the search is optimized for patterns that imply a single slot. If a pattern can only match keys of one slot, Redis only iterates over keys in that slot, rather than the whole database, when searching for keys matching the pattern. For example, with the pattern {a}h*llo, Redis would only try to match it with the keys in slot 15495, which hash tag {a} implies. To use pattern with hash tag, see Hash tags in the Cluster specification for more information.

The TYPE option

You can use the TYPE option to ask SCAN to only return objects that match a given type, allowing you to iterate through the database looking for keys of a specific type. The TYPE option is only available on the whole-database SCAN, not HSCAN or ZSCAN etc.

The type argument is the same string name that the TYPE command returns. Note a quirk where some Redis types, such as GeoHashes, HyperLogLogs, Bitmaps, and Bitfields, may internally be implemented using other Redis types, such as a string or zset, so can't be distinguished from other keys of that same type by SCAN. For example, a ZSET and GEOHASH:

> GEOADD geokey 0 0 value
(integer) 1
> ZADD zkey 1000 value
(integer) 1
> TYPE geokey
zset
> TYPE zkey
zset
> SCAN 0 TYPE zset
1) "0"
2) 1) "geokey"
 2) "zkey"
Are you tired of using redis-cli? Try Redis Insight - the developer GUI for Redis.
import redis
r = redis.Redis(decode_responses=True)
res = r.set("key1", "Hello")
print(res)
# >>> True
res = r.set("key2", "World")
print(res)
# >>> True
res = r.delete("key1", "key2", "key3")
print(res)
# >>> 2
res = r.set("key1", "Hello")
print(res)
# >>> True
res = r.exists("key1")
print(res)
# >>> 1
res = r.exists("nosuchkey")
print(res)
# >>> 0
res = r.set("key2", "World")
print(res)
# >>> True
res = r.exists("key1", "key2", "nosuchkey")
print(res)
# >>> 2
res = r.set("mykey", "Hello")
print(res)
# >>> True
res = r.expire("mykey", 10)
print(res)
# >>> True
res = r.ttl("mykey")
print(res)
# >>> 10
res = r.set("mykey", "Hello World")
print(res)
# >>> True
res = r.ttl("mykey")
print(res)
# >>> -1
res = r.expire("mykey", 10, xx=True)
print(res)
# >>> False
res = r.ttl("mykey")
print(res)
# >>> -1
res = r.expire("mykey", 10, nx=True)
print(res)
# >>> True
res = r.ttl("mykey")
print(res)
# >>> 10
res = r.set("mykey", "Hello")
print(res)
# >>> True
res = r.expire("mykey", 10)
print(res)
# >>> True
res = r.ttl("mykey")
print(res)
# >>> 10
res = r.sadd("myset", *set([1, 2, 3, "foo", "foobar", "feelsgood"]))
print(res)
# >>> 6
res = list(r.sscan_iter("myset", match="f*"))
print(res)
# >>> ['foobar', 'foo', 'feelsgood']
cursor, key = r.scan(cursor=0, match='*11*')
print(cursor, key)
cursor, key = r.scan(cursor, match='*11*')
print(cursor, key)
cursor, key = r.scan(cursor, match='*11*')
print(cursor, key)
cursor, key = r.scan(cursor, match='*11*')
print(cursor, key)
cursor, keys = r.scan(cursor, match='*11*', count=1000)
print(cursor, keys)
res = r.geoadd("geokey", (0, 0, "value"))
print(res)
# >>> 1
res = r.zadd("zkey", {"value": 1000})
print(res)
# >>> 1
res = r.type("geokey")
print(res)
# >>> zset
res = r.type("zkey")
print(res)
# >>> zset
cursor, keys = r.scan(cursor=0, _type="zset")
print(keys)
# >>> ['zkey', 'geokey']
res = r.hset("myhash", mapping={"a": 1, "b": 2})
print(res)
# >>> 2
cursor, keys = r.hscan("myhash", 0)
print(keys)
# >>> {'a': '1', 'b': '2'}
cursor, keys = r.hscan("myhash", 0, no_values=True)
print(keys)
# >>> ['a', 'b']
Python Quick-Start
import { createClient } from 'redis';
const client = createClient();
await client.connect().catch(console.error);
const delRes1 = await client.set('key1', 'Hello');
console.log(delRes1); // OK

const delRes2 = await client.set('key2', 'World');
console.log(delRes2); // OK

const delRes3 = await client.del(['key1', 'key2', 'key3']);
console.log(delRes3); // 2

const existsRes1 = await client.set('key1', 'Hello');
console.log(existsRes1); // OK

const existsRes2 = await client.exists('key1');
console.log(existsRes2); // 1

const existsRes3 = await client.exists('nosuchkey');
console.log(existsRes3); // 0

const existsRes4 = await client.set('key2', 'World');
console.log(existsRes4); // OK

const existsRes5 = await client.exists(['key1', 'key2', 'nosuchkey']);
console.log(existsRes5); // 2

const expireRes1 = await client.set('mykey', 'Hello');
console.log(expireRes1); // OK

const expireRes2 = await client.expire('mykey', 10);
console.log(expireRes2); // 1

const expireRes3 = await client.ttl('mykey');
console.log(expireRes3); // 10

const expireRes4 = await client.set('mykey', 'Hello World');
console.log(expireRes4); // OK

const expireRes5 = await client.ttl('mykey');
console.log(expireRes5); // -1

const expireRes6 = await client.expire('mykey', 10, "XX");
console.log(expireRes6); // 0

const expireRes7 = await client.ttl('mykey');
console.log(expireRes7); // -1

const expireRes8 = await client.expire('mykey', 10, "NX");
console.log(expireRes8); // 1

const expireRes9 = await client.ttl('mykey');
console.log(expireRes9); // 10

const ttlRes1 = await client.set('mykey', 'Hello');
console.log(ttlRes1); // OK

const ttlRes2 = await client.expire('mykey', 10);
console.log(ttlRes2); // 1

const ttlRes3 = await client.ttl('mykey');
console.log(ttlRes3); // 10

const scan1Res1 = await client.sAdd('myset', ['1', '2', '3', 'foo', 'foobar', 'feelsgood']);
console.log(scan1Res1); // 6

let scan1Res2 = [];
for await (const values of client.sScanIterator('myset', { MATCH: 'f*' })) {
 scan1Res2 = scan1Res2.concat(values);
}
console.log(scan1Res2); // ['foo', 'foobar', 'feelsgood']

let cursor = '0';
let scanResult;
scanResult = await client.scan(cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*', COUNT: 1000 });
console.log(scanResult.cursor, scanResult.keys);
const scan3Res1 = await client.geoAdd('geokey', { longitude: 0, latitude: 0, member: 'value' });
console.log(scan3Res1); // 1

const scan3Res2 = await client.zAdd('zkey', [{ score: 1000, value: 'value' }]);
console.log(scan3Res2); // 1

const scan3Res3 = await client.type('geokey');
console.log(scan3Res3); // zset

const scan3Res4 = await client.type('zkey');
console.log(scan3Res4); // zset

const scan3Res5 = await client.scan('0', { TYPE: 'zset' });
console.log(scan3Res5.keys); // ['zkey', 'geokey']

const scan4Res1 = await client.hSet('myhash', { a: 1, b: 2 });
console.log(scan4Res1); // 2

const scan4Res2 = await client.hScan('myhash', '0');
console.log(scan4Res2.entries); // [{field: 'a', value: '1'}, {field: 'b', value: '2'}]

const scan4Res3 = await client.hScan('myhash', '0', { COUNT: 10 });
const items = scan4Res3.entries.map((item) => item.field)
console.log(items); // ['a', 'b']

await client.close();
Node.js Quick-Start
importredis.clients.jedis.UnifiedJedis;importredis.clients.jedis.args.ExpiryOption;import staticorg.junit.jupiter.api.Assertions.assertEquals;publicclass CmdsGenericExample{publicvoidrun(){UnifiedJedisjedis=newUnifiedJedis("redis://localhost:6379");StringdelResult1=jedis.set("key1","Hello");System.out.println(delResult1);// >>> OKStringdelResult2=jedis.set("key2","World");System.out.println(delResult2);// >>> OKlongdelResult3=jedis.del("key1","key2","key3");System.out.println(delResult3);// >>> 2// Tests for 'del' step.StringexistsResult1=jedis.set("key1","Hello");System.out.println(existsResult1);// >>> OKbooleanexistsResult2=jedis.exists("key1");System.out.println(existsResult2);// >>> truebooleanexistsResult3=jedis.exists("nosuchkey");System.out.println(existsResult3);// >>> falseStringexistsResult4=jedis.set("key2","World");System.out.println(existsResult4);// >>> OKlongexistsResult5=jedis.exists("key1","key2","nosuchkey");System.out.println(existsResult5);// >>> 2// Tests for 'exists' step.StringexpireResult1=jedis.set("mykey","Hello");System.out.println(expireResult1);// >>> OKlongexpireResult2=jedis.expire("mykey",10);System.out.println(expireResult2);// >>> 1longexpireResult3=jedis.ttl("mykey");System.out.println(expireResult3);// >>> 10StringexpireResult4=jedis.set("mykey","Hello World");System.out.println(expireResult4);// >>> OKlongexpireResult5=jedis.ttl("mykey");System.out.println(expireResult5);// >>> -1longexpireResult6=jedis.expire("mykey",10,ExpiryOption.XX);System.out.println(expireResult6);// >>> 0longexpireResult7=jedis.ttl("mykey");System.out.println(expireResult7);// >>> -1longexpireResult8=jedis.expire("mykey",10,ExpiryOption.NX);System.out.println(expireResult8);// >>> 1longexpireResult9=jedis.ttl("mykey");System.out.println(expireResult9);// >>> 10// Tests for 'expire' step.StringttlResult1=jedis.set("mykey","Hello");System.out.println(ttlResult1);// >>> OKlongttlResult2=jedis.expire("mykey",10);System.out.println(ttlResult2);// >>> 1longttlResult3=jedis.ttl("mykey");System.out.println(ttlResult3);// >>> 10// Tests for 'ttl' step.jedis.close();}}
Java-Sync Quick-Start
packageio.redis.examples.async;importio.lettuce.core.*;importio.lettuce.core.api.async.RedisAsyncCommands;importio.lettuce.core.api.StatefulRedisConnection;importjava.util.concurrent.CompletableFuture;publicclass CmdsGenericExample{publicvoidrun(){CompletableFuture<Void>existsExample=asyncCommands.set("key1","Hello").thenCompose(res1->{System.out.println(res1);// >>> OKreturnasyncCommands.exists("key1");}).thenCompose(res2->{System.out.println(res2);// >>> 1returnasyncCommands.exists("nosuchkey");}).thenCompose(res3->{System.out.println(res3);// >>> 0returnasyncCommands.set("key2","World");}).thenCompose(res4->{System.out.println(res4);// >>> OKreturnasyncCommands.exists("key1","key2","nosuchkey");}).thenAccept(res5->{System.out.println(res5);// >>> 2}).toCompletableFuture();existsExample.join();}finally{redisClient.shutdown();}}}
Java-Async Quick-Start
packageio.redis.examples.reactive;importio.lettuce.core.*;importio.lettuce.core.api.reactive.RedisReactiveCommands;importio.lettuce.core.api.StatefulRedisConnection;importreactor.core.publisher.Mono;publicclass CmdsGenericExample{publicvoidrun(){RedisClientredisClient=RedisClient.create("redis://localhost:6379");try(StatefulRedisConnection<String,String>connection=redisClient.connect()){RedisReactiveCommands<String,String>reactiveCommands=connection.reactive();Mono<Void>existsExample=reactiveCommands.set("key1","Hello").doOnNext(res1->{System.out.println(res1);// >>> OK}).then(reactiveCommands.exists("key1")).doOnNext(res2->{System.out.println(res2);// >>> 1}).then(reactiveCommands.exists("nosuchkey")).doOnNext(res3->{System.out.println(res3);// >>> 0}).then(reactiveCommands.set("key2","World")).doOnNext(res4->{System.out.println(res4);// >>> OK}).then(reactiveCommands.exists("key1","key2","nosuchkey")).doOnNext(res5->{System.out.println(res5);// >>> 2}).then();Mono.when(existsExample).block();}finally{redisClient.shutdown();}}}
Java-Reactive Quick-Start
package example_commands_test
import (
	"context"
	"fmt"
	"math"
	"time"
	"github.com/redis/go-redis/v9"
)
func ExampleClient_del_cmd() {
	ctx := context.Background()
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		Password: "", // no password docs
		DB: 0, // use default DB
	})
	delResult1, err := rdb.Set(ctx, "key1", "Hello", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(delResult1) // >>> OK
	delResult2, err := rdb.Set(ctx, "key2", "World", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(delResult2) // >>> OK
	delResult3, err := rdb.Del(ctx, "key1", "key2", "key3").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(delResult3) // >>> 2
}
func ExampleClient_exists_cmd() {
	ctx := context.Background()
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		Password: "", // no password docs
		DB: 0, // use default DB
	})
	existsResult1, err := rdb.Set(ctx, "key1", "Hello", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult1) // >>> OK
	existsResult2, err := rdb.Exists(ctx, "key1").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult2) // >>> 1
	existsResult3, err := rdb.Exists(ctx, "nosuchkey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult3) // >>> 0
	existsResult4, err := rdb.Set(ctx, "key2", "World", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult4) // >>> OK
	existsResult5, err := rdb.Exists(ctx, "key1", "key2", "nosuchkey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult5) // >>> 2
}
func ExampleClient_expire_cmd() {
	ctx := context.Background()
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		Password: "", // no password docs
		DB: 0, // use default DB
	})
	expireResult1, err := rdb.Set(ctx, "mykey", "Hello", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult1) // >>> OK
	expireResult2, err := rdb.Expire(ctx, "mykey", 10*time.Second).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult2) // >>> true
	expireResult3, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(math.Round(expireResult3.Seconds())) // >>> 10
	expireResult4, err := rdb.Set(ctx, "mykey", "Hello World", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult4) // >>> OK
	expireResult5, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult5) // >>> -1ns
	expireResult6, err := rdb.ExpireXX(ctx, "mykey", 10*time.Second).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult6) // >>> false
	expireResult7, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult7) // >>> -1ns
	expireResult8, err := rdb.ExpireNX(ctx, "mykey", 10*time.Second).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult8) // >>> true
	expireResult9, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(math.Round(expireResult9.Seconds())) // >>> 10
}
func ExampleClient_ttl_cmd() {
	ctx := context.Background()
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		Password: "", // no password docs
		DB: 0, // use default DB
	})
	ttlResult1, err := rdb.Set(ctx, "mykey", "Hello", 10*time.Second).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(ttlResult1) // >>> OK
	ttlResult2, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(math.Round(ttlResult2.Seconds())) // >>> 10
}
Go Quick-Start
using NRedisStack.Tests;
using StackExchange.Redis;
public class CmdsGenericExample
{
 public void Run()
 {
 var muxer = ConnectionMultiplexer.Connect("localhost:6379");
 var db = muxer.GetDatabase();
 // Tests for 'copy' step.
 bool delResult1 = db.StringSet("key1", "Hello");
 Console.WriteLine(delResult1); // >>> true
 bool delResult2 = db.StringSet("key2", "World");
 Console.WriteLine(delResult2); // >>> true
 long delResult3 = db.KeyDelete(["key1", "key2", "key3"]);
 Console.WriteLine(delResult3); // >>> 2
 // Tests for 'del' step.
 // Tests for 'dump' step.
 bool existsResult1 = db.StringSet("key1", "Hello");
 Console.WriteLine(existsResult1); // >>> true
 bool existsResult2 = db.KeyExists("key1");
 Console.WriteLine(existsResult2); // >>> true
 bool existsResult3 = db.KeyExists("nosuchkey");
 Console.WriteLine(existsResult3); // >>> false
 bool existsResult4 = db.StringSet("key2", "World");
 Console.WriteLine(existsResult4); // >>> true
 long existsResult5 = db.KeyExists(["key1", "key2", "nosuchkey"]);
 Console.WriteLine(existsResult5); // >>> 2
 // Tests for 'exists' step.
 bool expireResult1 = db.StringSet("mykey", "Hello");
 Console.WriteLine(expireResult1); // >>> true
 bool expireResult2 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10));
 Console.WriteLine(expireResult2); // >>> true
 TimeSpan expireResult3 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 Console.WriteLine(Math.Round(expireResult3.TotalSeconds)); // >>> 10
 bool expireResult4 = db.StringSet("mykey", "Hello World");
 Console.WriteLine(expireResult4); // >>> true
 TimeSpan expireResult5 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 Console.WriteLine(Math.Round(expireResult5.TotalSeconds).ToString()); // >>> 0
 bool expireResult6 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10), ExpireWhen.HasExpiry);
 Console.WriteLine(expireResult6); // >>> false
 TimeSpan expireResult7 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 Console.WriteLine(Math.Round(expireResult7.TotalSeconds)); // >>> 0
 bool expireResult8 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10), ExpireWhen.HasNoExpiry);
 Console.WriteLine(expireResult8); // >>> true
 TimeSpan expireResult9 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 Console.WriteLine(Math.Round(expireResult9.TotalSeconds)); // >>> 10
 // Tests for 'expire' step.
 // Tests for 'expireat' step.
 // Tests for 'expiretime' step.
 // Tests for 'keys' step.
 // Tests for 'migrate' step.
 // Tests for 'move' step.
 // Tests for 'object_encoding' step.
 // Tests for 'object_freq' step.
 // Tests for 'object_idletime' step.
 // Tests for 'object_refcount' step.
 // Tests for 'persist' step.
 // Tests for 'pexpire' step.
 // Tests for 'pexpireat' step.
 // Tests for 'pexpiretime' step.
 // Tests for 'pttl' step.
 // Tests for 'randomkey' step.
 // Tests for 'rename' step.
 // Tests for 'renamenx' step.
 // Tests for 'restore' step.
 // Tests for 'scan1' step.
 // Tests for 'scan2' step.
 // Tests for 'scan3' step.
 // Tests for 'scan4' step.
 // Tests for 'sort' step.
 // Tests for 'sort_ro' step.
 // Tests for 'touch' step.
 bool ttlResult1 = db.StringSet("mykey", "Hello");
 Console.WriteLine(ttlResult1); // >>> true
 bool ttlResult2 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10));
 Console.WriteLine(ttlResult2);
 TimeSpan ttlResult3 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 string ttlRes = Math.Round(ttlResult3.TotalSeconds).ToString();
 Console.WriteLine(Math.Round(ttlResult3.TotalSeconds)); // >>> 10
 // Tests for 'ttl' step.
 // Tests for 'type' step.
 // Tests for 'unlink' step.
 // Tests for 'wait' step.
 // Tests for 'waitaof' step.
 }
}
C#-Sync Quick-Start
<?php
require_once 'vendor/autoload.php';
use Predis\Client as PredisClient;
class CmdsGenericTest
{
 public function testCmdsGeneric() {
 $r = new PredisClient([
 'scheme' => 'tcp',
 'host' => '127.0.0.1',
 'port' => 6379,
 'password' => '',
 'database' => 0,
 ]);
 $existsResult1 = $r->set('key1', 'Hello');
 echo $existsResult1 . PHP_EOL; // >>> OK

 $existsResult2 = $r->exists('key1');
 echo $existsResult2 . PHP_EOL; // >>> 1

 $existsResult3 = $r->exists('nosuchkey');
 echo $existsResult3 . PHP_EOL; // >>> 0

 $existsResult4 = $r->set('key2', 'World');
 echo $existsResult4 . PHP_EOL; // >>> OK

 $existsResult5 = $r->exists('key1', 'key2', 'nosuchkey');
 echo $existsResult5 . PHP_EOL; // >>> 2

 }
}
PHP Quick-Start

It is important to note that the TYPE filter is also applied after elements are retrieved from the database, so the option does not reduce the amount of work the server has to do to complete a full iteration, and for rare types you may receive no elements in many iterations.

The NOVALUES option

When using HSCAN, you can use the NOVALUES option to make Redis return only the keys in the hash table without their corresponding values.

> HSET myhash a 1 b 2
OK
> HSCAN myhash 0
1) "0"
2) 1) "a"
 2) "1"
 3) "b"
 4) "2"
> HSCAN myhash 0 NOVALUES
1) "0"
2) 1) "a"
 2) "b"
Are you tired of using redis-cli? Try Redis Insight - the developer GUI for Redis.
import redis
r = redis.Redis(decode_responses=True)
res = r.set("key1", "Hello")
print(res)
# >>> True
res = r.set("key2", "World")
print(res)
# >>> True
res = r.delete("key1", "key2", "key3")
print(res)
# >>> 2
res = r.set("key1", "Hello")
print(res)
# >>> True
res = r.exists("key1")
print(res)
# >>> 1
res = r.exists("nosuchkey")
print(res)
# >>> 0
res = r.set("key2", "World")
print(res)
# >>> True
res = r.exists("key1", "key2", "nosuchkey")
print(res)
# >>> 2
res = r.set("mykey", "Hello")
print(res)
# >>> True
res = r.expire("mykey", 10)
print(res)
# >>> True
res = r.ttl("mykey")
print(res)
# >>> 10
res = r.set("mykey", "Hello World")
print(res)
# >>> True
res = r.ttl("mykey")
print(res)
# >>> -1
res = r.expire("mykey", 10, xx=True)
print(res)
# >>> False
res = r.ttl("mykey")
print(res)
# >>> -1
res = r.expire("mykey", 10, nx=True)
print(res)
# >>> True
res = r.ttl("mykey")
print(res)
# >>> 10
res = r.set("mykey", "Hello")
print(res)
# >>> True
res = r.expire("mykey", 10)
print(res)
# >>> True
res = r.ttl("mykey")
print(res)
# >>> 10
res = r.sadd("myset", *set([1, 2, 3, "foo", "foobar", "feelsgood"]))
print(res)
# >>> 6
res = list(r.sscan_iter("myset", match="f*"))
print(res)
# >>> ['foobar', 'foo', 'feelsgood']
cursor, key = r.scan(cursor=0, match='*11*')
print(cursor, key)
cursor, key = r.scan(cursor, match='*11*')
print(cursor, key)
cursor, key = r.scan(cursor, match='*11*')
print(cursor, key)
cursor, key = r.scan(cursor, match='*11*')
print(cursor, key)
cursor, keys = r.scan(cursor, match='*11*', count=1000)
print(cursor, keys)
res = r.geoadd("geokey", (0, 0, "value"))
print(res)
# >>> 1
res = r.zadd("zkey", {"value": 1000})
print(res)
# >>> 1
res = r.type("geokey")
print(res)
# >>> zset
res = r.type("zkey")
print(res)
# >>> zset
cursor, keys = r.scan(cursor=0, _type="zset")
print(keys)
# >>> ['zkey', 'geokey']
res = r.hset("myhash", mapping={"a": 1, "b": 2})
print(res)
# >>> 2
cursor, keys = r.hscan("myhash", 0)
print(keys)
# >>> {'a': '1', 'b': '2'}
cursor, keys = r.hscan("myhash", 0, no_values=True)
print(keys)
# >>> ['a', 'b']
Python Quick-Start
import { createClient } from 'redis';
const client = createClient();
await client.connect().catch(console.error);
const delRes1 = await client.set('key1', 'Hello');
console.log(delRes1); // OK

const delRes2 = await client.set('key2', 'World');
console.log(delRes2); // OK

const delRes3 = await client.del(['key1', 'key2', 'key3']);
console.log(delRes3); // 2

const existsRes1 = await client.set('key1', 'Hello');
console.log(existsRes1); // OK

const existsRes2 = await client.exists('key1');
console.log(existsRes2); // 1

const existsRes3 = await client.exists('nosuchkey');
console.log(existsRes3); // 0

const existsRes4 = await client.set('key2', 'World');
console.log(existsRes4); // OK

const existsRes5 = await client.exists(['key1', 'key2', 'nosuchkey']);
console.log(existsRes5); // 2

const expireRes1 = await client.set('mykey', 'Hello');
console.log(expireRes1); // OK

const expireRes2 = await client.expire('mykey', 10);
console.log(expireRes2); // 1

const expireRes3 = await client.ttl('mykey');
console.log(expireRes3); // 10

const expireRes4 = await client.set('mykey', 'Hello World');
console.log(expireRes4); // OK

const expireRes5 = await client.ttl('mykey');
console.log(expireRes5); // -1

const expireRes6 = await client.expire('mykey', 10, "XX");
console.log(expireRes6); // 0

const expireRes7 = await client.ttl('mykey');
console.log(expireRes7); // -1

const expireRes8 = await client.expire('mykey', 10, "NX");
console.log(expireRes8); // 1

const expireRes9 = await client.ttl('mykey');
console.log(expireRes9); // 10

const ttlRes1 = await client.set('mykey', 'Hello');
console.log(ttlRes1); // OK

const ttlRes2 = await client.expire('mykey', 10);
console.log(ttlRes2); // 1

const ttlRes3 = await client.ttl('mykey');
console.log(ttlRes3); // 10

const scan1Res1 = await client.sAdd('myset', ['1', '2', '3', 'foo', 'foobar', 'feelsgood']);
console.log(scan1Res1); // 6

let scan1Res2 = [];
for await (const values of client.sScanIterator('myset', { MATCH: 'f*' })) {
 scan1Res2 = scan1Res2.concat(values);
}
console.log(scan1Res2); // ['foo', 'foobar', 'feelsgood']

let cursor = '0';
let scanResult;
scanResult = await client.scan(cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*', COUNT: 1000 });
console.log(scanResult.cursor, scanResult.keys);
const scan3Res1 = await client.geoAdd('geokey', { longitude: 0, latitude: 0, member: 'value' });
console.log(scan3Res1); // 1

const scan3Res2 = await client.zAdd('zkey', [{ score: 1000, value: 'value' }]);
console.log(scan3Res2); // 1

const scan3Res3 = await client.type('geokey');
console.log(scan3Res3); // zset

const scan3Res4 = await client.type('zkey');
console.log(scan3Res4); // zset

const scan3Res5 = await client.scan('0', { TYPE: 'zset' });
console.log(scan3Res5.keys); // ['zkey', 'geokey']

const scan4Res1 = await client.hSet('myhash', { a: 1, b: 2 });
console.log(scan4Res1); // 2

const scan4Res2 = await client.hScan('myhash', '0');
console.log(scan4Res2.entries); // [{field: 'a', value: '1'}, {field: 'b', value: '2'}]

const scan4Res3 = await client.hScan('myhash', '0', { COUNT: 10 });
const items = scan4Res3.entries.map((item) => item.field)
console.log(items); // ['a', 'b']

await client.close();
Node.js Quick-Start
importredis.clients.jedis.UnifiedJedis;importredis.clients.jedis.args.ExpiryOption;import staticorg.junit.jupiter.api.Assertions.assertEquals;publicclass CmdsGenericExample{publicvoidrun(){UnifiedJedisjedis=newUnifiedJedis("redis://localhost:6379");StringdelResult1=jedis.set("key1","Hello");System.out.println(delResult1);// >>> OKStringdelResult2=jedis.set("key2","World");System.out.println(delResult2);// >>> OKlongdelResult3=jedis.del("key1","key2","key3");System.out.println(delResult3);// >>> 2// Tests for 'del' step.StringexistsResult1=jedis.set("key1","Hello");System.out.println(existsResult1);// >>> OKbooleanexistsResult2=jedis.exists("key1");System.out.println(existsResult2);// >>> truebooleanexistsResult3=jedis.exists("nosuchkey");System.out.println(existsResult3);// >>> falseStringexistsResult4=jedis.set("key2","World");System.out.println(existsResult4);// >>> OKlongexistsResult5=jedis.exists("key1","key2","nosuchkey");System.out.println(existsResult5);// >>> 2// Tests for 'exists' step.StringexpireResult1=jedis.set("mykey","Hello");System.out.println(expireResult1);// >>> OKlongexpireResult2=jedis.expire("mykey",10);System.out.println(expireResult2);// >>> 1longexpireResult3=jedis.ttl("mykey");System.out.println(expireResult3);// >>> 10StringexpireResult4=jedis.set("mykey","Hello World");System.out.println(expireResult4);// >>> OKlongexpireResult5=jedis.ttl("mykey");System.out.println(expireResult5);// >>> -1longexpireResult6=jedis.expire("mykey",10,ExpiryOption.XX);System.out.println(expireResult6);// >>> 0longexpireResult7=jedis.ttl("mykey");System.out.println(expireResult7);// >>> -1longexpireResult8=jedis.expire("mykey",10,ExpiryOption.NX);System.out.println(expireResult8);// >>> 1longexpireResult9=jedis.ttl("mykey");System.out.println(expireResult9);// >>> 10// Tests for 'expire' step.StringttlResult1=jedis.set("mykey","Hello");System.out.println(ttlResult1);// >>> OKlongttlResult2=jedis.expire("mykey",10);System.out.println(ttlResult2);// >>> 1longttlResult3=jedis.ttl("mykey");System.out.println(ttlResult3);// >>> 10// Tests for 'ttl' step.jedis.close();}}
Java-Sync Quick-Start
packageio.redis.examples.async;importio.lettuce.core.*;importio.lettuce.core.api.async.RedisAsyncCommands;importio.lettuce.core.api.StatefulRedisConnection;importjava.util.concurrent.CompletableFuture;publicclass CmdsGenericExample{publicvoidrun(){CompletableFuture<Void>existsExample=asyncCommands.set("key1","Hello").thenCompose(res1->{System.out.println(res1);// >>> OKreturnasyncCommands.exists("key1");}).thenCompose(res2->{System.out.println(res2);// >>> 1returnasyncCommands.exists("nosuchkey");}).thenCompose(res3->{System.out.println(res3);// >>> 0returnasyncCommands.set("key2","World");}).thenCompose(res4->{System.out.println(res4);// >>> OKreturnasyncCommands.exists("key1","key2","nosuchkey");}).thenAccept(res5->{System.out.println(res5);// >>> 2}).toCompletableFuture();existsExample.join();}finally{redisClient.shutdown();}}}
Java-Async Quick-Start
packageio.redis.examples.reactive;importio.lettuce.core.*;importio.lettuce.core.api.reactive.RedisReactiveCommands;importio.lettuce.core.api.StatefulRedisConnection;importreactor.core.publisher.Mono;publicclass CmdsGenericExample{publicvoidrun(){RedisClientredisClient=RedisClient.create("redis://localhost:6379");try(StatefulRedisConnection<String,String>connection=redisClient.connect()){RedisReactiveCommands<String,String>reactiveCommands=connection.reactive();Mono<Void>existsExample=reactiveCommands.set("key1","Hello").doOnNext(res1->{System.out.println(res1);// >>> OK}).then(reactiveCommands.exists("key1")).doOnNext(res2->{System.out.println(res2);// >>> 1}).then(reactiveCommands.exists("nosuchkey")).doOnNext(res3->{System.out.println(res3);// >>> 0}).then(reactiveCommands.set("key2","World")).doOnNext(res4->{System.out.println(res4);// >>> OK}).then(reactiveCommands.exists("key1","key2","nosuchkey")).doOnNext(res5->{System.out.println(res5);// >>> 2}).then();Mono.when(existsExample).block();}finally{redisClient.shutdown();}}}
Java-Reactive Quick-Start
package example_commands_test
import (
	"context"
	"fmt"
	"math"
	"time"
	"github.com/redis/go-redis/v9"
)
func ExampleClient_del_cmd() {
	ctx := context.Background()
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		Password: "", // no password docs
		DB: 0, // use default DB
	})
	delResult1, err := rdb.Set(ctx, "key1", "Hello", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(delResult1) // >>> OK
	delResult2, err := rdb.Set(ctx, "key2", "World", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(delResult2) // >>> OK
	delResult3, err := rdb.Del(ctx, "key1", "key2", "key3").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(delResult3) // >>> 2
}
func ExampleClient_exists_cmd() {
	ctx := context.Background()
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		Password: "", // no password docs
		DB: 0, // use default DB
	})
	existsResult1, err := rdb.Set(ctx, "key1", "Hello", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult1) // >>> OK
	existsResult2, err := rdb.Exists(ctx, "key1").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult2) // >>> 1
	existsResult3, err := rdb.Exists(ctx, "nosuchkey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult3) // >>> 0
	existsResult4, err := rdb.Set(ctx, "key2", "World", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult4) // >>> OK
	existsResult5, err := rdb.Exists(ctx, "key1", "key2", "nosuchkey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(existsResult5) // >>> 2
}
func ExampleClient_expire_cmd() {
	ctx := context.Background()
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		Password: "", // no password docs
		DB: 0, // use default DB
	})
	expireResult1, err := rdb.Set(ctx, "mykey", "Hello", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult1) // >>> OK
	expireResult2, err := rdb.Expire(ctx, "mykey", 10*time.Second).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult2) // >>> true
	expireResult3, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(math.Round(expireResult3.Seconds())) // >>> 10
	expireResult4, err := rdb.Set(ctx, "mykey", "Hello World", 0).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult4) // >>> OK
	expireResult5, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult5) // >>> -1ns
	expireResult6, err := rdb.ExpireXX(ctx, "mykey", 10*time.Second).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult6) // >>> false
	expireResult7, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult7) // >>> -1ns
	expireResult8, err := rdb.ExpireNX(ctx, "mykey", 10*time.Second).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(expireResult8) // >>> true
	expireResult9, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(math.Round(expireResult9.Seconds())) // >>> 10
}
func ExampleClient_ttl_cmd() {
	ctx := context.Background()
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		Password: "", // no password docs
		DB: 0, // use default DB
	})
	ttlResult1, err := rdb.Set(ctx, "mykey", "Hello", 10*time.Second).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(ttlResult1) // >>> OK
	ttlResult2, err := rdb.TTL(ctx, "mykey").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(math.Round(ttlResult2.Seconds())) // >>> 10
}
Go Quick-Start
using NRedisStack.Tests;
using StackExchange.Redis;
public class CmdsGenericExample
{
 public void Run()
 {
 var muxer = ConnectionMultiplexer.Connect("localhost:6379");
 var db = muxer.GetDatabase();
 // Tests for 'copy' step.
 bool delResult1 = db.StringSet("key1", "Hello");
 Console.WriteLine(delResult1); // >>> true
 bool delResult2 = db.StringSet("key2", "World");
 Console.WriteLine(delResult2); // >>> true
 long delResult3 = db.KeyDelete(["key1", "key2", "key3"]);
 Console.WriteLine(delResult3); // >>> 2
 // Tests for 'del' step.
 // Tests for 'dump' step.
 bool existsResult1 = db.StringSet("key1", "Hello");
 Console.WriteLine(existsResult1); // >>> true
 bool existsResult2 = db.KeyExists("key1");
 Console.WriteLine(existsResult2); // >>> true
 bool existsResult3 = db.KeyExists("nosuchkey");
 Console.WriteLine(existsResult3); // >>> false
 bool existsResult4 = db.StringSet("key2", "World");
 Console.WriteLine(existsResult4); // >>> true
 long existsResult5 = db.KeyExists(["key1", "key2", "nosuchkey"]);
 Console.WriteLine(existsResult5); // >>> 2
 // Tests for 'exists' step.
 bool expireResult1 = db.StringSet("mykey", "Hello");
 Console.WriteLine(expireResult1); // >>> true
 bool expireResult2 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10));
 Console.WriteLine(expireResult2); // >>> true
 TimeSpan expireResult3 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 Console.WriteLine(Math.Round(expireResult3.TotalSeconds)); // >>> 10
 bool expireResult4 = db.StringSet("mykey", "Hello World");
 Console.WriteLine(expireResult4); // >>> true
 TimeSpan expireResult5 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 Console.WriteLine(Math.Round(expireResult5.TotalSeconds).ToString()); // >>> 0
 bool expireResult6 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10), ExpireWhen.HasExpiry);
 Console.WriteLine(expireResult6); // >>> false
 TimeSpan expireResult7 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 Console.WriteLine(Math.Round(expireResult7.TotalSeconds)); // >>> 0
 bool expireResult8 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10), ExpireWhen.HasNoExpiry);
 Console.WriteLine(expireResult8); // >>> true
 TimeSpan expireResult9 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 Console.WriteLine(Math.Round(expireResult9.TotalSeconds)); // >>> 10
 // Tests for 'expire' step.
 // Tests for 'expireat' step.
 // Tests for 'expiretime' step.
 // Tests for 'keys' step.
 // Tests for 'migrate' step.
 // Tests for 'move' step.
 // Tests for 'object_encoding' step.
 // Tests for 'object_freq' step.
 // Tests for 'object_idletime' step.
 // Tests for 'object_refcount' step.
 // Tests for 'persist' step.
 // Tests for 'pexpire' step.
 // Tests for 'pexpireat' step.
 // Tests for 'pexpiretime' step.
 // Tests for 'pttl' step.
 // Tests for 'randomkey' step.
 // Tests for 'rename' step.
 // Tests for 'renamenx' step.
 // Tests for 'restore' step.
 // Tests for 'scan1' step.
 // Tests for 'scan2' step.
 // Tests for 'scan3' step.
 // Tests for 'scan4' step.
 // Tests for 'sort' step.
 // Tests for 'sort_ro' step.
 // Tests for 'touch' step.
 bool ttlResult1 = db.StringSet("mykey", "Hello");
 Console.WriteLine(ttlResult1); // >>> true
 bool ttlResult2 = db.KeyExpire("mykey", new TimeSpan(0, 0, 10));
 Console.WriteLine(ttlResult2);
 TimeSpan ttlResult3 = db.KeyTimeToLive("mykey") ?? TimeSpan.Zero;
 string ttlRes = Math.Round(ttlResult3.TotalSeconds).ToString();
 Console.WriteLine(Math.Round(ttlResult3.TotalSeconds)); // >>> 10
 // Tests for 'ttl' step.
 // Tests for 'type' step.
 // Tests for 'unlink' step.
 // Tests for 'wait' step.
 // Tests for 'waitaof' step.
 }
}
C#-Sync Quick-Start
<?php
require_once 'vendor/autoload.php';
use Predis\Client as PredisClient;
class CmdsGenericTest
{
 public function testCmdsGeneric() {
 $r = new PredisClient([
 'scheme' => 'tcp',
 'host' => '127.0.0.1',
 'port' => 6379,
 'password' => '',
 'database' => 0,
 ]);
 $existsResult1 = $r->set('key1', 'Hello');
 echo $existsResult1 . PHP_EOL; // >>> OK

 $existsResult2 = $r->exists('key1');
 echo $existsResult2 . PHP_EOL; // >>> 1

 $existsResult3 = $r->exists('nosuchkey');
 echo $existsResult3 . PHP_EOL; // >>> 0

 $existsResult4 = $r->set('key2', 'World');
 echo $existsResult4 . PHP_EOL; // >>> OK

 $existsResult5 = $r->exists('key1', 'key2', 'nosuchkey');
 echo $existsResult5 . PHP_EOL; // >>> 2

 }
}
PHP Quick-Start

Multiple parallel iterations

It is possible for an infinite number of clients to iterate the same collection at the same time, as the full state of the iterator is in the cursor, that is obtained and returned to the client at every call. No server side state is taken at all.

Terminating iterations in the middle

Since there is no state server side, but the full state is captured by the cursor, the caller is free to terminate an iteration half-way without signaling this to the server in any way. An infinite number of iterations can be started and never terminated without any issue.

Calling SCAN with a corrupted cursor

Calling SCAN with a broken, negative, out of range, or otherwise invalid cursor, will result in undefined behavior but never in a crash. What will be undefined is that the guarantees about the returned elements can no longer be ensured by the SCAN implementation.

The only valid cursors to use are:

Guarantee of termination

The SCAN algorithm is guaranteed to terminate only if the size of the iterated collection remains bounded to a given maximum size, otherwise iterating a collection that always grows may result into SCAN to never terminate a full iteration.

This is easy to see intuitively: if the collection grows there is more and more work to do in order to visit all the possible elements, and the ability to terminate the iteration depends on the number of calls to SCAN and its COUNT option value compared with the rate at which the collection grows.

Why SCAN may return all the items of an aggregate data type in a single call?

In the COUNT option documentation, we state that sometimes this family of commands may return all the elements of a Set, Hash or Sorted Set at once in a single call, regardless of the COUNT option value. The reason why this happens is that the cursor-based iterator can be implemented, and is useful, only when the aggregate data type that we are scanning is represented as a hash table. However Redis uses a memory optimization where small aggregate data types, until they reach a given amount of items or a given max size of single elements, are represented using a compact single-allocation packed encoding. When this is the case, SCAN has no meaningful cursor to return, and must iterate the whole data structure at once, so the only sane behavior it has is to return everything in a call.

However once the data structures are bigger and are promoted to use real hash tables, the SCAN family of commands will resort to the normal behavior. Note that since this special behavior of returning all the elements is true only for small aggregates, it has no effects on the command complexity or latency. However the exact limits to get converted into real hash tables are user configurable, so the maximum number of elements you can see returned in a single call depends on how big an aggregate data type could be and still use the packed representation.

Also note that this behavior is specific of SSCAN, HSCAN and ZSCAN. SCAN itself never shows this behavior because the key space is always represented by hash tables.

Further reading

For more information about managing keys, please refer to the The Redis Keyspace tutorial.

Additional examples

Give the following commands, showing iteration of a hash key, a try in the interactive console:

HMSET hash name Jack age 33 HSCAN hash 0

Return information

Array reply: specifically, an array with two elements.

  • The first element is a Bulk string reply that represents an unsigned 64-bit number, the cursor.
  • The second element is an Array reply with the names of scanned keys.

History

RATE THIS PAGE
Back to top ↑

AltStyle によって変換されたページ (->オリジナル) /