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

相关文章

使用Dify访问mysql数据库详细代码示例

《使用Dify访问mysql数据库详细代码示例》:本文主要介绍使用Dify访问mysql数据库的相关资料,并详细讲解了如何在本地搭建数据库访问服务,使用ngrok暴露到公网,并创建知识库、数据库访... 1、在本地搭建数据库访问的服务,并使用ngrok暴露到公网。#sql_tools.pyfrom

使用mvn deploy命令上传jar包的实现

《使用mvndeploy命令上传jar包的实现》本文介绍了使用mvndeploy:deploy-file命令将本地仓库中的JAR包重新发布到Maven私服,文中通过示例代码介绍的非常详细,对大家的学... 目录一、背景二、环境三、配置nexus上传账号四、执行deploy命令上传包1. 首先需要把本地仓中要

Spring Cloud之注册中心Nacos的使用详解

《SpringCloud之注册中心Nacos的使用详解》本文介绍SpringCloudAlibaba中的Nacos组件,对比了Nacos与Eureka的区别,展示了如何在项目中引入SpringClo... 目录Naacos服务注册/服务发现引⼊Spring Cloud Alibaba依赖引入Naco编程s依

Java springBoot初步使用websocket的代码示例

《JavaspringBoot初步使用websocket的代码示例》:本文主要介绍JavaspringBoot初步使用websocket的相关资料,WebSocket是一种实现实时双向通信的协... 目录一、什么是websocket二、依赖坐标地址1.springBoot父级依赖2.springBoot依赖

Java使用Mail构建邮件功能的完整指南

《Java使用Mail构建邮件功能的完整指南》JavaMailAPI是一个功能强大的工具,它可以帮助开发者轻松实现邮件的发送与接收功能,本文将介绍如何使用JavaMail发送和接收邮件,希望对大家有所... 目录1、简述2、主要特点3、发送样例3.1 发送纯文本邮件3.2 发送 html 邮件3.3 发送带

使用DeepSeek搭建个人知识库(在笔记本电脑上)

《使用DeepSeek搭建个人知识库(在笔记本电脑上)》本文介绍了如何在笔记本电脑上使用DeepSeek和开源工具搭建个人知识库,通过安装DeepSeek和RAGFlow,并使用CherryStudi... 目录部署环境软件清单安装DeepSeek安装Cherry Studio安装RAGFlow设置知识库总

Python FastAPI入门安装使用

《PythonFastAPI入门安装使用》FastAPI是一个现代、快速的PythonWeb框架,用于构建API,它基于Python3.6+的类型提示特性,使得代码更加简洁且易于绶护,这篇文章主要介... 目录第一节:FastAPI入门一、FastAPI框架介绍什么是ASGI服务(WSGI)二、FastAP

Spring-AOP-ProceedingJoinPoint的使用详解

《Spring-AOP-ProceedingJoinPoint的使用详解》:本文主要介绍Spring-AOP-ProceedingJoinPoint的使用方式,具有很好的参考价值,希望对大家有所帮... 目录ProceedingJoinPoijsnt简介获取环绕通知方法的相关信息1.proceed()2.g

Maven pom.xml文件中build,plugin标签的使用小结

《Mavenpom.xml文件中build,plugin标签的使用小结》本文主要介绍了Mavenpom.xml文件中build,plugin标签的使用小结,文中通过示例代码介绍的非常详细,对大家的学... 目录<build> 标签Plugins插件<build> 标签<build> 标签是 pom.XML

JAVA虚拟机中 -D, -X, -XX ,-server参数使用

《JAVA虚拟机中-D,-X,-XX,-server参数使用》本文主要介绍了JAVA虚拟机中-D,-X,-XX,-server参数使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录一、-D参数二、-X参数三、-XX参数总结:在Java开发过程中,对Java虚拟机(JVM)的启动参数进