Skip to main content

Hashes API

Purely functional interface for the Hashes API.

Hashes are maps between string fields and string values, making them perfect for representing objects.

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
// HSET - set multiple fields
_ <- valkey.hset("user:1", Map(
"name" -> "Alice",
"email" -> "alice@example.com",
"age" -> "30"
))

// HGET - get a single field
name <- valkey.hget("user:1", "name")
_ <- name match
case Ok(Some(v)) => IO.println(s"Name: $v")
case Ok(None) => IO.println("Field not found")
case Err(e) => IO.println(s"Error: ${e.message}")

// HGETALL - get all fields and values
all <- valkey.hgetall("user:1")
_ <- IO.println(s"All fields: ${all.toOption}")

// HDEL - delete fields
deleted <- valkey.hdel("user:1", "age")
_ <- IO.println(s"Deleted fields: ${deleted.toOption}") // Some(1)

// HINCRBY
_ <- valkey.hset("user:1", Map("score" -> "100"))
newScore <- valkey.hincrBy("user:1", "score", 50)
_ <- IO.println(s"New score: ${newScore.toOption}") // Some(150)
yield ()
}

Hash field expiration (Valkey 8.0+)

Valkey4Cats supports per-field expiration on hashes:

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

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

Valkey[IO].utf8("valkey://localhost:6379").use { valkey =>
for
_ <- valkey.hset("session:abc", Map("token" -> "xyz", "user" -> "alice"))
_ <- valkey.hexpire("session:abc", 60, "token")
ttl <- valkey.httl("session:abc", "token")
_ <- IO.println(s"Token TTL: ${ttl.toOption}")
yield ()
}

Available commands

CommandMethodReturn type
HSEThset(key, fieldValues)F[ValkeyResponse[Long]]
HGEThget(key, field)F[ValkeyResponse[Option[V]]]
HGETALLhgetall(key)F[ValkeyResponse[Map[K, V]]]
HMGEThmget(key, fields*)F[ValkeyResponse[List[Option[V]]]]
HDELhdel(key, fields*)F[ValkeyResponse[Long]]
HEXISTShexists(key, field)F[ValkeyResponse[Boolean]]
HKEYShkeys(key)F[ValkeyResponse[List[K]]]
HVALShvals(key)F[ValkeyResponse[List[V]]]
HLENhlen(key)F[ValkeyResponse[Long]]
HINCRBYhincrBy(key, field, increment)F[ValkeyResponse[Long]]
HINCRBYFLOAThincrByFloat(key, field, increment)F[ValkeyResponse[Double]]
HSETNXhsetnx(key, field, value)F[ValkeyResponse[Boolean]]
HSTRLENhstrlen(key, field)F[ValkeyResponse[Long]]
HRANDFIELDhrandfield(key)F[ValkeyResponse[Option[K]]]
HSCANhscan(key, cursor)F[ValkeyResponse[ScanResult[...]]]
HEXPIREhexpire(key, seconds, fields*)F[ValkeyResponse[List[Long]]]
HPEXPIREhpexpire(key, millis, fields*)F[ValkeyResponse[List[Long]]]
HTTLhttl(key, fields*)F[ValkeyResponse[List[Long]]]
HPTTLhpttl(key, fields*)F[ValkeyResponse[List[Long]]]
HPERSISThpersist(key, fields*)F[ValkeyResponse[List[Long]]]
HGETEXhgetex(key, expiry, fields*)F[ValkeyResponse[List[Option[V]]]]
HSETEXhsetex(key, fieldValues, expiry)F[ValkeyResponse[Long]]