Scala 操作Redis使用连接池工具类RedisUtil

网友投稿 326 2023-01-04


Scala 操作Redis使用连接池工具类RedisUtil

本文介绍了Scala 操作Redis,分享给大家,具体如下:

package com.zjw.util

import java.util

import org.apache.commons.pool2.impl.GenericObjectPoolConfig

import org.apache.logging.log4j.scala.Logging

import redis.clients.jedis.{Jedis, JedisPool, Response}

import redis.clients.util.Pool

object RedisUtil extends Logging {

private[this] var jedisPool: Pool[Jedis] = _

def main(args: Array[String]): Unit = {

val password = "h-{

val host = "192.168.100.101"

val port = 6379

val timeout = 1000

RedisUtil.init(host, port, timeout, password, 0)

//RedisUtil.set("Time".getBytes(), "2018-09-03 09:00:00".getBytes())

//val result = RedisUtil.get("Time".getBytes())

//println(new String(result))

//val map = Map("name"-> "zhangsan","age"-> "21", "gender"-> "male", "id"-> "519099386")

//RedisUtil.setCols("hash",map)

// val result = RedisUtil.getCols("hash", Array("name", "age", "xx")).map(x => (x._1, new String(x._2)))

// logger.info(result)

val result = RedisUtil.bulkGetCols(Array("hash", "ss"))

logger.info(s"result: ${result}")

}

def init(host: String, port: Int, timeout: Int, password: String, database: Int = 0): Unit = {

jedisPool = new JedisPool(new GenericObjectPoolConfig, host, port, timeout, password, database)

}

def get(key: Array[Byte]): Array[Byte] = {

val jedis = jedisPool.getResource

val result: Array[Byte] = jedis.get(key)

jedis.close()

result

}

def set(key: Array[Byte], value: Array[Byte]): Boolean = {

try {

val jedis = jedisPool.getResource

jedis.set(key, value)

jedis.close()

true

} catch {

case e: Exception => {

logger.error(s"写入数据到Redis出错: ${e}")

false

}

}

}

def getCols(key: String,

cols: Array[String] = Array.empty

): Map[String, Array[Byte]] = {

import scala.collectioNfIgqn.JavaConverters._

val jedis = jedisPoNfIgqol.getResource

var map = Map.empty[String, Array[Byte]]

if (cols.length > 0) {

val pipe = jedis.pipelined()

val response = 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}")

val tmpMap: util.Map[Array[Byte], Array[Byte]] = jedis.hgetAll(key.getBytes())

map = tmpMap.asScala.toMap.map(x => (new String(x._1), x._2))

}

jedis.close

map

}

def getCols2(

key: String,

cols: Array[String] = Array.empty

): Map[String, Array[Byte]] = {

val jedis = jedisPool.getResource

var map = Map.empty[String, Array[Byte]]

if (cols.length > 0) {

for (col <- cols) {

val value: Array[Byte] = jedis.hget(key.getBytes(), col.getBytes())

if (null != value) {

map = map + (col -> value)

}

}

} else {

logger.info(s"rowkey: ${key}")

val tmpMap: util.Map[Array[Byte], Array[Byte]] = jedis.hgetAll(key.getBytes())

import scala.collection.JavaConverters._

map = tmpMap.asScala.toMap.map(x => (new String(x._1), x._2))

}

jedis.close

map

}

def bulkGetCols(keys: Array[String],

cols: Array[String] = Array.empty

): Map[String, Map[String, Array[Byte]]] = {

import scala.collection.JavaConverters._

var result: Map[String, Map[String, Array[Byte]]] = Map.empty

val jedis = jedisPool.getResource

val pipe = jedis.pipelined

if (cols.length > 0) {

val data = 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 {

val data: 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 => (new String(x._1), x._2)).toMap))

.toMap.filter(_._2.nonEmpty)

}

result

}

def bulkGetCols2(rowkeys: Array[String],

cols: Array[String] = Array.empty

): Map[String, Map[String, Array[Byte]]] = {

val jedis = jedisPool.getResource

var map = Map.empty[String, Map[String, Array[Byte]]]

import scala.collection.JavaConverters._

for (rowkey <- rowkeys) {

var cellMap = Map.empty[String, Array[Byte]]

if (cols.length > 0) {

for (col <- cols) {

val value = jedis.hget(rowkey.getBytes(), col.getBytes())

if (null != value) {

cellMap = cellMap + (col -> value)

}

}

} else {

logger.info(s"rowkey: ${rowkey}")

val tmpMap = jedis.hgetAll(rowkey.getBytes())

cellMap = tmpMap.asScala.NfIgqtoMap.map(x => (new String(x._1), x._2))

}

if (cellMap.nonEmpty) {

map = map + (rowkey -> cellMap)

}

}

jedis.close

map

}

def setCols(

key: String,

fieldValues: Map[String, String]

): Unit = {

import scala.collection.JavaConverters._

val data = fieldValues.map(element => {

(element._1.getBytes(), element._2.getBytes())

}).asJava

val jedis = jedisPool.getResource

jedis.hmset(key.getBytes(), data)

jedis.close()

}

}

val host = "192.168.100.101"

val port = 6379

val timeout = 1000

RedisUtil.init(host, port, timeout, password, 0)

//RedisUtil.set("Time".getBytes(), "2018-09-03 09:00:00".getBytes())

//val result = RedisUtil.get("Time".getBytes())

//println(new String(result))

//val map = Map("name"-> "zhangsan","age"-> "21", "gender"-> "male", "id"-> "519099386")

//RedisUtil.setCols("hash",map)

// val result = RedisUtil.getCols("hash", Array("name", "age", "xx")).map(x => (x._1, new String(x._2)))

// logger.info(result)

val result = RedisUtil.bulkGetCols(Array("hash", "ss"))

logger.info(s"result: ${result}")

}

def init(host: String, port: Int, timeout: Int, password: String, database: Int = 0): Unit = {

jedisPool = new JedisPool(new GenericObjectPoolConfig, host, port, timeout, password, database)

}

def get(key: Array[Byte]): Array[Byte] = {

val jedis = jedisPool.getResource

val result: Array[Byte] = jedis.get(key)

jedis.close()

result

}

def set(key: Array[Byte], value: Array[Byte]): Boolean = {

try {

val jedis = jedisPool.getResource

jedis.set(key, value)

jedis.close()

true

} catch {

case e: Exception => {

logger.error(s"写入数据到Redis出错: ${e}")

false

}

}

}

def getCols(key: String,

cols: Array[String] = Array.empty

): Map[String, Array[Byte]] = {

import scala.collectioNfIgqn.JavaConverters._

val jedis = jedisPoNfIgqol.getResource

var map = Map.empty[String, Array[Byte]]

if (cols.length > 0) {

val pipe = jedis.pipelined()

val response = 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}")

val tmpMap: util.Map[Array[Byte], Array[Byte]] = jedis.hgetAll(key.getBytes())

map = tmpMap.asScala.toMap.map(x => (new String(x._1), x._2))

}

jedis.close

map

}

def getCols2(

key: String,

cols: Array[String] = Array.empty

): Map[String, Array[Byte]] = {

val jedis = jedisPool.getResource

var map = Map.empty[String, Array[Byte]]

if (cols.length > 0) {

for (col <- cols) {

val value: Array[Byte] = jedis.hget(key.getBytes(), col.getBytes())

if (null != value) {

map = map + (col -> value)

}

}

} else {

logger.info(s"rowkey: ${key}")

val tmpMap: util.Map[Array[Byte], Array[Byte]] = jedis.hgetAll(key.getBytes())

import scala.collection.JavaConverters._

map = tmpMap.asScala.toMap.map(x => (new String(x._1), x._2))

}

jedis.close

map

}

def bulkGetCols(keys: Array[String],

cols: Array[String] = Array.empty

): Map[String, Map[String, Array[Byte]]] = {

import scala.collection.JavaConverters._

var result: Map[String, Map[String, Array[Byte]]] = Map.empty

val jedis = jedisPool.getResource

val pipe = jedis.pipelined

if (cols.length > 0) {

val data = 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 {

val data: 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 => (new String(x._1), x._2)).toMap))

.toMap.filter(_._2.nonEmpty)

}

result

}

def bulkGetCols2(rowkeys: Array[String],

cols: Array[String] = Array.empty

): Map[String, Map[String, Array[Byte]]] = {

val jedis = jedisPool.getResource

var map = Map.empty[String, Map[String, Array[Byte]]]

import scala.collection.JavaConverters._

for (rowkey <- rowkeys) {

var cellMap = Map.empty[String, Array[Byte]]

if (cols.length > 0) {

for (col <- cols) {

val value = jedis.hget(rowkey.getBytes(), col.getBytes())

if (null != value) {

cellMap = cellMap + (col -> value)

}

}

} else {

logger.info(s"rowkey: ${rowkey}")

val tmpMap = jedis.hgetAll(rowkey.getBytes())

cellMap = tmpMap.asScala.NfIgqtoMap.map(x => (new String(x._1), x._2))

}

if (cellMap.nonEmpty) {

map = map + (rowkey -> cellMap)

}

}

jedis.close

map

}

def setCols(

key: String,

fieldValues: Map[String, String]

): Unit = {

import scala.collection.JavaConverters._

val data = fieldValues.map(element => {

(element._1.getBytes(), element._2.getBytes())

}).asJava

val jedis = jedisPool.getResource

jedis.hmset(key.getBytes(), data)

jedis.close()

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:购物网站系统接口设计方案(电子购物网站的设计与实现)
下一篇:Spring中使用atomikos+druid实现经典分布式事务的方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~