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

Add evalReadOnly & evalShaReadOnly #880

Merged
merged 3 commits into from
Jun 5, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,3 @@ trait RedisSyntax {
}

object literals extends RedisSyntax


yisraelU marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ trait Scripting[F[_], K, V] {
def eval(script: String, output: ScriptOutputType[V]): F[output.R]
def eval(script: String, output: ScriptOutputType[V], keys: List[K]): F[output.R]
def eval(script: String, output: ScriptOutputType[V], keys: List[K], values: List[V]): F[output.R]
def evalReadOnly(script: String, output: ScriptOutputType[V]): F[output.R]
def evalReadOnly(script: String, output: ScriptOutputType[V], keys: List[K]): F[output.R]
def evalReadOnly(script: String, output: ScriptOutputType[V], keys: List[K], values: List[V]): F[output.R]
def evalSha(digest: String, output: ScriptOutputType[V]): F[output.R]
def evalSha(digest: String, output: ScriptOutputType[V], keys: List[K]): F[output.R]
def evalSha(digest: String, output: ScriptOutputType[V], keys: List[K], values: List[V]): F[output.R]
def evalShaReadOnly(digest: String, output: ScriptOutputType[V]): F[output.R]
def evalShaReadOnly(digest: String, output: ScriptOutputType[V], keys: List[K]): F[output.R]
def evalShaReadOnly(digest: String, output: ScriptOutputType[V], keys: List[K], values: List[V]): F[output.R]
def scriptLoad(script: String): F[String]
def scriptLoad(script: Array[Byte]): F[String]
def scriptExists(digests: String*): F[List[Boolean]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import io.lettuce.core.{
}
import org.typelevel.keypool.KeyPool

import java.nio.charset.StandardCharsets
import java.time.Instant
import java.util.concurrent.TimeUnit
import scala.concurrent.duration._
Expand Down Expand Up @@ -1314,7 +1315,40 @@ private[redis4cats] class BaseRedis[F[_]: FutureLift: MonadThrow: Log, K, V](
_.eval[output.Underlying](
script,
output.outputType,
// see comment in eval above
// see comment in eval above.
keys.toArray[Any].asInstanceOf[Array[K with Object]],
values: _*
).futureLift.map(output.convert(_))
)

override def evalReadOnly(script: String, output: ScriptOutputType[V]): F[output.R] =
async
.flatMap(
_.evalReadOnly[output.Underlying](
script.getBytes(StandardCharsets.UTF_8),
yisraelU marked this conversation as resolved.
Show resolved Hide resolved
output.outputType,
// see comment in eval above.
Array.emptyObjectArray.asInstanceOf[Array[K with Object]]
).futureLift
)
.map(r => output.convert(r))

override def evalReadOnly(script: String, output: ScriptOutputType[V], keys: List[K]): F[output.R] =
async.flatMap(
_.evalReadOnly[output.Underlying](
script.getBytes(StandardCharsets.UTF_8),
output.outputType,
// see comment in eval above.
keys.toArray[Any].asInstanceOf[Array[K with Object]]
).futureLift.map(output.convert(_))
)

override def evalReadOnly(script: String, output: ScriptOutputType[V], keys: List[K], values: List[V]): F[output.R] =
async.flatMap(
_.evalReadOnly[output.Underlying](
script.getBytes(StandardCharsets.UTF_8),
output.outputType,
// see comment in eval above.
keys.toArray[Any].asInstanceOf[Array[K with Object]],
values: _*
).futureLift.map(output.convert(_))
Expand All @@ -1330,7 +1364,7 @@ private[redis4cats] class BaseRedis[F[_]: FutureLift: MonadThrow: Log, K, V](
_.evalsha[output.Underlying](
digest,
output.outputType,
// see comment in eval above
// see comment in eval above.
keys.toArray[Any].asInstanceOf[Array[K with Object]]
).futureLift.map(output.convert(_))
)
Expand All @@ -1340,7 +1374,45 @@ private[redis4cats] class BaseRedis[F[_]: FutureLift: MonadThrow: Log, K, V](
_.evalsha[output.Underlying](
digest,
output.outputType,
// see comment in eval above
// see comment in eval above.
keys.toArray[Any].asInstanceOf[Array[K with Object]],
values: _*
).futureLift.map(output.convert(_))
)

override def evalShaReadOnly(digest: String, output: ScriptOutputType[V]): F[output.R] =
async
.flatMap(
_.evalshaReadOnly[output.Underlying](
digest,
output.outputType,
// see comment in eval above.
Array.emptyObjectArray.asInstanceOf[Array[K with Object]]
).futureLift
)
.map(output.convert(_))

override def evalShaReadOnly(digest: String, output: ScriptOutputType[V], keys: List[K]): F[output.R] =
async.flatMap(
_.evalshaReadOnly[output.Underlying](
digest,
output.outputType,
// see comment in eval above.
keys.toArray[Any].asInstanceOf[Array[K with Object]]
).futureLift.map(output.convert(_))
)

override def evalShaReadOnly(
digest: String,
output: ScriptOutputType[V],
keys: List[K],
values: List[V]
): F[output.R] =
async.flatMap(
_.evalshaReadOnly[output.Underlying](
digest,
output.outputType,
// see comment in eval above.
keys.toArray[Any].asInstanceOf[Array[K with Object]],
values: _*
).futureLift.map(output.convert(_))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,10 +592,23 @@ trait TestScenarios { self: FunSuite =>
)
)
_ <- IO(assertEquals(list, List("Let", "us", "have", "fun")))
boolReadOnly <- redis.evalReadOnly("return true", ScriptOutputType.Boolean, List("Foo"))
_ <- IO(assert(boolReadOnly))
_ <- redis.eval(statusScript, ScriptOutputType.Status, List("test"), List("foo"))
either <- redis.evalReadOnly(statusScript, ScriptOutputType.Status, List("test"), List("foo")).attempt
_ <- IO(
assert(
either.left.exists { ex =>
ex.isInstanceOf[RedisCommandExecutionException] &&
ex.getMessage.startsWith("ERR Write commands are not allowed from read-only scripts")
}
)
)
sha42 <- redis.scriptLoad("return 42")
fortyTwoSha <- redis.evalSha(sha42, ScriptOutputType.Integer)
_ <- IO(assertEquals(fortyTwoSha, 42L))
fortyTwoShaReadOnly <- redis.evalShaReadOnly(sha42, ScriptOutputType.Integer)
_ <- IO(assertEquals(fortyTwoShaReadOnly, 42L))
shaStatusScript <- redis.scriptLoad(statusScript)
_ <- redis.evalSha(shaStatusScript, ScriptOutputType.Status, List("test"), List("foo", "bar"))
exists <- redis.scriptExists(sha42, "foobar")
Expand Down
Loading