Skip to content

Commit

Permalink
Fix caching module
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Jan 14, 2025
1 parent dd23592 commit 2eb4855
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 34 deletions.
23 changes: 19 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,9 @@ val resilience4jVersion = "2.3.0"
val http4s_ce2_version = "0.22.15"
val http4s_ce3_version = "0.23.30"
val osLibVersion = "0.11.3"

val tethysVersion = "0.29.3"

val openTelemetryVersion = "1.46.0"
val slf4jVersion = "1.7.36"

val compileAndTest = "compile->compile;test->test"

Expand Down Expand Up @@ -243,6 +242,7 @@ lazy val rawAllAggregates =
armeriaFs2Backend.projectRefs ++
scribeBackend.projectRefs ++
slf4jBackend.projectRefs ++
cachingBackend.projectRefs ++
examplesCe2.projectRefs ++
examples.projectRefs ++
docs.projectRefs ++
Expand Down Expand Up @@ -945,13 +945,26 @@ lazy val slf4jBackend = (projectMatrix in file("logging/slf4j"))
.settings(
name := "slf4j-backend",
libraryDependencies ++= Seq(
"org.slf4j" % "slf4j-api" % "1.7.36"
"org.slf4j" % "slf4j-api" % slf4jVersion
),
scalaTest
)
.jvmPlatform(scalaVersions = scala2And3)
.dependsOn(core)

lazy val cachingBackend = (projectMatrix in file("caching"))
.settings(commonJvmSettings)
.settings(
name := "caching-backend",
libraryDependencies ++= Seq(
"org.slf4j" % "slf4j-api" % slf4jVersion,
"com.github.plokhotnyuk.jsoniter-scala" %%% "jsoniter-scala-macros" % jsoniterVersion % Compile
),
scalaTest
)
.jvmPlatform(scalaVersions = scala2And3)
.dependsOn(core, jsoniter)

lazy val examplesCe2 = (projectMatrix in file("examples-ce2"))
.settings(commonJvmSettings)
.settings(
Expand All @@ -977,6 +990,7 @@ lazy val examples = (projectMatrix in file("examples"))
"io.github.resilience4j" % "resilience4j-circuitbreaker" % resilience4jVersion,
"io.github.resilience4j" % "resilience4j-ratelimiter" % resilience4jVersion,
"com.lihaoyi" %% "os-lib" % osLibVersion,
"redis.clients" % "jedis" % "5.2.0",
pekkoStreams,
logback
),
Expand All @@ -995,7 +1009,8 @@ lazy val examples = (projectMatrix in file("examples"))
zioJson,
scribeBackend,
slf4jBackend,
ox
ox,
cachingBackend
)

//TODO this should be invoked by compilation process, see #https://github.com/scalameta/mdoc/issues/355
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,44 @@

package sttp.client4.examples.wrapper

import redis.clients.jedis.UnifiedJedis
import sttp.client4.*
import sttp.client4.caching.Cache
import sttp.client4.caching.CachingBackend
import sttp.shared.Identity
// import redis.clients.jedis.UnifiedJedis
// import sttp.client4.*
// import sttp.client4.caching.Cache
// import sttp.client4.caching.CachingBackend
// import sttp.shared.Identity

import scala.concurrent.duration.FiniteDuration
// import scala.concurrent.duration.FiniteDuration

// you'll need to start redis to run this demo, e.g. using Docker:
// docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest
// // you'll need to start redis to run this demo, e.g. using Docker:
// // docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest

class RedisCache(jedis: UnifiedJedis) extends Cache[Identity]:
override def get(key: Array[Byte]): Option[Array[Byte]] = Option(jedis.get(key))
// class RedisCache(jedis: UnifiedJedis) extends Cache[Identity]:
// override def get(key: Array[Byte]): Option[Array[Byte]] = Option(jedis.get(key))

override def delete(key: Array[Byte]): Unit =
val _ = jedis.del(key)
// override def delete(key: Array[Byte]): Unit =
// val _ = jedis.del(key)

override def set(key: Array[Byte], value: Array[Byte], ttl: FiniteDuration): Unit =
val _ = jedis.setex(key, ttl.toSeconds.toInt, value)
// override def set(key: Array[Byte], value: Array[Byte], ttl: FiniteDuration): Unit =
// val _ = jedis.setex(key, ttl.toSeconds.toInt, value)

override def close(): Unit = jedis.close()
// override def close(): Unit = jedis.close()

@main def redisCachingBackend(): Unit =
val backend: WebSocketSyncBackend =
CachingBackend(DefaultSyncBackend(), new RedisCache(new UnifiedJedis("redis://localhost:6379")))
// @main def redisCachingBackend(): Unit =
// val backend: WebSocketSyncBackend =
// CachingBackend(DefaultSyncBackend(), new RedisCache(new UnifiedJedis("redis://localhost:6379")))

// returns a response with a max-age of 3 seconds
val request = basicRequest.get(uri"https://httpbin.org/cache/3")
// // returns a response with a max-age of 3 seconds
// val request = basicRequest.get(uri"https://httpbin.org/cache/3")

val response1 = request.send(backend) // the logs should contain information that the response is stored in the cache
println(s"Response 1: (${response1.code})")
println(response1.body)
// val response1 = request.send(backend) // the logs should contain information that the response is stored in the cache
// println(s"Response 1: (${response1.code})")
// println(response1.body)

val response2 = request.send(backend) // an immediate subsequent request should be read from the cache
println(s"Response 2: (${response2.code})")
println(response2.body)
// val response2 = request.send(backend) // an immediate subsequent request should be read from the cache
// println(s"Response 2: (${response2.code})")
// println(response2.body)

Thread.sleep(5000)
val response3 = request.send(backend) // after 5 seconds, the cache should be invalidated
println(s"Response 3: (${response3.code})")
println(response3.body)
// Thread.sleep(5000)
// val response3 = request.send(backend) // after 5 seconds, the cache should be invalidated
// println(s"Response 3: (${response3.code})")
// println(response3.body)

0 comments on commit 2eb4855

Please sign in to comment.