【发布时间】:2014-08-27 03:39:59
【问题描述】:
我目前有一个基于Future[String] 更新 Redis 表的方法。
def update(key: String, timeStamp: Long, jsonStringF: Future[String], redisClient: RedisClient) = {
jsonStringF.map { jsonString =>
val historyKey = "history." + key
val tempKey = "temp." + key
val tran = redisClient.transaction()
tran.zadd(historyKey, (timeStamp, jsonString))
tran.del(tempKey)
val f = tran.exec()
f.onComplete {
case Success(suc) => dlogger.info(s"Updated $historyKey and deleted $tempKey successfully ....")
case Failure(ex) => dlogger.warn(s"Error updating tables", ex)
}
}
}
现在我有两个Future[String](jsonStringF1 和jsonStringF2),我想更新两个不同的表。
def update(key: String, timeStamp: Long, jsonStringF1: Future[String], jsonStringF2: Future[String], redisClient: RedisClient) = {
....
}
我想在jsonStringF2 中用String 更新另一个表("another." + key)。我怎样才能做到这一点 ?
更新:下面的代码是否正确?
def update(tableKey: String, timeStamp: Long, jsonStringF1: Future[String], jsonStringF2: Future[String], redisClient: RedisClient) =
{
for {
a <- jsonStringF1
t <- jsonStringF2
historyKey = "history." + tableKey
anotherKey = "another." + tableKey +
tempKey = "temp." + tableKey
tran = redisClient.transaction()
_ = tran.zadd(historyKey, (timeStamp, a))
_ = tran.zadd(anotherKey, (timeStamp, t))
_ = tran.del(tempKey)
f = tran.exec()
} yield ()
}
【问题讨论】: