Scala 操作Redis使用连接池工具类RedisUtil
本文介绍了Scala操作Redis,分享给大家,具体如下:
packagecom.zjw.util importjava.util importorg.apache.commons.pool2.impl.GenericObjectPoolConfig importorg.apache.logging.log4j.scala.Logging importredis.clients.jedis.{Jedis,JedisPool,Response} importredis.clients.util.Pool objectRedisUtilextendsLogging{ private[this]varjedisPool:Pool[Jedis]=_ defmain(args:Array[String]):Unit={ valpassword="h-{"zhangsan","age"->"21","gender"->"male","id"->"519099386") //RedisUtil.setCols("hash",map) //valresult=RedisUtil.getCols("hash",Array("name","age","xx")).map(x=>(x._1,newString(x._2))) //logger.info(result) valresult=RedisUtil.bulkGetCols(Array("hash","ss")) logger.info(s"result:${result}") } definit(host:String,port:Int,timeout:Int,password:String,database:Int=0):Unit={ jedisPool=newJedisPool(newGenericObjectPoolConfig,host,port,timeout,password,database) } defget(key:Array[Byte]):Array[Byte]={ valjedis=jedisPool.getResource valresult:Array[Byte]=jedis.get(key) jedis.close() result } defset(key:Array[Byte],value:Array[Byte]):Boolean={ try{ valjedis=jedisPool.getResource jedis.set(key,value) jedis.close() true }catch{ casee:Exception=>{ logger.error(s"写入数据到Redis出错:${e}") false } } } defgetCols(key:String, cols:Array[String]=Array.empty ):Map[String,Array[Byte]]={ importscala.collection.JavaConverters._ valjedis=jedisPool.getResource varmap=Map.empty[String,Array[Byte]] if(cols.length>0){ valpipe=jedis.pipelined() valresponse=pipe.hmget(key.getBytes(),cols.map(_.getBytes()):_*) pipe.sync() map=cols.zip(response.get.asScala).toMap.filter(x=>x._2!=null) pipe.close() }else{ logger.info(s"key:${key}") valtmpMap:util.Map[Array[Byte],Array[Byte]]=jedis.hgetAll(key.getBytes()) map=tmpMap.asScala.toMap.map(x=>(newString(x._1),x._2)) } jedis.close map } defgetCols2( key:String, cols:Array[String]=Array.empty ):Map[String,Array[Byte]]={ valjedis=jedisPool.getResource varmap=Map.empty[String,Array[Byte]] if(cols.length>0){ for(col<-cols){ valvalue:Array[Byte]=jedis.hget(key.getBytes(),col.getBytes()) if(null!=value){ map=map+(col->value) } } }else{ logger.info(s"rowkey:${key}") valtmpMap:util.Map[Array[Byte],Array[Byte]]=jedis.hgetAll(key.getBytes()) importscala.collection.JavaConverters._ map=tmpMap.asScala.toMap.map(x=>(newString(x._1),x._2)) } jedis.close map } defbulkGetCols(keys:Array[String], cols:Array[String]=Array.empty ):Map[String,Map[String,Array[Byte]]]={ importscala.collection.JavaConverters._ varresult:Map[String,Map[String,Array[Byte]]]=Map.empty valjedis=jedisPool.getResource valpipe=jedis.pipelined if(cols.length>0){ valdata=keys.map(x=>{ pipe.hmget(x.getBytes(),cols.map(_.getBytes()):_*) }) pipe.sync pipe.close jedis.close result=keys.zip(data.map(_.get().asScala.toArray).map(cols.zip(_).toMap.filter(null!=_._2))) .toMap.filter(_._2.nonEmpty) }else{ valdata:Array[Response[util.Map[Array[Byte],Array[Byte]]]]=keys.map(x=>{ pipe.hgetAll(x.getBytes()) }) pipe.sync pipe.close jedis.close result=keys.zip(data.map(_.get().asScala.map(x=>(newString(x._1),x._2)).toMap)) .toMap.filter(_._2.nonEmpty) } result } defbulkGetCols2(rowkeys:Array[String], cols:Array[String]=Array.empty ):Map[String,Map[String,Array[Byte]]]={ valjedis=jedisPool.getResource varmap=Map.empty[String,Map[String,Array[Byte]]] importscala.collection.JavaConverters._ for(rowkey<-rowkeys){ varcellMap=Map.empty[String,Array[Byte]] if(cols.length>0){ for(col<-cols){ valvalue=jedis.hget(rowkey.getBytes(),col.getBytes()) if(null!=value){ cellMap=cellMap+(col->value) } } }else{ logger.info(s"rowkey:${rowkey}") valtmpMap=jedis.hgetAll(rowkey.getBytes()) cellMap=tmpMap.asScala.toMap.map(x=>(newString(x._1),x._2)) } if(cellMap.nonEmpty){ map=map+(rowkey->cellMap) } } jedis.close map } defsetCols( key:String, fieldValues:Map[String,String] ):Unit={ importscala.collection.JavaConverters._ valdata=fieldValues.map(element=>{ (element._1.getBytes(),element._2.getBytes()) }).asJava valjedis=jedisPool.getResource jedis.hmset(key.getBytes(),data) jedis.close() } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。