A kotlin redis client based on non-blocking I/O with coroutines. This is just some experimental code I wrote when learning about coroutines and channels, not very complete or well tested in any production system.
Create a client instance:
val redis = RedisClient(InetSocketAddress("localhost", 6379))
The instance manages its own connection pool, auto-scales upto default 50 connections. It uses redis command pipelining to possibly send multiple commands on the same connection before a reply has been received.
Set a key, (a suspending function). Encodes strings as UTF-8.
redis.set("somekey", "somevalue")
Get a key, decodes data as UTF-8
val stringValue = redis.get("somekey")
add to your build.gradle
repositories {
...
maven { url 'https://jitpack.io' }
}
and in your dependencies section:
dependencies {
implementation 'com.github.rrva:coredis:0.1.1'
}
get
get a key, decoded as a utf8 stringset
set a keysetex
set a key with expire time in secondsdel(key)
: delete a keyping(echomsg)
- ping the redis server, accepts optional text to echo backttl(key)
- show the ttl of a key
The RedisClient class supports the following constructor parameters:
address
Redis server addresscommandTimeoutMillis
timeout for sending commandsreplyTimeoutMillis
timeout for waiting to read a replydbIndex
select another redis database for all connections other than 0maxPoolSize
Maximum number of connections to open
- Needs more test coverage. This is alpha quality, has not been used in production yet.
- Redesign the error handling and possibly add more timeouts
- Better reconnect and pooling logic
- Support setting and getting byte arrays in addition to utf-8 strings
- Implement many more redis commands