Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support GETEX and GETDEL #340

Merged
merged 10 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions redis/src/main/scala/zio/redis/Input.scala
Original file line number Diff line number Diff line change
Expand Up @@ -465,29 +465,33 @@ object Input {
Chunk.single(encodeString(data.stringify))
}

case object GetExPersistInput extends Input[(String, Boolean)] {
override private[redis] def encode(data: (String, Boolean)): Chunk[RespValue.BulkString] =
if (data._2) Chunk(encodeString(data._1), encodeString("PERSIST")) else Chunk(encodeString(data._1))
}
case object GetExInput extends Input[(String, Expire, Duration)] {
override private[redis] def encode(data: (String, Expire, Duration)): Chunk[RespValue.BulkString] =
case class GetExPersistInput[K: Schema]() extends Input[(K, Boolean)] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case classes should be final.

override private[redis] def encode(data: (K, Boolean))(implicit codec: Codec): Chunk[RespValue.BulkString] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove override private. I noticed that some of the recently added inputs are using it, but please remove it everywhere together with the ones from this PR.

if (data._2) Chunk(encodeBytes(data._1), encodeString("PERSIST")) else Chunk(encodeBytes(data._1))
}
case class GetExInput[K: Schema]() extends Input[(K, Expire, Duration)] {
override private[redis] def encode(
data: (K, Expire, Duration)
)(implicit codec: Codec): Chunk[RespValue.BulkString] =
data match {
case (key, Expire.SetExpireSeconds, duration) =>
Chunk(encodeString(key), encodeString("EX")) ++ DurationSecondsInput.encode(duration)
Chunk(encodeBytes(key), encodeString("EX")) ++ DurationSecondsInput.encode(duration)
case (key, Expire.SetExpireMilliseconds, duration) =>
Chunk(encodeString(key), encodeString("PX")) ++ DurationMillisecondsInput.encode(duration)
case _ => Chunk(encodeString(data._1))
Chunk(encodeBytes(key), encodeString("PX")) ++ DurationMillisecondsInput.encode(duration)
case _ => Chunk(encodeBytes(data._1))
}
}

case object GetExAtInput extends Input[(String, ExpiredAt, Instant)] {
override private[redis] def encode(data: (String, ExpiredAt, Instant)): Chunk[RespValue.BulkString] =
case class GetExAtInput[K: Schema]() extends Input[(K, ExpiredAt, Instant)] {
override private[redis] def encode(
data: (K, ExpiredAt, Instant)
)(implicit codec: Codec): Chunk[RespValue.BulkString] =
data match {
case (key, ExpiredAt.SetExpireAtSeconds, instant) =>
Chunk(encodeString(key), encodeString("EXAT")) ++ TimeSecondsInput.encode(instant)
Chunk(encodeBytes(key), encodeString("EXAT")) ++ TimeSecondsInput.encode(instant)
case (key, ExpiredAt.SetExpireAtMilliseconds, instant) =>
Chunk(encodeString(key), encodeString("PXAT")) ++ TimeMillisecondsInput.encode(instant)
case _ => Chunk(encodeString(data._1))
Chunk(encodeBytes(key), encodeString("PXAT")) ++ TimeMillisecondsInput.encode(instant)
case _ => Chunk(encodeBytes(data._1))
}
}

Expand Down
154 changes: 49 additions & 105 deletions redis/src/main/scala/zio/redis/api/Strings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,10 @@ trait Strings {
* @param key Key to get the value of
* @return Returns the value of the string or None if it did not previously have a value
*/
final def getDel(key: String): ZIO[RedisExecutor, RedisError, Option[String]] =
GetDel.run(key)
final def getDel[K: Schema, R: Schema](key: K): ZIO[RedisExecutor, RedisError, Option[R]] = {
val command = RedisCommand(GetDel, ArbitraryInput[K](), OptionalOutput(ArbitraryOutput[R]()))
command.run(key)
}

/**
* Get the value of key and set its expiration
Expand All @@ -183,8 +185,14 @@ trait Strings {
* @param expireTime Time in seconds/milliseconds until the string should expire
* @return Returns the value of the string or None if it did not previously have a value
*/
final def getEx(key: String, expire: Expire, expireTime: Duration): ZIO[RedisExecutor, RedisError, Option[String]] =
GetEx.run((key, expire, expireTime))
final def getEx[K: Schema, R: Schema](
key: K,
expire: Expire,
expireTime: Duration
): ZIO[RedisExecutor, RedisError, Option[R]] = {
val command = RedisCommand(GetEx, GetExInput[K](), OptionalOutput(ArbitraryOutput[R]()))
command.run((key, expire, expireTime))
}

/**
* Get the value of key and set its expiration
Expand All @@ -194,12 +202,14 @@ trait Strings {
* @param timestamp an absolute Unix timestamp (seconds/milliseconds since January 1, 1970)
* @return Returns the value of the string or None if it did not previously have a value
*/
final def getExAt(
key: String,
final def getEx[K: Schema, R: Schema](
key: K,
expiredAt: ExpiredAt,
timestamp: Instant
): ZIO[RedisExecutor, RedisError, Option[String]] =
GetExAt.run((key, expiredAt, timestamp))
): ZIO[RedisExecutor, RedisError, Option[R]] = {
val command = RedisCommand(GetEx, GetExAtInput[K](), OptionalOutput(ArbitraryOutput[R]()))
command.run((key, expiredAt, timestamp))
}

/**
* Get the value of key and remove the time to live associated with the key
Expand All @@ -208,8 +218,10 @@ trait Strings {
* @param persist if true, remove the time to live associated with the key, otherwise not
* @return Returns the value of the string or None if it did not previously have a value
*/
final def getEx(key: String, persist: Boolean): ZIO[RedisExecutor, RedisError, Option[String]] =
GetExPersist.run((key, persist))
final def getEx[K: Schema, R: Schema](key: K, persist: Boolean): ZIO[RedisExecutor, RedisError, Option[R]] = {
val command = RedisCommand(GetEx, GetExPersistInput[K](), OptionalOutput(ArbitraryOutput[R]()))
command.run((key, persist))
}

/**
* Increment the integer value of a key by one
Expand Down Expand Up @@ -421,99 +433,31 @@ trait Strings {
}

private[redis] object Strings {

final val Append: RedisCommand[(String, String), Long] =
RedisCommand("APPEND", Tuple2(StringInput, StringInput), LongOutput)

final val BitCount: RedisCommand[(String, Option[Range]), Long] =
RedisCommand("BITCOUNT", Tuple2(StringInput, OptionalInput(RangeInput)), LongOutput)

final val BitField: RedisCommand[(String, (BitFieldCommand, List[BitFieldCommand])), Chunk[Option[Long]]] =
RedisCommand(
"BITFIELD",
Tuple2(StringInput, NonEmptyList(BitFieldCommandInput)),
ChunkOptionalLongOutput
)

final val BitOp: RedisCommand[(BitOperation, String, (String, List[String])), Long] =
RedisCommand("BITOP", Tuple3(BitOperationInput, StringInput, NonEmptyList(StringInput)), LongOutput)

final val BitPos: RedisCommand[(String, Boolean, Option[BitPosRange]), Long] =
RedisCommand("BITPOS", Tuple3(StringInput, BoolInput, OptionalInput(BitPosRangeInput)), LongOutput)

final val Decr: RedisCommand[String, Long] = RedisCommand("DECR", StringInput, LongOutput)

final val DecrBy: RedisCommand[(String, Long), Long] =
RedisCommand("DECRBY", Tuple2(StringInput, LongInput), LongOutput)

final val Get: RedisCommand[String, Option[String]] =
RedisCommand("GET", StringInput, OptionalOutput(MultiStringOutput))

final val GetBit: RedisCommand[(String, Long), Long] =
RedisCommand("GETBIT", Tuple2(StringInput, LongInput), LongOutput)

final val GetRange: RedisCommand[(String, Range), String] =
RedisCommand("GETRANGE", Tuple2(StringInput, RangeInput), MultiStringOutput)

final val GetSet: RedisCommand[(String, String), Option[String]] =
RedisCommand("GETSET", Tuple2(StringInput, StringInput), OptionalOutput(MultiStringOutput))

final val GetDel: RedisCommand[String, Option[String]] =
RedisCommand("GETDEL", StringInput, OptionalOutput(MultiStringOutput))

final val GetEx: RedisCommand[(String, Expire, Duration), Option[String]] =
RedisCommand("GETEX", GetExInput, OptionalOutput(MultiStringOutput))

final val GetExAt: RedisCommand[(String, ExpiredAt, Instant), Option[String]] =
RedisCommand("GETEX", GetExAtInput, OptionalOutput(MultiStringOutput))

final val GetExPersist: RedisCommand[(String, Boolean), Option[String]] =
RedisCommand("GETEX", GetExPersistInput, OptionalOutput(MultiStringOutput))

final val Incr: RedisCommand[String, Long] = RedisCommand("INCR", StringInput, LongOutput)

final val IncrBy: RedisCommand[(String, Long), Long] =
RedisCommand("INCRBY", Tuple2(StringInput, LongInput), LongOutput)

final val IncrByFloat: RedisCommand[(String, Double), String] =
RedisCommand("INCRBYFLOAT", Tuple2(StringInput, DoubleInput), MultiStringOutput)

final val MGet: RedisCommand[(String, List[String]), Chunk[Option[String]]] =
RedisCommand("MGET", NonEmptyList(StringInput), ChunkOptionalMultiStringOutput)

final val MSet: RedisCommand[((String, String), List[(String, String)]), Unit] =
RedisCommand("MSET", NonEmptyList(Tuple2(StringInput, StringInput)), UnitOutput)

final val MSetNx: RedisCommand[((String, String), List[(String, String)]), Boolean] =
RedisCommand("MSETNX", NonEmptyList(Tuple2(StringInput, StringInput)), BoolOutput)

final val PSetEx: RedisCommand[(String, Duration, String), Unit] =
RedisCommand("PSETEX", Tuple3(StringInput, DurationMillisecondsInput, StringInput), UnitOutput)

final val Set: RedisCommand[(String, String, Option[Duration], Option[Update], Option[KeepTtl]), Boolean] =
RedisCommand(
"SET",
Tuple5(
StringInput,
StringInput,
OptionalInput(DurationTtlInput),
OptionalInput(UpdateInput),
OptionalInput(KeepTtlInput)
),
SetOutput
)

final val SetBit: RedisCommand[(String, Long, Boolean), Boolean] =
RedisCommand("SETBIT", Tuple3(StringInput, LongInput, BoolInput), BoolOutput)

final val SetEx: RedisCommand[(String, Duration, String), Unit] =
RedisCommand("SETEX", Tuple3(StringInput, DurationSecondsInput, StringInput), UnitOutput)

final val SetNx: RedisCommand[(String, String), Boolean] =
RedisCommand("SETNX", Tuple2(StringInput, StringInput), BoolOutput)

final val SetRange: RedisCommand[(String, Long, String), Long] =
RedisCommand("SETRANGE", Tuple3(StringInput, LongInput, StringInput), LongOutput)

final val StrLen: RedisCommand[String, Long] = RedisCommand("STRLEN", StringInput, LongOutput)
final val Append = "APPEND"
final val BitCount = "BITCOUNT"
final val BitField = "BITFIELD"
final val BitOp = "BITOP"
final val BitPos = "BITPOS"
final val Decr = "DECR"
final val DecrBy = "DECRBY"
final val Get = "GET"
final val GetBit = "GETBIT"
final val GetRange = "GETRANGE"
final val GetSet = "GETSET"
final val Incr = "INCR"
final val IncrBy = "INCRBY"
final val IncrByFloat = "INCRBYFLOAT"
final val MGet = "MGET"
final val MSet = "MSET"
final val MSetNx = "MSETNX"
final val PSetEx = "PSETEX"
final val Set = "SET"
final val SetBit = "SETBIT"
final val SetEx = "SETEX"
final val SetNx = "SETNX"
final val SetRange = "SETRANGE"
final val StrLen = "STRLEN"
final val StrAlgoLcs = "STRALGO LCS"
final val GetDel = "GETDEL"
final val GetEx = "GETEX"
}
14 changes: 8 additions & 6 deletions redis/src/test/scala/zio/redis/InputSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1127,8 +1127,10 @@ object InputSpec extends BaseSpec {
suite("GetEx")(
testM("GetExInput - valid value") {
for {
resultSeconds <- Task(GetExInput.encode(scala.Tuple3.apply("key", Expire.SetExpireSeconds, 1.second)))
resultMilliseconds <- Task(GetExInput.encode(scala.Tuple3("key", Expire.SetExpireMilliseconds, 100.millis)))
resultSeconds <-
Task(GetExInput[String]().encode(scala.Tuple3.apply("key", Expire.SetExpireSeconds, 1.second)))
resultMilliseconds <-
Task(GetExInput[String]().encode(scala.Tuple3("key", Expire.SetExpireMilliseconds, 100.millis)))
} yield assert(resultSeconds)(equalTo(respArgs("key", "EX", "1"))) && assert(resultMilliseconds)(
equalTo(respArgs("key", "PX", "100"))
)
Expand All @@ -1137,13 +1139,13 @@ object InputSpec extends BaseSpec {
for {
resultSeconds <-
Task(
GetExAtInput.encode(
GetExAtInput[String]().encode(
scala.Tuple3("key", ExpiredAt.SetExpireAtSeconds, Instant.parse("2021-04-06T00:00:00Z"))
)
)
resultMilliseconds <-
Task(
GetExAtInput.encode(
GetExAtInput[String]().encode(
scala.Tuple3("key", ExpiredAt.SetExpireAtMilliseconds, Instant.parse("2021-04-06T00:00:00Z"))
)
)
Expand All @@ -1153,8 +1155,8 @@ object InputSpec extends BaseSpec {
},
testM("GetExPersistInput - valid value") {
for {
result <- Task(GetExPersistInput.encode("key" -> true))
resultWithoutOption <- Task(GetExPersistInput.encode("key" -> false))
result <- Task(GetExPersistInput[String]().encode("key" -> true))
resultWithoutOption <- Task(GetExPersistInput[String]().encode("key" -> false))
} yield assert(result)(equalTo(respArgs("key", "PERSIST"))) &&
assert(resultWithoutOption)(equalTo(respArgs("key")))
}
Expand Down
36 changes: 18 additions & 18 deletions redis/src/test/scala/zio/redis/StringsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1651,59 +1651,59 @@ trait StringsSpec extends BaseSpec {
key <- uuid
value <- uuid
_ <- pSetEx(key, 10.millis, value)
exists <- getEx(key, true)
exists <- getEx[String, String](key, true)
_ <- ZIO.sleep(20.millis)
res <- get(key)
res <- get[String, String](key)
} yield assert(res.isDefined)(equalTo(true)) && assert(exists)(equalTo(Some(value)))
} @@ eventually,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a question why is this test @eventually?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because sleep is unreliable and subject to CPU and thread scheduling. 20ms is likely to cause failure. Here, it is similar to CAS. (guess)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah true, I forgot that we use the Live Clock in these tests.

testM("not found value when set seconds ttl") {
for {
key <- uuid
value <- uuid
_ <- set(key, value)
exists <- getEx(key, Expire.SetExpireSeconds, 1.second)
exists <- getEx[String, String](key, Expire.SetExpireSeconds, 1.second)
_ <- ZIO.sleep(1020.millis)
res <- get(key)
res <- get[String, String](key)
} yield assert(res.isDefined)(equalTo(false)) && assert(exists)(equalTo(Some(value)))
} @@ eventually,
testM("not found value when set milliseconds ttl") {
for {
key <- uuid
value <- uuid
_ <- set(key, value)
exists <- getEx(key, Expire.SetExpireMilliseconds, 10.millis)
exists <- getEx[String, String](key, Expire.SetExpireMilliseconds, 10.millis)
_ <- ZIO.sleep(20.millis)
res <- get(key)
res <- get[String, String](key)
} yield assert(res.isDefined)(equalTo(false)) && assert(exists)(equalTo(Some(value)))
} @@ eventually,
testM("not found value when set seconds timestamp") {
for {
key <- uuid
value <- uuid
_ <- set(key, value)
exists <- getExAt(key, ExpiredAt.SetExpireAtSeconds, Instant.now().plusMillis(10))
exists <- getEx[String, String](key, ExpiredAt.SetExpireAtSeconds, Instant.now().plusMillis(10))
_ <- ZIO.sleep(20.millis)
res <- get(key)
res <- get[String, String](key)
} yield assert(res.isDefined)(equalTo(false)) && assert(exists)(equalTo(Some(value)))
} @@ eventually,
testM("not found value when set milliseconds timestamp") {
for {
key <- uuid
value <- uuid
_ <- set(key, value)
exists <- getExAt(key, ExpiredAt.SetExpireAtMilliseconds, Instant.now().plusMillis(10))
exists <- getEx[String, String](key, ExpiredAt.SetExpireAtMilliseconds, Instant.now().plusMillis(10))
_ <- ZIO.sleep(20.millis)
res <- get(key)
res <- get[String, String](key)
} yield assert(res.isDefined)(equalTo(false)) && assert(exists)(equalTo(Some(value)))
} @@ eventually,
testM("key not found") {
for {
key <- uuid
value <- uuid
_ <- set(key, value)
res <- getExAt(value, ExpiredAt.SetExpireAtMilliseconds, Instant.now().plusMillis(10))
res2 <- getEx(value, Expire.SetExpireMilliseconds, 10.millis)
res3 <- getEx(value, true)
_ <- set[String, String](key, value)
res <- getEx[String, String](value, ExpiredAt.SetExpireAtMilliseconds, Instant.now().plusMillis(10))
res2 <- getEx[String, String](value, Expire.SetExpireMilliseconds, 10.millis)
res3 <- getEx[String, String](value, true)
} yield assert(res)(equalTo(None)) && assert(res2)(equalTo(None)) && assert(res3)(equalTo(None))
} @@ eventually
),
Expand All @@ -1712,22 +1712,22 @@ trait StringsSpec extends BaseSpec {
for {
key <- uuid
_ <- sAdd(key, "a")
res <- getDel(key).either
res <- getDel[String, String](key).either
} yield assert(res)(isLeft(isSubtype[WrongType](anything)))
},
testM("key not exists") {
for {
key <- uuid
res <- getDel(key)
res <- getDel[String, String](key)
} yield assert(res)(equalTo(None))
},
testM("get and remove key") {
for {
key <- uuid
value <- uuid
_ <- set(key, value)
res <- getDel(key)
notFound <- getDel(key)
res <- getDel[String, String](key)
notFound <- getDel[String, String](key)
} yield assert(res)(equalTo(Some(value))) && assert(notFound)(equalTo(None))
}
)
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.