0

包含该问题的 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 知道如何序列化任何类型的方式TCaffeineCache使用 a CacheReprwhich is InMemoryReprwhere asSentinelRedisCache使用 a CacheReprwhich 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])
4

1 回答 1

2

您似乎要求编译器根据缓存类型的运行时选择来解析隐式值。这是不可能的,因为在应用程序代码启动时编译器已不再运行。

您必须在编译时而不是运行时进行类型解析。因此,您需要定义一个trait代表缓存的抽象接口,并提供一个工厂函数,该函数根据ApplicationConfig. 它可能看起来像这样(未经测试):

sealed trait MyScalaCache {
  def putInt(value: Int)
}

object MyScalaCache {
  def apply(): MyScalaCache =
    if (ApplicationConfig.useCaffine) {
      MyCache(ScalaCache(CaffeineCache())
    } else {
      MyCache(ScalaCache(SentinelRedisCache("", Set.empty, ""))
    }
}

final case class MyCache[CacheRepr](scalaCache: ScalaCache[CacheRepr]) extends MyScalaCache (
  implicit stringCodec: Codec[Int, CacheRepr]) {

  def putInt(value: Int) = scalaCache.cache.put[Int]("my_int", value, None)
}

编译器将在编译时解析隐式 in MyCache,其中两个具体实例在apply.

于 2018-10-05T13:11:28.250 回答