Skip to content

Commit

Permalink
add unlink and random key commands (#930)
Browse files Browse the repository at this point in the history
* add unlink and random key commands

* fix tests
  • Loading branch information
yisraelU authored Nov 24, 2024
1 parent c6c511b commit 87cd3cb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ trait KeyCommands[F[_], K] {
def objectIdletime(key: K): F[Option[FiniteDuration]]
def persist(key: K): F[Boolean]
def pttl(key: K): F[Option[FiniteDuration]]
def randomKey: F[Option[K]]
// restores a key with the given serialized value, previously obtained using DUMP without a ttl
def restore(key: K, value: Array[Byte]): F[Unit]
def restore(key: K, value: Array[Byte], restoreArgs: RestoreArgs): F[Unit]
Expand All @@ -52,5 +53,7 @@ trait KeyCommands[F[_], K] {
def scan(cursor: KeyScanCursor[K], keyScanArgs: KeyScanArgs): F[KeyScanCursor[K]]
def typeOf(key: K): F[Option[RedisType]]
def ttl(key: K): F[Option[FiniteDuration]]
//This command is very similar to DEL: it removes the specified keys. Just like DEL a key is ignored if it does not exist. However the command performs the actual memory reclaiming in a different thread, so it is not blocking, while DEL is. This is where the command name comes from: the command just unlinks the keys from the keyspace. The actual removal will happen later asynchronously.
def unlink(key: K*): F[Long]

}
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,9 @@ private[redis4cats] class BaseRedis[F[_]: FutureLift: MonadThrow: Log, K, V](
override def pttl(key: K): F[Option[FiniteDuration]] =
async.flatMap(_.pttl(key).futureLift.map(toFiniteDuration(TimeUnit.MILLISECONDS)))

override def randomKey: F[Option[K]] =
async.flatMap(_.randomkey().futureLift.map(Option(_)))

override def restore(key: K, value: Array[Byte]): F[Unit] =
async.flatMap(_.restore(key, 0, value).futureLift.void)

Expand Down Expand Up @@ -580,6 +583,9 @@ private[redis4cats] class BaseRedis[F[_]: FutureLift: MonadThrow: Log, K, V](
override def typeOf(key: K): F[Option[RedisType]] =
async.flatMap(_.`type`(key).futureLift.map(RedisType.fromString))

override def unlink(key: K*): F[Long] =
async.flatMap(_.unlink(key: _*).futureLift.map(x => Long.box(x)))

/******************************* Transactions API **********************************/
// When in a cluster, transactions should run against a single node.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ trait TestScenarios { self: FunSuite =>
_ <- redis.set(key1, "some value")
exist2 <- redis.exists(key1)
_ <- IO(assert(exist2))
rkey <- redis.randomKey
_ <- IO(assert(rkey.forall(_ == key1)))
dump <- redis.dump(key1)
_ <- IO(assert(dump.nonEmpty))
_ <- redis.restore(key1, dump.get, RestoreArgs().replace(true))
Expand Down Expand Up @@ -301,7 +303,7 @@ trait TestScenarios { self: FunSuite =>
_ <- IO(assertEquals(kv, Some("v")))
kTtl2 <- redis.ttl("k")
_ <- IO(assert(kTtl2.nonEmpty))
_ <- redis.del("k")
_ <- redis.unlink("k")
tpe <- redis.typeOf("k")
_ <- IO(assertEquals(tpe, None))
_ <- redis.set("aV", "v")
Expand Down

0 comments on commit 87cd3cb

Please sign in to comment.