-
Notifications
You must be signed in to change notification settings - Fork 90
Draft: Client side caching #1001
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
base: series/2.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright 2018-2021 ProfunKtor | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.profunktor.redis4cats.caching | ||
|
||
trait CacheAccessor[F[_], K, V] { | ||
def get(key: K): F[V] | ||
def put(key: K, value: V): F[Unit] | ||
def evict(key: K): F[Unit] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2018-2021 ProfunKtor | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dev.profunktor.redis4cats.caching | ||
|
||
import cats.effect.kernel.{ Async, Resource } | ||
import dev.profunktor.redis4cats.effect.TxExecutor | ||
import io.lettuce.core.TrackingArgs | ||
import io.lettuce.core.api.StatefulRedisConnection | ||
import io.lettuce.core.support.caching.{ | ||
CacheAccessor => JCacheAccessor, | ||
CacheFrontend => JCacheFrontend, | ||
ClientSideCaching => JClientSideCaching | ||
} | ||
|
||
object ClientSideCaching { | ||
|
||
def make[F[_]: Async, K, V]( | ||
connection: StatefulRedisConnection[K, V], | ||
args: TrackingArgs, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the convention in this lib , is to create a scala api for the java pojo's . i.e. |
||
cacheAccessor: CacheAccessor[F, K, V] | ||
): Resource[F, JCacheFrontend[K, V]] = | ||
TxExecutor.make[F].flatMap { redisExecutor => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. putting aside the changes to TxExecutor, we should create a facade for CacheFrontend similar to CacheAccessor and not require a Dispatcher and unsafeRun |
||
Resource.make[F, JCacheFrontend[K, V]] { | ||
Async[F].delay { | ||
JClientSideCaching.enable( | ||
new JCacheAccessor[K, V] { | ||
override def get(key: K): V = redisExecutor.unsafeRun(cacheAccessor.get(key)) | ||
override def put(key: K, value: V): Unit = redisExecutor.unsafeRun(cacheAccessor.put(key, value)) | ||
override def evict(key: K): Unit = redisExecutor.unsafeRun(cacheAccessor.evict(key)) | ||
}, | ||
connection, | ||
args | ||
) | ||
} | ||
}(cacheFrontend => Async[F].delay(cacheFrontend.close())) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,26 +29,30 @@ import scala.util.control.NonFatal | |
|
||
import cats.effect.kernel._ | ||
import cats.syntax.all._ | ||
import cats.effect.std.Dispatcher | ||
|
||
private[redis4cats] trait TxExecutor[F[_]] { | ||
def delay[A](thunk: => A): F[A] | ||
def eval[A](fa: F[A]): F[A] | ||
def start[A](fa: F[A]): F[Fiber[F, Throwable, A]] | ||
def liftK[G[_]: Async]: TxExecutor[G] | ||
def liftK[G[_]: Async: Dispatcher]: TxExecutor[G] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this would be a breaking change as it adds additional parameters. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add that |
||
def unsafeRun[A](fa: F[A]): A | ||
} | ||
|
||
private[redis4cats] object TxExecutor { | ||
def make[F[_]: Async]: Resource[F, TxExecutor[F]] = | ||
Resource | ||
.make(Sync[F].delay(Executors.newFixedThreadPool(1, TxThreadFactory))) { ec => | ||
Sync[F] | ||
.delay(ec.shutdownNow()) | ||
.ensure(new IllegalStateException("There were outstanding tasks at time of shutdown of the Redis thread"))( | ||
_.isEmpty | ||
) | ||
.void | ||
} | ||
.map(es => fromEC(exitOnFatal(ExecutionContext.fromExecutorService(es)))) | ||
Dispatcher.parallel[F].flatMap { dispatcher => | ||
Resource | ||
.make(Sync[F].delay(Executors.newFixedThreadPool(1, TxThreadFactory))) { ec => | ||
Sync[F] | ||
.delay(ec.shutdownNow()) | ||
.ensure(new IllegalStateException("There were outstanding tasks at time of shutdown of the Redis thread"))( | ||
_.isEmpty | ||
) | ||
.void | ||
} | ||
.map(es => fromEC(exitOnFatal(ExecutionContext.fromExecutorService(es)))(dispatcher)) | ||
} | ||
|
||
private def exitOnFatal(ec: ExecutionContext): ExecutionContext = new ExecutionContext { | ||
def execute(r: Runnable): Unit = | ||
|
@@ -70,11 +74,12 @@ private[redis4cats] object TxExecutor { | |
ec.reportFailure(t) | ||
} | ||
|
||
private def fromEC[F[_]: Async](ec: ExecutionContext): TxExecutor[F] = | ||
private def fromEC[F[_]: Async](ec: ExecutionContext)(dispatcher: Dispatcher[F]): TxExecutor[F] = | ||
new TxExecutor[F] { | ||
def delay[A](thunk: => A): F[A] = eval(Sync[F].delay(thunk)) | ||
def eval[A](fa: F[A]): F[A] = Async[F].evalOn(fa, ec) | ||
def start[A](fa: F[A]): F[Fiber[F, Throwable, A]] = Async[F].startOn(fa, ec) | ||
def liftK[G[_]: Async]: TxExecutor[G] = fromEC[G](ec) | ||
def liftK[G[_]: Async: Dispatcher]: TxExecutor[G] = fromEC[G](ec)(implicitly) | ||
def unsafeRun[A](fa: F[A]): A = dispatcher.unsafeRunSync(fa) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should be using the connection interface https://github.com/profunktor/redis4cats/blob/dd861e5f2cef45b2b0cfcf1aab0a5af5968f48a1/modules/core/src/main/scala/dev/profunktor/redis4cats/connection/RedisConnection.scala#L38C27-L38C42