Skip to main content

Lists API

Purely functional interface for the Lists API.

Lists are simple collections of string elements sorted by insertion order.

Usage

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

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

Valkey[IO].utf8("valkey://localhost:6379").use { valkey =>
for
// RPUSH / LPUSH
_ <- valkey.rpush("mylist", "one", "two", "three")
_ <- valkey.lpush("mylist", "zero")

// LRANGE - get a range of elements
range <- valkey.lrange("mylist", 0, -1)
_ <- IO.println(s"Range: ${range.toOption}")
// Some(List("zero", "one", "two", "three"))

// LPOP / RPOP
left <- valkey.lpop("mylist")
_ <- IO.println(s"Left pop: ${left.toOption}") // Some(Some("zero"))

// LINSERT
_ <- valkey.linsert("mylist", InsertPosition.Before, "two", "one-and-a-half")

// LMOVE - atomic pop+push between lists
_ <- valkey.rpush("source", "a", "b", "c")
moved <- valkey.lmove("source", "dest", ListDirection.Right, ListDirection.Left)
_ <- IO.println(s"Moved: ${moved.toOption}") // Some(Some("c"))
yield ()
}

Blocking operations

import cats.effect.*
import dev.profunktor.valkey4cats.Valkey
import dev.profunktor.valkey4cats.effect.Log
import dev.profunktor.valkey4cats.arguments.ListDirection

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

Valkey[IO].utf8("valkey://localhost:6379").use { valkey =>
for
// BLPOP - blocking left pop (5 second timeout)
result <- valkey.blpop(List("queue1", "queue2"), 5.0)
_ <- result.toOption.flatten match
case Some((key, value)) => IO.println(s"Got $value from $key")
case None => IO.println("Timeout, no element available")
yield ()
}

Available commands

CommandMethodReturn type
LPUSHlpush(key, elements*)F[ValkeyResponse[Long]]
RPUSHrpush(key, elements*)F[ValkeyResponse[Long]]
LPOPlpop(key)F[ValkeyResponse[Option[V]]]
RPOPrpop(key)F[ValkeyResponse[Option[V]]]
LPOP COUNTlpopCount(key, count)F[ValkeyResponse[List[V]]]
RPOP COUNTrpopCount(key, count)F[ValkeyResponse[List[V]]]
LRANGElrange(key, start, stop)F[ValkeyResponse[List[V]]]
LINDEXlindex(key, index)F[ValkeyResponse[Option[V]]]
LLENllen(key)F[ValkeyResponse[Long]]
LTRIMltrim(key, start, stop)F[ValkeyResponse[Unit]]
LSETlset(key, index, element)F[ValkeyResponse[Unit]]
LREMlrem(key, count, element)F[ValkeyResponse[Long]]
LINSERTlinsert(key, position, pivot, element)F[ValkeyResponse[InsertResult]]
LPOSlpos(key, element)F[ValkeyResponse[Option[Long]]]
LPUSHXlpushx(key, elements*)F[ValkeyResponse[Long]]
RPUSHXrpushx(key, elements*)F[ValkeyResponse[Long]]
LMOVElmove(src, dst, from, to)F[ValkeyResponse[Option[V]]]
BLPOPblpop(keys, timeout)F[ValkeyResponse[Option[(K, V)]]]
BRPOPbrpop(keys, timeout)F[ValkeyResponse[Option[(K, V)]]]
BLMOVEblmove(src, dst, from, to, timeout)F[ValkeyResponse[Option[V]]]
LMPOPlmpop(keys, direction)F[ValkeyResponse[Option[(K, List[V])]]]
BLMPOPblmpop(keys, direction, timeout)F[ValkeyResponse[Option[(K, List[V])]]]