redis - Redis client

Requires: hiredis (auto-installed via minks install kse_redis)

#include "kse_redis"

Query results from HGetAll, SMembers, and Keys are returned as JSON arrays.


Method Args Out Description
Redis:Connect host, port Number Connect; returns handle. Throws RedisException on failure
Redis:ConnectAuth host, port, password Number Connect with AUTH
Redis:Close handle void Close the connection
Redis:Error handle String Last error message, "" if none
Redis:Set handle, key, value Boolean SET
Redis:SetEx handle, key, value, ttl Boolean SETEX with TTL in seconds
Redis:Get handle, key String GET; "" if key missing
Redis:Del handle, key Boolean DEL
Redis:Exists handle, key Boolean EXISTS
Redis:Expire handle, key, ttl Boolean EXPIRE
Redis:TTL handle, key Number TTL in seconds; -1 = no expiry, -2 = missing
Redis:Incr handle, key Number INCR
Redis:IncrBy handle, key, delta Number INCRBY
Redis:HSet handle, hash, field, value Boolean HSET
Redis:HGet handle, hash, field String HGET
Redis:HGetAll handle, hash String HGETALL → JSON object
Redis:HDel handle, hash, field Boolean HDEL
Redis:LPush handle, key, value Number LPUSH → new list length
Redis:RPush handle, key, value Number RPUSH → new list length
Redis:LPop handle, key String LPOP
Redis:RPop handle, key String RPOP
Redis:LLen handle, key Number LLEN
Redis:SAdd handle, key, member Boolean SADD
Redis:SRem handle, key, member Boolean SREM
Redis:SIsMember handle, key, member Boolean SISMEMBER
Redis:SMembers handle, key String SMEMBERS → JSON array
Redis:Keys handle, pattern String KEYS → JSON array
Redis:Exec handle, command String Raw command string → reply as string

Throws RedisException on connection failure. See Object - Exception.

Var:Number r;
Var:Boolean ok;
Var:String val;

try {
    Redis:Connect("127.0.0.1", 6379, r);
} catch (RedisException e) {
    Konsol:Print("connect failed: ${e.message}");
}

Redis:Set(r, "greeting", "hello", ok);
Redis:Get(r, "greeting", val);
Konsol:Print(val);   // hello

Redis:HSet(r, "user:1", "name", "Alice", ok);
Var:String fields;
Redis:HGetAll(r, "user:1", fields);
Konsol:Print(fields);   // {"name":"Alice"}

Redis:Close(r);

Examples

// test_redis.ks - Redis plugin tests
// Requires a Redis server running on localhost:6379
//
// After installing:
//   minks install kse_redis   (run once from inside redis_plugin/)
//   minks test_redis.ks
//
// Without installing:
//   minks --plugin-path . test_redis.ks

#include "kse_redis"

Konsol:Print("=== Redis Plugin Test ===");
Konsol:Print("");

Var:Number r;
try {
    Redis:Connect("127.0.0.1", 6379, r);
} catch (RedisException e) {
    Konsol:Print("Connect failed: ${e.message}");
    Konsol:Exit(1);
}
Konsol:Print("Connected, handle: ${r}");
Konsol:Print("");

Var:Boolean ok;
Var:String  val;
Var:Number  num;

// -- String -------------------------------------------------------------------

Konsol:Print("--- String");
Redis:Set(r, "ks:greeting", "hello minks", ok);
Konsol:Print("Set: ${ok}");

Redis:Get(r, "ks:greeting", val);
Konsol:Print("Get: ${val}");

Redis:Exists(r, "ks:greeting", ok);
Konsol:Print("Exists: ${ok}");

Redis:Del(r, "ks:greeting", ok);
Konsol:Print("Del: ${ok}");

Redis:Exists(r, "ks:greeting", ok);
Konsol:Print("Exists after Del: ${ok}");
Konsol:Print("");

// -- SetEx / TTL / Expire -----------------------------------------------------

Konsol:Print("--- SetEx / TTL");
Redis:SetEx(r, "ks:temp", "expires soon", 60, ok);
Konsol:Print("SetEx: ${ok}");

Redis:TTL(r, "ks:temp", num);
Konsol:Print("TTL: ${num}s");

Redis:Expire(r, "ks:temp", 120, ok);
Redis:TTL(r, "ks:temp", num);
Konsol:Print("TTL after Expire(120): ${num}s");

Redis:Del(r, "ks:temp", ok);
Konsol:Print("");

// -- Incr / IncrBy ------------------------------------------------------------

Konsol:Print("--- Incr / IncrBy");
Redis:Del(r, "ks:counter", ok);

Redis:Incr(r, "ks:counter", num);
Konsol:Print("Incr: ${num}");

Redis:IncrBy(r, "ks:counter", 9, num);
Konsol:Print("IncrBy 9: ${num}");

Redis:Del(r, "ks:counter", ok);
Konsol:Print("");

// -- Hash ---------------------------------------------------------------------

Konsol:Print("--- Hash");
Redis:HSet(r, "ks:user:1", "name",  "Alice",  ok);
Redis:HSet(r, "ks:user:1", "email", "alice@example.com", ok);
Redis:HSet(r, "ks:user:1", "score", "42", ok);

Redis:HGet(r, "ks:user:1", "name", val);
Konsol:Print("HGet name: ${val}");

Var:String fields;
Redis:HGetAll(r, "ks:user:1", fields);
Konsol:Print("HGetAll: ${fields}");

Redis:HDel(r, "ks:user:1", "score", ok);
Konsol:Print("HDel score: ${ok}");

Redis:HGetAll(r, "ks:user:1", fields);
Konsol:Print("HGetAll after HDel: ${fields}");

Redis:Del(r, "ks:user:1", ok);
Konsol:Print("");

// -- List ---------------------------------------------------------------------

Konsol:Print("--- List");
Redis:Del(r, "ks:queue", ok);

Redis:RPush(r, "ks:queue", "first",  num);
Redis:RPush(r, "ks:queue", "second", num);
Redis:LPush(r, "ks:queue", "zeroth", num);
Konsol:Print("LLen: ${num}");

Redis:LPop(r, "ks:queue", val);
Konsol:Print("LPop: ${val}");

Redis:RPop(r, "ks:queue", val);
Konsol:Print("RPop: ${val}");

Redis:LLen(r, "ks:queue", num);
Konsol:Print("LLen after pops: ${num}");

Redis:Del(r, "ks:queue", ok);
Konsol:Print("");

// -- Set ----------------------------------------------------------------------

Konsol:Print("--- Set");
Redis:Del(r, "ks:tags", ok);

Redis:SAdd(r, "ks:tags", "alpha",   ok);
Redis:SAdd(r, "ks:tags", "beta",    ok);
Redis:SAdd(r, "ks:tags", "gamma",   ok);

Redis:SIsMember(r, "ks:tags", "beta", ok);
Konsol:Print("SIsMember beta: ${ok}");

Var:String members;
Redis:SMembers(r, "ks:tags", members);
Konsol:Print("SMembers: ${members}");

Redis:SRem(r, "ks:tags", "beta", ok);
Konsol:Print("SRem beta: ${ok}");

Redis:SIsMember(r, "ks:tags", "beta", ok);
Konsol:Print("SIsMember beta after SRem: ${ok}");

Redis:Del(r, "ks:tags", ok);
Konsol:Print("");

// -- Keys pattern -------------------------------------------------------------

Konsol:Print("--- Keys");
Redis:Set(r, "ks:a", "1", ok);
Redis:Set(r, "ks:b", "2", ok);
Redis:Set(r, "ks:c", "3", ok);

Var:String keys;
Redis:Keys(r, "ks:*", keys);
Konsol:Print("Keys ks:*: ${keys}");

Redis:Del(r, "ks:a", ok);
Redis:Del(r, "ks:b", ok);
Redis:Del(r, "ks:c", ok);
Konsol:Print("");

// -- Raw Exec -----------------------------------------------------------------

Konsol:Print("--- Raw Exec");
Var:String reply;
Redis:Exec(r, "PING", reply);
Konsol:Print("PING: ${reply}");

Redis:Exec(r, "DBSIZE", reply);
Konsol:Print("DBSIZE: ${reply}");
Konsol:Print("");

// -- Done ---------------------------------------------------------------------

Redis:Close(r);
Konsol:Print("=== done ===");


Building from source

Platform Command
Debian/Ubuntu sudo apt install libhiredis-dev
Fedora/RHEL sudo dnf install hiredis-devel
macOS brew install hiredis
MSYS2 MINGW64 pacman -S mingw-w64-x86_64-redis-plus-plus
MSYS2 UCRT64 pacman -S mingw-w64-ucrt-x86_64-redis-plus-plus
MSYS2 CLANG64 pacman -S mingw-w64-clang-x86_64-redis-plus-plus
MSYS2 CLANGARM64 pacman -S mingw-w64-clang-aarch64-redis-plus-plus

The Makefile auto-detects via pkg-config. For native Windows, build hiredis from source or install via vcpkg and set HIREDIS_DIR:

make -C redis_plugin HIREDIS_DIR="C:/path/to/hiredis"