|
| 1 | +/* |
| 2 | + * Copyright 2018-2021 ProfunKtor |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package dev.profunktor.redis4cats.extensions |
| 18 | + |
| 19 | +import cats.effect.kernel.{ Resource, Sync } |
| 20 | +import cats.syntax.all._ |
| 21 | +import cats.{ ApplicativeThrow, Functor } |
| 22 | +import dev.profunktor.redis4cats.algebra.Scripting |
| 23 | +import dev.profunktor.redis4cats.effects.ScriptOutputType |
| 24 | +import io.lettuce.core.RedisNoScriptException |
| 25 | + |
| 26 | +object luaScripting { |
| 27 | + |
| 28 | + final case class LuaScript(contents: String, sha: String) |
| 29 | + |
| 30 | + object LuaScript { |
| 31 | + |
| 32 | + def make[F[_]: Functor](redis: Scripting[F, _, _])(contents: String): F[LuaScript] = |
| 33 | + redis.digest(contents).map(sha => LuaScript(contents, sha)) |
| 34 | + |
| 35 | + /** Helper to load a lua script from resources/lua/{resourceName}. The path to the lua scripts can be configured. |
| 36 | + * @param redis |
| 37 | + * redis commands |
| 38 | + * @param resourceName |
| 39 | + * filename of the lua script |
| 40 | + * @param pathToScripts |
| 41 | + * path to the lua scripts |
| 42 | + * @return |
| 43 | + * [[LuaScript]] |
| 44 | + */ |
| 45 | + def loadFromResources[F[_]: Sync](redis: Scripting[F, _, _])( |
| 46 | + resourceName: String, |
| 47 | + pathToScripts: String = "lua" |
| 48 | + ): F[LuaScript] = |
| 49 | + Resource |
| 50 | + .fromAutoCloseable( |
| 51 | + Sync[F].blocking( |
| 52 | + scala.io.Source.fromResource(resource = s"$pathToScripts/$resourceName") |
| 53 | + ) |
| 54 | + ) |
| 55 | + .evalMap(fileSrc => Sync[F].blocking(fileSrc.mkString)) |
| 56 | + .use(make(redis)) |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + implicit class LuaScriptingExtensions[F[_]: ApplicativeThrow, K, V](redis: Scripting[F, K, V]) { |
| 61 | + |
| 62 | + /** Evaluate the cached lua script via it's sha. If the script is not cached, fallback to evaluating the script |
| 63 | + * directly. |
| 64 | + * @param luaScript |
| 65 | + * the lua script with its content and sha |
| 66 | + * @param output |
| 67 | + * output of script |
| 68 | + * @param keys |
| 69 | + * keys to script |
| 70 | + * @param values |
| 71 | + * values to script |
| 72 | + * @return |
| 73 | + * ScriptOutputType |
| 74 | + */ |
| 75 | + def evalLua( |
| 76 | + luaScript: LuaScript, |
| 77 | + output: ScriptOutputType[V], |
| 78 | + keys: List[K], |
| 79 | + values: List[V] |
| 80 | + ): F[output.R] = |
| 81 | + redis |
| 82 | + .evalSha( |
| 83 | + luaScript.sha, |
| 84 | + output, |
| 85 | + keys, |
| 86 | + values |
| 87 | + ) |
| 88 | + .recoverWith { case _: RedisNoScriptException => |
| 89 | + redis.eval(luaScript.contents, output, keys, values) |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | +} |
0 commit comments