Skip to content

Commit

Permalink
add unlink and random key commands
Browse files Browse the repository at this point in the history
  • Loading branch information
yisraelU committed Oct 1, 2024
1 parent 6fcd86e commit 9eea22a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
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 @@ -48,5 +49,7 @@ trait KeyCommands[F[_], K] {
def scan(previous: KeyScanCursor[K], scanArgs: ScanArgs): 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 @@ -574,6 +577,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(assertEquals(rkey, Some(key1)))
dump <- redis.dump(key1)
_ <- IO(assert(dump.nonEmpty))
_ <- redis.restore(key1, dump.get, RestoreArgs().replace(true))
Expand Down

0 comments on commit 9eea22a

Please sign in to comment.