Created
March 19, 2024 16:03
-
-
Save zmccoy/bf93e78d30cee68e44fc56d6a20a84c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats.effect._ | |
import fs2.Stream | |
import scala.concurrent.duration.FiniteDuration | |
import cats.effect.std._ | |
import com.comcast.ip4s._ | |
//import dev.profunktor.redis4cats.connection.RedisURI | |
//import dev.profunktor.redis4cats.RedisCommands | |
//import io.lettuce.core.SocketOptions | |
import scala.concurrent.duration.DurationDouble | |
//import io.lettuce.core.protocol.RedisCommand | |
import io.chrisdavenport.rediculous._ | |
import com.comcast.ip4s._ | |
import fs2.io.net.tls.TLSContext | |
/* | |
object Rediculous extends IOApp.Simple { | |
import io.chrisdavenport.rediculous._ | |
val command = io.chrisdavenport.rediculous.RedisCommands.ping[Redis[IO, *]] | |
val context = TLSContext.Builder.forAsync[IO].system | |
val pooledConn: Resource[IO, RedisConnection[IO]] = | |
Resource.eval(context).flatMap(c => | |
RedisConnection.pool[IO] | |
.withHost(host"azure-cache-testing.redis.cache.windows.net") | |
.withPort(port"6380") | |
.withAuth(None, Secrets.cacheKey) | |
.withTLS | |
.build | |
) | |
val queuedConn: Resource[IO, RedisConnection[IO]] = | |
Resource.eval(context).flatMap(c => | |
RedisConnection.queued[IO] | |
.withHost(host"azure-cache-testing.redis.cache.windows.net") | |
.withPort(port"6380") | |
.withAuth(None, Secrets.cacheKey) | |
.withTLS | |
.withWorkers(1) | |
.withMaxQueued(10000) | |
.build | |
) | |
def prog(c: RedisConnection[IO]) = { | |
IO.println("Connection Acquired") >> | |
command.run(c) | |
.flatTap(_ => Clock[IO].realTime.flatMap(IO.println)).timeout(scala.concurrent.duration.Duration("10 seconds")).recoverWith(t => IO.println(s"Errorrrr: \n $t")) | |
} | |
def streamThem = Stream.resource(pooledConn).flatMap { c => | |
Stream.awakeEvery[IO](FiniteDuration.apply(100, "millisecond")) >> Stream.eval(prog(c)) | |
}.compile.drain | |
def run = streamThem | |
} | |
*/ | |
object Redis4Cats extends IOApp.Simple { | |
import cats.effect._ | |
import cats.implicits._ | |
import dev.profunktor.redis4cats.Redis | |
import dev.profunktor.redis4cats.connection.RedisClient | |
import dev.profunktor.redis4cats.effect.Log.Stdout._ | |
import io.lettuce.core.ClientOptions | |
import dev.profunktor.redis4cats.config._ | |
import dev.profunktor.redis4cats.data.RedisCodec | |
val stringCodec: RedisCodec[String, String] = RedisCodec.Utf8 | |
val redis: Resource[IO, RedisCommands[IO, String, String]] = | |
for { | |
client <- RedisClient[IO].fromUri( | |
RedisURI.fromUnderlying(io.lettuce.core.RedisURI.Builder.redis(Secrets.hostName).withPort(Secrets.port).withPassword(Secrets.cacheKey).withSsl(true).build())) | |
_ <- Resource.eval(IO.println("lol made another")) | |
r <- Redis[IO].fromClient(client, stringCodec) | |
} yield r | |
def redisProgram(r: RedisCommands[IO, String, String]): IO[Unit] = | |
(for { | |
s <- r.set("redis4Cats", "Here") | |
g <- r.get("redis4Cats") | |
c <- Clock[IO].realTime.flatMap(IO.println) | |
_ <- IO.println("Got " + g + " " + c) | |
} yield ()).timeout(scala.concurrent.duration.Duration("10 seconds")).recoverWith(t => IO.println(s"Errorrrr: \n $t")) | |
val everyXReadandWrite = Stream.resource(redis).flatMap(redis => Stream.awakeEvery[IO]((FiniteDuration.apply(100, "millisecond"))) >> Stream.eval(redisProgram(redis))).compile.drain | |
def run = everyXReadandWrite | |
} | |
/* | |
//Jedis pool | |
//Fail over works fine. | |
object JedisPool extends IOApp.Simple { | |
import redis.clients.jedis.{JedisPool, JedisPoolConfig} | |
val pooled = new JedisPool(new JedisPoolConfig(), Secrets.hostName, Secrets.port, 500, 100, Secrets.cacheKey, 0, "clientname", true) //This one works but gets a connection disconnect randomly, stops and comes back up. Maybe related to the timeout value? | |
val pooledold = new JedisPool(new JedisPoolConfig(), Secrets.hostName, Secrets.port, 500, Secrets.cacheKey, true) //This one works! (Suggested for now) | |
val poolResource = Resource.make(IO.delay(pooled.getResource()))(jedis => IO.delay(pooled.returnResource(jedis))) | |
val commandsPool: IO[Unit] = poolResource.use { j => | |
(for { | |
_ <- IO.delay(j.set("jedisPooled", "message")) | |
got <- IO.delay(j.get("jedisPooled")) | |
_ <- IO.println("Got " + got) | |
} yield ()).recoverWith(t => IO.println(t)) | |
} | |
val everyXPool = (Stream.awakeEvery[IO](FiniteDuration.apply(100,"millisecond")) >> Stream.eval(commandsPool)).compile.drain | |
def run = everyXPool | |
} | |
object Jedis extends IOApp.Simple { | |
import redis.clients.jedis.{Jedis} | |
import redis.clients.jedis.DefaultJedisClientConfig | |
val client = new Jedis(Secrets.hostName, Secrets.port, DefaultJedisClientConfig.builder().password(Secrets.cacheKey).ssl(true).build()) | |
val commands: IO[Unit] = | |
for { | |
s <- IO.delay(client.set("jedis", "This is my message")) | |
_ <- IO.println("Set " + s) | |
got <- IO.delay(client.get("jedis")) | |
_ <- IO.println("Get " + got) | |
} yield () | |
val everyX = (Stream.awakeEvery[IO](FiniteDuration.apply(10, "millisecond")) >> Stream.eval(commands)).compile.drain | |
def run = everyX | |
} | |
//Lettuce is also used, but is internally used in Redis4Cats, so no more testing is needed. | |
*/ | |
object Secrets { | |
val hostName = "10.235.92.150" | |
val port = 6379 | |
val cacheKey = "a57342dd-b389-4e68-a63b-a5f5c6f15f77" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment