首先添加jedis的jar: ```xml redis.clients jedis 2.7.3 ``` 接下来就是直接贴代码了,请看如下scala class,非常简单: ```java import java.util import x.x.x.x.RedisConnector._ import redis.clients.jedis.{JedisCluster, HostAndPort} import scala.collection.JavaConversions._ import scala.collection.mutable import redis.clients.jedis.Jedis class RedisDao extends Serializable{ /** * * @param key * @param value * @return */ def set(key:String,value:Any): Unit ={ clients.set(key,String.valueOf(value)) } def get(key:String) : Option[String] = { val value = clients.get(key); if(value == null) None else Some(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("172.25.13.43",8002)) // val clients = new JedisCluster(jedisClusterNodes) val clients = new Jedis("172.25.13.43",8002); } ``` <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-4353345653789615" data-ad-slot="8840342077"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({});</script>首先添加jedis的jar: ```xml redis.clients jedis 2.7.3 ` 你的当前访问异常,请进行认证后继续阅读剩余内容。 提交