Skip to main content

Strings API

Purely functional interface for the Strings API.

Usage

import cats.effect.*
import dev.profunktor.valkey4cats.Valkey
import dev.profunktor.valkey4cats.effect.Log
import dev.profunktor.valkey4cats.model.ValkeyResponse.{Ok, Err}

given Log[IO] = Log.Stdout.instance[IO]

Valkey[IO].utf8("valkey://localhost:6379").use { valkey =>
for
// SET and GET
_ <- valkey.set("username", "gvolpe")
x <- valkey.get("username")
_ <- x match
case Ok(Some(v)) => IO.println(s"Got: $v")
case Ok(None) => IO.println("Not found")
case Err(e) => IO.println(s"Error: ${e.message}")

// SET with NX (only if not exists)
nx <- valkey.setNx("username", "should not happen")
_ <- IO.println(s"SetNx result: ${nx.toOption}") // Ok(false)

// INCR / DECR
_ <- valkey.set("counter", "0")
c1 <- valkey.incr("counter")
_ <- IO.println(s"After INCR: ${c1.toOption}") // Some(1)
c2 <- valkey.incrBy("counter", 10)
_ <- IO.println(s"After INCRBY 10: ${c2.toOption}") // Some(11)
c3 <- valkey.decrBy("counter", 5)
_ <- IO.println(s"After DECRBY 5: ${c3.toOption}") // Some(6)

// MSET / MGET
_ <- valkey.mSet(Map("a" -> "1", "b" -> "2", "c" -> "3"))
values <- valkey.mGet(Set("a", "b", "c"))
_ <- IO.println(s"MGET: ${values.toOption}")

// APPEND and STRLEN
_ <- valkey.set("greeting", "Hello")
len <- valkey.append("greeting", ", World!")
_ <- IO.println(s"After APPEND, length: ${len.toOption}") // Some(13)

// GETDEL
deleted <- valkey.getDel("greeting")
_ <- IO.println(s"GETDEL: ${deleted.toOption}") // Some(Some("Hello, World!"))
yield ()
}

Available commands

CommandMethodReturn type
GETget(key)F[ValkeyResponse[Option[V]]]
SETset(key, value)F[ValkeyResponse[Unit]]
SET (with options)set(key, value, options)F[ValkeyResponse[SetResult[V]]]
SETNXsetNx(key, value)F[ValkeyResponse[Boolean]]
MGETmGet(keys)F[ValkeyResponse[Map[K, V]]]
MSETmSet(keyValues)F[ValkeyResponse[Unit]]
MSETNXmSetNx(keyValues)F[ValkeyResponse[Boolean]]
INCRincr(key)F[ValkeyResponse[Long]]
INCRBYincrBy(key, amount)F[ValkeyResponse[Long]]
INCRBYFLOATincrByFloat(key, amount)F[ValkeyResponse[Double]]
DECRdecr(key)F[ValkeyResponse[Long]]
DECRBYdecrBy(key, amount)F[ValkeyResponse[Long]]
APPENDappend(key, value)F[ValkeyResponse[Long]]
STRLENstrlen(key)F[ValkeyResponse[Long]]
GETRANGEgetRange(key, start, end)F[ValkeyResponse[V]]
SETRANGEsetRange(key, offset, value)F[ValkeyResponse[Long]]
GETEXgetEx(key, expiry)F[ValkeyResponse[Option[V]]]
GETDELgetDel(key)F[ValkeyResponse[Option[V]]]
LCSlcs(key1, key2)F[ValkeyResponse[V]]
LCS LENlcsLen(key1, key2)F[ValkeyResponse[Long]]