Java Redis 连接池 Jedis 工具类

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class MyJedisPool {

    private final static Logger logger = LoggerFactory.getLogger(MyJedisPool.class);

    private static JedisPool readPool = null;
    private static JedisPool writePool = null;

    //静态代码初始化池配置
    static {
        try{
            Properties props = new Properties();
            InputStream in = MyJedisPool.class.getResourceAsStream("/redis.properties");
            props.load(in);

            //创建jedis池配置实例
            JedisPoolConfig config = new JedisPoolConfig();

            //设置池配置项值
            config.setMaxTotal(Integer.valueOf(props.getProperty("jedis.pool.maxActive")));
            config.setMaxIdle(Integer.valueOf(props.getProperty("jedis.pool.maxIdle")));
            config.setMaxWaitMillis(Long.valueOf(props.getProperty("jedis.pool.maxWait")));
            config.setTestOnBorrow(Boolean.valueOf(props.getProperty("jedis.pool.testOnBorrow")));
            config.setTestOnReturn(Boolean.valueOf(props.getProperty("jedis.pool.testOnReturn")));

            //根据配置实例化jedis池
            readPool = new JedisPool(config, props.getProperty("redisReadURL"), Integer.valueOf(props.getProperty("redisReadPort")));
            writePool = new JedisPool(config, props.getProperty("redisWriteURL"), Integer.valueOf(props.getProperty("redisWritePort")));

        }catch (IOException e) {
            logger.info("redis连接池异常",e);
        }
    }

    /**获得jedis对象*/
    public static Jedis getReadJedisObject(){
        return readPool.getResource();
    }
    /**获得jedis对象*/
    public static Jedis getWriteJedisObject(){
        return writePool.getResource();
    }

    /**归还jedis对象*/
    public static void returnJedisOjbect(Jedis jedis){
        if (jedis != null) {
            jedis.close();
        }
    }

}

  

import redis.clients.jedis.Jedis;

import java.util.Set;

public class RedisUtils {

    /**
     * 获取hash表中所有key
     * @param name
     * @return
     */
    public static Set<String> getHashAllKey(String name){
        Jedis jedis = null;
        try {
            jedis = MyJedisPool.getReadJedisObject();
            return jedis.hkeys(name);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyJedisPool.returnJedisOjbect(jedis);
        }
        return null;
    }

    /**
     * 从redis hash表中获取
     * @param hashName
     * @param key
     * @return
     */
    public static String getHashKV(String hashName,String key){
        Jedis jedis = null;
        try {
            jedis = MyJedisPool.getReadJedisObject();
            return jedis.hget(hashName, key);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyJedisPool.returnJedisOjbect(jedis);
        }
        return null;
    }

    /**
     * 删除hash表的键值对
     * @param hashName
     * @param key
     */
    public static Long delHashKV(String hashName,String key){
        Jedis jedis = null;
        try {
            jedis = MyJedisPool.getWriteJedisObject();
            return jedis.hdel(hashName,key);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyJedisPool.returnJedisOjbect(jedis);
        }
        return null;
    }

    /**
     * 存放hash表键值对
     * @param hashName
     * @param key
     * @param value
     */
    public static Long setHashKV(String hashName,String key,String value){
        Jedis jedis = null;
        try {
            jedis = MyJedisPool.getWriteJedisObject();
            return jedis.hset(hashName,key,value);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyJedisPool.returnJedisOjbect(jedis);
        }
        return null;
    }

    /**
     * 删除键值对
     * @param k
     * @return
     */
    public static Long delKV(String k){
        Jedis jedis = null;
        try {
            jedis = MyJedisPool.getWriteJedisObject();
            return jedis.del(k);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyJedisPool.returnJedisOjbect(jedis);
        }
        return null;
    }

    /**
     * 放键值对
     * 永久
     * @param k
     * @param v
     */
    public static String setKV(String k, String v)
    {
        Jedis jedis = null;
        try {
            jedis = MyJedisPool.getWriteJedisObject();
            return jedis.set(k, v);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyJedisPool.returnJedisOjbect(jedis);
        }
        return null;
    }

    /**
     * 放键值对
     *
     * @param k
     * @param v
     */
    public static String setKV(String k,int second, String v)
    {
        Jedis jedis = null;
        try {
            jedis = MyJedisPool.getWriteJedisObject();
            return jedis.setex(k,second, v);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyJedisPool.returnJedisOjbect(jedis);
        }
        return null;
    }

    /**
     * 根据key取value
     *
     * @param k
     * @return
     */
    public static String getKV(String k)
    {
        Jedis jedis = null;
        try {
            jedis = MyJedisPool.getReadJedisObject();
            return jedis.get(k);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyJedisPool.returnJedisOjbect(jedis);
        }
        return null;
    }

}

  

原文地址:https://www.cnblogs.com/yanqin/p/8342836.html

时间: 2024-12-30 00:13:43

Java Redis 连接池 Jedis 工具类的相关文章

JDBC连接池与工具类

1.连接池概述 用池来管理Connection,这样可以重复使用Connection,有了池,所以我们就不用自己来创建Connection,而是通过池来获取Connection对象,当使用完Connection后,调用Connection的close()方法也不会真的关闭Connection,而是把Connection归还给池,池就可以再利用这个Connection对象了 2.C3P0 public class Demo1 { @Test public void test() throws Ex

使用C3P0连接池的工具类

package util; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DataSourceUtils { private static

java中使用jedis操作redis(连接池方式)

1 package com.test; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.List; 6 import java.util.Map; 7 8 import org.junit.Before; 9 import org.junit.Test; 10 11 import redis.clients.jedis.Jedis; 12 13 public class TestRedis

Redis Java客户端jedis工具类以及Redis实现的跨jvm的锁

Redis Java客户端jedis工具类以及Redis实现的跨jvm的锁 最近项目中使用redis,学习了一下,client端使用jedis-2.1.0 首先是一个redis实现的跨jvm的lock, 接着是一个简单封装的工具类,也对pipeline处理进行了几个常用的封装 然后是对应Spring的相关配置 Java代码   public class RedisLock { /** 加锁标志 */ public static final String LOCKED = "TRUE";

Java的Redis连接池代码性能不错

其实这个是引用自网友http://blog.csdn.net/tuposky/article/details/45340183,有2个版本,差别就是ReentrantLock和synchronized.另外原作者使用了断言,我觉得这个还是不用为好. ReentrantLock版 import java.util.concurrent.locks.ReentrantLock; import org.apache.commons.lang.StringUtils; import org.apache

redis 连接池 - 转载

所需jar:jedis-2.1.0.jar和commons-pool-1.5.4.jar Jedis操作步骤如下:1->获取Jedis实例需要从JedisPool中获取:2->用完Jedis实例需要返还给JedisPool:3->如果Jedis在使用过程中出错,则也需要还给JedisPool: package com.ljq.utils; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; imp

三:Redis连接池、JedisPool详解、Redisi分布式

单机模式: 1 package com.ljq.utils; 2 3 import redis.clients.jedis.Jedis; 4 import redis.clients.jedis.JedisPool; 5 import redis.clients.jedis.JedisPoolConfig; 6 7 /** 8 * Redis操作接口 9 * 10 * @author NiceCui 11 * @version 1.0 2017-6-14 上午08:54:14 12 */ 13

redis连接池 jedis-2.9.0.jar+commons-pool2-2.4.2.jar

java使用Redis连接池  jar包为 jedis-2.9.0.jar+commons-pool2-2.4.2.jar 1 package com.test; 2 3 import redis.clients.jedis.Jedis; 4 import redis.clients.jedis.JedisPool; 5 import redis.clients.jedis.JedisPoolConfig; 6 7 public class RedisUtil { 8 //Redis服务器IP

java实现发邮件的工具类,方便 好用(需要架包的Send Email To me)

原文:java实现发邮件的工具类,方便 好用(需要架包的Send Email To me) 源代码下载地址:http://www.zuidaima.com/share/1550463394794496.htm package com.zuidaima.util; import java.util.Properties; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.Multipart; impor