Redis工具类

import org.apache.http.client.ClientProtocolException;
import redis.clients.jedis.*;
import redis.clients.jedis.exceptions.JedisConnectionException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.DriverManager;
import java.util.*;

public class JedisUtils {
private static JedisPool pool = null;
private static Properties pro = null;

private static void initializePool()  {
    //加载配置文件
    InputStream in = JedisUtils.class.getClassLoader().getResourceAsStream("redis.properties");
    pro = new Properties();
    try {
        pro.load(in);
    } catch (IOException e) {
        e.printStackTrace();
    }
    //获得池子对象(redis数据库)
   JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxIdle(Integer.parseInt(pro.get("redis.maxIdle").toString()));//最大闲置个数
    poolConfig.setMinIdle(Integer.parseInt(pro.get("redis.minIdle").toString()));//最小闲置个数
    poolConfig.setMaxTotal(Integer.parseInt(pro.get("redis.maxTotal").toString()));//最大连接数
    //初始化连接池
    pool = new JedisPool(poolConfig, pro.getProperty("redis.host"), Integer.parseInt(pro.get("redis.port").toString()));
}

//保证项目中有且仅有一个连接池
private static synchronized void poolInit() {
    if (null == pool) {
        initializePool();

    }
}
public static Jedis getJedis() {
    if (null == pool) {
        poolInit();
    }

    int timeoutCount = 0;
    while (true) {
        try {
            if (null != pool) {
                return pool.getResource();
            }
        } catch (Exception e) {
            if (e instanceof JedisConnectionException) {
                timeoutCount++;
                System.out.println("getJedis timeoutCount="+ timeoutCount);
                if (timeoutCount > 3) {
                    break;
                }
            } else {
                System.out.println("GetJedis error,"+e);
                break;
            }
        }
        break;
    }
    return null;
}


public static String getKey() {
    String keys = pro.getProperty("redis.keys");
    return keys;
}

public static Object getObject(String key) {
    try (Jedis jedis = pool.getResource()) {
        //Jedis jedis = new Jedis("127.0.0.1")
        byte[] bytes = jedis.get(key.getBytes());
        if (bytes != null) {
            return SerializeUtil.unserialize(bytes);
        }
    } catch (Exception e) {
        System.out.println("获取Redis键值getObject方法异常:key=" + key + " cause=" + e.getMessage());
    }
    return null;
}

public static String setObject(String key, Object value) {
    try (Jedis jedis = pool.getResource()) {
        //Jedis jedis = new Jedis("127.0.0.1")
        //ShardedJedis jedis = pool.getResource()
        return jedis.set(key.getBytes(), SerializeUtil.serialize(value));
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("设置Redis键值setObject方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
    }
    return null;
}

public static String hsetObject(String keys, String key, Object value) {
    try (Jedis jedis = pool.getResource()) {
        //Jedis jedis = new Jedis("127.0.0.1")
        //ShardedJedis jedis = pool.getResource()
        return jedis.set(keys.getBytes(), key.getBytes(), SerializeUtil.serialize(value));
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("设置Redis键值setObject方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
    }
    return null;
}

public static boolean hset(String keys, String field, String value) {
    if (keys.isEmpty() || field.isEmpty()) {
        return false;
    }
    Jedis jedis = pool.getResource();
    jedis.get("redis.keys");
    Long statusCode = jedis.hset(keys, field, value);
    jedis.close();
    if (statusCode > -1) {
        return true;
    }
    return false;
}

public static String hget(String key, String field){
    if(key.isEmpty()|| field.isEmpty()){
        return null;
    }
    Jedis jedis = pool.getResource();
    String value = jedis.hget(key, field);
    jedis.close();
    return value;
}


public static void closePool() throws Exception {
    try {
        pool.close();
    } catch (Exception e) {
        throw new Exception("释放Jedis资源异常:" + e.getMessage());
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值