jedis使用api(2)

2024-08-27 15:18
文章标签 使用 api jedis

本文主要是介绍jedis使用api(2),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

/** * 获取连接池. * @return 连接池实例 */  
private static JedisPool getPool(String ip,int port) {  JedisPoolConfig config = new JedisPoolConfig();  config.setMaxActive(RedisConfig.getMaxactive());  config.setMaxIdle(RedisConfig.getMaxidle());  config.setMaxWait(RedisConfig.getMaxwait());  config.setTestOnBorrow(true);  config.setTestOnReturn(true);  try{    /** *如果你遇到 java.net.SocketTimeoutException: Read timed out exception的异常信息 *请尝试在构造JedisPool的时候设置自己的超时值. JedisPool默认的超时时间是2秒(单位毫秒) */  pool = new JedisPool(config, ip, port,RedisConfig.getTimeout());  } catch(Exception e) {  e.printStackTrace();  }  return pool;  
}


package com.zhongsou.vertportal.util;  import java.util.HashMap;  
import java.util.Map;  import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;  import redis.clients.jedis.Jedis;  
import redis.clients.jedis.JedisPool;  
import redis.clients.jedis.JedisPoolConfig;  import com.zhongsou.vertportal.conf.BaseConfig;  
import com.zhongsou.vertportal.conf.RedisConfig;  /** * Redis工具类,用于获取RedisPool. * 参考官网说明如下: * You shouldn't use the same instance from different threads because you'll have strange errors. * And sometimes creating lots of Jedis instances is not good enough because it means lots of sockets and connections, * which leads to strange errors as well. A single Jedis instance is not threadsafe! * To avoid these problems, you should use JedisPool, which is a threadsafe pool of network connections. * This way you can overcome those strange errors and achieve great performance. * To use it, init a pool: *  JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost"); *  You can store the pool somewhere statically, it is thread-safe. *  JedisPoolConfig includes a number of helpful Redis-specific connection pooling defaults. *  For example, Jedis with JedisPoolConfig will close a connection after 300 seconds if it has not been returned. * @author wujintao */  
public class JedisUtil  {  protected Logger log = LoggerFactory.getLogger(getClass());  /** * 私有构造器. */  private JedisUtil() {  }  private static Map<String,JedisPool> maps  = new HashMap<String,JedisPool>();  /** * 获取连接池. * @return 连接池实例 */  private static JedisPool getPool(String ip,int port) {  String key = ip+":" +port;  JedisPool pool = null;  if(!maps.containsKey(key)) {  JedisPoolConfig config = new JedisPoolConfig();  config.setMaxActive(RedisConfig.getMaxactive());  config.setMaxIdle(RedisConfig.getMaxidle());  config.setMaxWait(RedisConfig.getMaxwait());  config.setTestOnBorrow(true);  config.setTestOnReturn(true);  try{    /** *如果你遇到 java.net.SocketTimeoutException: Read timed out exception的异常信息 *请尝试在构造JedisPool的时候设置自己的超时值. JedisPool默认的超时时间是2秒(单位毫秒) */  pool = new JedisPool(config, ip, port,RedisConfig.getTimeout());  maps.put(key, pool);  } catch(Exception e) {  e.printStackTrace();  }  }else{  pool = maps.get(key);  }  return pool;  }  /** *类级的内部类,也就是静态的成员式内部类,该内部类的实例与外部类的实例 *没有绑定关系,而且只有被调用到时才会装载,从而实现了延迟加载。 */  private static class RedisUtilHolder{  /** * 静态初始化器,由JVM来保证线程安全 */  private static JedisUtil instance = new JedisUtil();  }  /** *当getInstance方法第一次被调用的时候,它第一次读取 *RedisUtilHolder.instance,导致RedisUtilHolder类得到初始化;而这个类在装载并被初始化的时候,会初始化它的静 *态域,从而创建RedisUtil的实例,由于是静态的域,因此只会在虚拟机装载类的时候初始化一次,并由虚拟机来保证它的线程安全性。 *这个模式的优势在于,getInstance方法并没有被同步,并且只是执行一个域的访问,因此延迟初始化并没有增加任何访问成本。 */  public static JedisUtil getInstance() {  return RedisUtilHolder.instance;  }  /** * 获取Redis实例. * @return Redis工具类实例 */  public Jedis getJedis(String ip,int port) {  Jedis jedis  = null;  int count =0;  do{  try{   jedis = getPool(ip,port).getResource();  //log.info("get redis master1!");  } catch (Exception e) {  log.error("get redis master1 failed!", e);  // 销毁对象    getPool(ip,port).returnBrokenResource(jedis);    }  count++;  }while(jedis==null&&count<BaseConfig.getRetryNum());  return jedis;  }  /** * 释放redis实例到连接池. * @param jedis redis实例 */  public void closeJedis(Jedis jedis,String ip,int port) {  if(jedis != null) {  getPool(ip,port).returnResource(jedis);  }  }  
}



开发参考资料:

redis官方文档:http://redis.io/documentation

redis命令参考中文版:http://redis.readthedocs.org/en/2.4/index.html



这篇关于jedis使用api(2)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1112039

相关文章

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Pandas使用AdaBoost进行分类的实现

《Pandas使用AdaBoost进行分类的实现》Pandas和AdaBoost分类算法,可以高效地进行数据预处理和分类任务,本文主要介绍了Pandas使用AdaBoost进行分类的实现,具有一定的参... 目录什么是 AdaBoost?使用 AdaBoost 的步骤安装必要的库步骤一:数据准备步骤二:模型

使用Pandas进行均值填充的实现

《使用Pandas进行均值填充的实现》缺失数据(NaN值)是一个常见的问题,我们可以通过多种方法来处理缺失数据,其中一种常用的方法是均值填充,本文主要介绍了使用Pandas进行均值填充的实现,感兴趣的... 目录什么是均值填充?为什么选择均值填充?均值填充的步骤实际代码示例总结在数据分析和处理过程中,缺失数

如何使用 Python 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

C 语言中enum枚举的定义和使用小结

《C语言中enum枚举的定义和使用小结》在C语言里,enum(枚举)是一种用户自定义的数据类型,它能够让你创建一组具名的整数常量,下面我会从定义、使用、特性等方面详细介绍enum,感兴趣的朋友一起看... 目录1、引言2、基本定义3、定义枚举变量4、自定义枚举常量的值5、枚举与switch语句结合使用6、枚

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

使用Python实现图像LBP特征提取的操作方法

《使用Python实现图像LBP特征提取的操作方法》LBP特征叫做局部二值模式,常用于纹理特征提取,并在纹理分类中具有较强的区分能力,本文给大家介绍了如何使用Python实现图像LBP特征提取的操作方... 目录一、LBP特征介绍二、LBP特征描述三、一些改进版本的LBP1.圆形LBP算子2.旋转不变的LB

Maven的使用和配置国内源的保姆级教程

《Maven的使用和配置国内源的保姆级教程》Maven是⼀个项目管理工具,基于POM(ProjectObjectModel,项目对象模型)的概念,Maven可以通过一小段描述信息来管理项目的构建,报告... 目录1. 什么是Maven?2.创建⼀个Maven项目3.Maven 核心功能4.使用Maven H