ioredis

Redis client for dart.

Features

  • Support Pub/Sub
  • Support pool connection by default
  • Support auto reconnect on failed
  • Support retry strategy
  • Support selectable database

Basic Usage

/// Create a new redis instance
Redis redis = new Redis();
Redis redis = new Redis(RedisOptions(host: '127.0.0.1', port: 6379));

/// Set value
await redis.set('key', value);

/// Set value with expiry time
await redis.set('key', value, 'EX', 10);

/// Get value
String? value = await redis.get('key');

/// Get multiple values
List<String?> value = await redis.mget(['key1', 'key2']);

Create an instance

Redis redis = new Redis();
Redis redis = new Redis(RedisOptions(host: '127.0.0.1', port: 6379));
Redis redis = new Redis(
    RedisOptions(
        username: 'root', 
        password: 'password',
        db: 1,
    ),
);

Duplicate

Redis redis = new Redis();
Redis duplicatedRedis = redis.duplicate();

Pub/Sub

Redis subClient = new Redis();
Redis pubClient = subClient.duplicate();

RedisSubscriber subscriber = await subClient.subscribe('chat')
subscriber.onMessage = (String channel, String? message) {
    print(channel, message);
}

await pubClient.publish('chat', 'hello');

Delete

Redis redis = new Redis();
await redis.delete('key')
await redis.mdelete(['key1', 'key2'])

Flushdb

Redis redis = new Redis();
await redis.flushdb()

Send command

Redis redis = new Redis();
await redis.sendCommand(['GET', 'key'])

Pool connection

Redis redis = new Redis(RedisOptions(maxConnection: 10));
String? value = await redis.get('key');

Pipelining

Redis redis = new Redis();
List<dynamic> result = await redis.multi()
    .set('key', 'value')
    .set('key2', 'value2')
    .get('key')
    .get('key2')
    .exec()

// result => ['OK', 'OK', 'value', 'value2']

Libraries

ioredis