scala使用redis client - Jedis

来源:转载

虽然scala也有很多redis的client,不过我在使用最新的Redis3.0集群模式的时候总是报错,只好用回java版本的client了。

报错如下

java.lang.Exception: MOVED 1133 192.168.6.53:6379

代码

package cn.www.daoimport java.utilimport cn.www.dao.RedisConnector.clientsimport redis.clients.jedis.{JedisCluster, HostAndPort}import scala.collection.JavaConversions._import scala.collection.mutableclass RedisDao extends Serializable{ /** * * @param key * @param value * @return */ def set(key:String,value:Any): Unit ={ clients.set(key,String.valueOf(value)) } /** * * @param key * @param time * @return */ def hget(key:String,time:Long): Option[String] ={ val value=clients.hget(key,String.valueOf(time)) if(value==null) None else Some(value) } /** * * @param key * @param time * @param value * @return */ def hset(key:String,time:Long,value:Any): Boolean ={ clients.hset(key,String.valueOf(time),String.valueOf(value))==1 } def hmset(key:String,map:mutable.Map[Long,String]): Unit ={ val map2=mutable.Map[String,String]() map.foreach{case (key:Long,value:String)=> map2.put(key.toString,value) } clients.hmset(key,mapAsJavaMap(map2)) } /** * * @param key * @param time * @return */ def hdel(key:String,time:Any): Option[Long] ={ Some(clients.hdel(key,String.valueOf(time))) } /** * * @param key * @param times * @return */ def rpush(key:String,times:Any): Option[Long] ={ Some(clients.rpush(key,String.valueOf(times))) } /** * * @param key * @return */ def lpop(key:String): Option[Long] ={ val time=clients.lpop(key) if(time==null) None else Some(time.toLong) } /** * * @param key * @return */ def lhead(key:String): Option[Long] ={ val head=clients.lindex(key,0) if(head==null) None else Some(head.toLong) }}object RedisConnector { private val jedisClusterNodes = new util.HashSet[HostAndPort]() jedisClusterNodes.add(new HostAndPort("192.168.6.52",6379)) jedisClusterNodes.add(new HostAndPort("192.168.6.53",6380)) jedisClusterNodes.add(new HostAndPort("192.168.6.53",6379)) val clients = new JedisCluster(jedisClusterNodes)}



分享给朋友:
您可能感兴趣的文章:
随机阅读: