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 'GET' option in 'SET' command #801

Merged
merged 4 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions modules/redis/src/main/scala/zio/redis/Input.scala
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ object Input {
RespCommand(RespCommandArgument.Literal("FREQ"), RespCommandArgument.Unknown(data.frequency))
}

case object GetKeywordInput extends Input[GetKeyword] {
def encode(data: GetKeyword): RespCommand =
RespCommand(RespCommandArgument.Literal(data.asString))
}

case object IdleTimeInput extends Input[IdleTime] {
def encode(data: IdleTime): RespCommand =
RespCommand(RespCommandArgument.Literal("IDLETIME"), RespCommandArgument.Unknown(data.seconds.toString))
Expand Down
38 changes: 37 additions & 1 deletion modules/redis/src/main/scala/zio/redis/api/Strings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,42 @@ trait Strings extends RedisEnvironment {
command.run((key, expiration, value))
}

/**
* Set the string value of a key with a 'GET' option.
*
* @param key
* Key of the string to set
* @param value
* Value to set
* @param expireTime
* Time until the string expires
* @param update
* Update can be Update.SetExisting which only sets the key if it exists, or Update.SetNew which nly sets the key if
* it does not exist
* @param keepTtl
* When set any previously set expire time remains unchanged
* @return
* the old value stored at key, or None if key did not exist
*/
final def setGet[K: Schema, V: Schema](
key: K,
value: V,
expireTime: Option[Duration] = None,
update: Option[Update] = None,
keepTtl: Option[KeepTtl] = None
): IO[RedisError, Option[V]] = {
val input = Tuple6(
ArbitraryKeyInput[K](),
ArbitraryValueInput[V](),
OptionalInput(DurationTtlInput),
OptionalInput(UpdateInput),
OptionalInput(KeepTtlInput),
GetKeywordInput
)
val command = RedisCommand(Set, input, OptionalOutput(ArbitraryOutput[V]()), executor)
command.run((key, value, expireTime, update, keepTtl, GetKeyword))
}

/**
* Set the value of a key, only if the key does not exist.
*
Expand All @@ -543,7 +579,7 @@ trait Strings extends RedisEnvironment {
* Overwrite part of a string at key starting at the specified offset.
*
* @param key
* Key of the string to overwite
* Key of the string to overwrite
* @param offset
* Offset to start writing
* @param value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ trait Strings {
case Expire.SetExpireMilliseconds => "PX"
}
}

object Expire {
case object SetExpireSeconds extends Expire
case object SetExpireMilliseconds extends Expire
Expand All @@ -130,8 +131,15 @@ trait Strings {
case ExpiredAt.SetExpireAtMilliseconds => "PXAT"
}
}

object ExpiredAt {
case object SetExpireAtSeconds extends ExpiredAt
case object SetExpireAtMilliseconds extends ExpiredAt
}

case object GetKeyword {
private[redis] def asString: String = "GET"
}

type GetKeyword = GetKeyword.type
}
7 changes: 7 additions & 0 deletions modules/redis/src/test/scala/zio/redis/InputSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,13 @@ object InputSpec extends BaseSpec {
} yield assert(result)(equalTo(RespCommand(Literal("GET"), Unknown("mypattern_*"))))
}
),
suite("GetKeyword")(
test("valid value") {
for {
result <- ZIO.attempt(GetKeywordInput.encode(GetKeyword))
} yield assert(result)(equalTo(RespCommand(Literal("GET"))))
}
),
suite("IdleTime")(
test("0 seconds") {
for {
Expand Down
18 changes: 18 additions & 0 deletions modules/redis/src/test/scala/zio/redis/KeysSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ trait KeysSpec extends BaseSpec {
v <- redis.get(key).returning[String]
} yield assert(v)(isSome(equalTo(value)))
},
test("setGet with non-existing key") {
for {
redis <- ZIO.service[Redis]
key <- uuid
value <- uuid
v <- redis.setGet(key, value)
} yield assert(v)(isNone)
},
test("setGet with the existing key") {
for {
redis <- ZIO.service[Redis]
value <- uuid
key <- uuid
_ <- redis.set(key, value)
newValue <- uuid
v <- redis.setGet(key, newValue)
} yield assert(v)(isSome(equalTo(value)))
},
test("get non-existing key") {
for {
redis <- ZIO.service[Redis]
Expand Down