包含该问题的 git 存储库可以在这里找到https://github.com/mdedetrich/scalacache-example
我目前遇到的问题是我试图让我的 ScalaCache 后端不可知,它可以在运行时使用类型安全配置进行配置。
我遇到的问题是 ScalaCache 参数化了缓存的构造函数,即构建一个你会做的咖啡因缓存
ScalaCache(CaffeineCache())
至于SentinelRedisCache
你会在哪里做
ScalaCache(SentinelRedisCache("", Set.empty, ""))
就我而言,我创建了一个通用缓存包装器MyCache
,如下所示
import scalacache.ScalaCache
import scalacache.serialization.Codec
final case class MyCache[CacheRepr](scalaCache: ScalaCache[CacheRepr])(
implicit stringCodec: Codec[Int, CacheRepr]) {
def putInt(value: Int) = scalaCache.cache.put[Int]("my_int", value, None)
}
我们需要随身携带,CacheRepr
因为这就是 ScalaCache 知道如何序列化任何类型的方式T
。CaffeineCache
使用 a CacheRepr
which is InMemoryRepr
where asSentinelRedisCache
使用 a CacheRepr
which is Array[Byte]
。
这就是问题的症结所在,我有一个Config
只存储正在使用的缓存的地方,即
import scalacache.Cache
import scalacache.caffeine.CaffeineCache
import scalacache.redis.SentinelRedisCache
final case class ApplicationConfig(cache: Cache[_])
它 a 的原因Cache[_]
是因为在编译时我们不知道正在使用什么缓存,ApplicationConfig
将在运行时使用CaffeineCache
/实例化SentinelRedisCache
。
这就是问题的症结所在,Codec
如果我们只是将我们applicationConfig.cache
作为构造函数,Scala 无法找到通配符类型的隐式,即https://github.com/mdedetrich/scalacache-example/blob/master/src /main/scala/Main.scala#L17
如果我们取消注释上面的行,我们得到
[error] /Users/mdedetrich/github/scalacache-example/src/main/scala/Main.scala:17:37: Could not find any Codecs for type Int and _$1. Please provide one or import scalacache._
[error] Error occurred in an application involving default arguments.
[error] val myCache3: MyCache[_] = MyCache(ScalaCache(applicationConfig.cache)) // This doesn't
有谁知道如何解决这个问题,本质上我想在我的ApplicationConfig
, 缓存中指定类型Cache[InMemoryRepr | Array[Byte]]
而不是仅仅Cache[_]
(以便 Scala 编译器知道查找任何一个的隐含InMemoryRepr or Array[Byte]
并MyCache
定义类似这样的内容
final case class MyCache[CacheRepr <: InMemoryRepr | Array[Byte]](scalaCache: ScalaCache[CacheRepr])