public
Created

UnifiedJedisScala3 - testing scala to remove massive code duplication of previous versions

  • Download Gist
UnifiedJedisScala3.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
package test
 
import redis.clients.util.Pool
import redis.clients.jedis._
 
class UnifiedJedisScala3 {
val readPool: Pool[Jedis]
val masterPool: Pool[Jedis]
val currentDB: Int = 0;
val redundancySwitch: Int = 3;
val writing: Int = 0;
 
def get(key: Array[Byte]): Array[Byte] = {
val f: (Transaction) => Response[Array[Byte]] = _.get(key)
wrap[Array[Byte]](f, redundancySwitch, currentDB, key).get()
}
 
def set(key: Array[Byte], value: Array[Byte]): Array[Byte] = {
val f: (Transaction) => Response[Array[Byte]] = _.set(key, value)
wrap[Array[Byte]](f, writing, currentDB, key).get()
}
 
def wrap[Array[Byte]](x: (Transaction) => Response[Array[Byte]], switcher: Int, db: Int, key: Array[Byte]*): Response[Array[Byte]] = {
var result: Response[Array[Byte]] = null
val jedis: Jedis = if (switcher > 0) readPool.getResource else masterPool.getResource
 
try {
key.foreach(jedis.watch(_))
val transa = jedis.multi()
transa.select(db)
result = x(transa)
transa.exec
}
catch {
case _: Exception => result = wrap[Array[Byte]](x, switcher - 1, db, key)
}
finally {
if (switcher > 0) readPool.returnResource(jedis) else masterPool.returnResource(jedis)
}
result
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.