我正在使用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)
}
}
}
我想复制同一个case类,但是要将
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/