我在播放应用程序中使用 Redis 作为捕捉。使用 Redis,我可以存储一些与键字符串相关联的字符串列表,就像在 case class Cache1
中一样:
case class Cache1(val hostname : String, val port : Int, val timeout : Int) {
val pool : Pool =
new Pool(new JedisPool(new JedisPoolConfig(), hostname, port, timeout))
val j = pool.underlying.getResource
j.flushAll
pool.underlying.returnResourceObject(j)
def set(key : String, value : String) : Unit = pool.withClient { client =>
client.lpush(key, value)
}
def get(key : String) : Option[List[String]] = {
pool.withClient { client =>
val l : List[String] =
Dress.up(client).lrange(key, 0, Dress.up(client).llen(key)-1)
if(l.length == 0) return None else return Some(l)
}
}
}
我想重现相同的案例类,但不是将 String
存储为值,我想存储一个 ListBuffer[List[Double]]
。但是我在 redis API 中找不到这样做的方法,这就是我在这里问这个问题的原因。
最佳答案
我会将整个结构存储为 JSON,并以 JSON 读取它。这很简单且易于维护。
Redis 存储列表
object MyJacksonMapper extends JacksonMapper
val jsonListBuffer= MyJacksonMapper.serializeJson(myListBuffer)
Dress.up(client).set("listbuffer",jsonListBuffer)
Redis 获取
val json = Dress.up(client).get("listbuffer")
val myNewObject = MyJacksonMapper.deserializeJson[ListBuffer](json)
关于scala - 使用 Redis 存储 ListBuffer[List[Double]],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33756073/