java项目直接使用Jedis操作Redis缓存示例-java项目使用Jedis

java项目直接使用Jedis操作Redis缓存示例-java项目使用Jedis

比如可在非springboot的java项目中使用Jedis操作Redis缓存
使用连接池管理连接

  • 直接上代码,示例代码如下:
依赖引入
		<!--jedis-->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>3.8.0</version>
		</dependency>
连接池使用示例
package com.minpay.posp.manager.keytool.engine;

import java.time.Duration;
import org.apache.logging.log4j.Logger;
import com.minpay.posp.manager.keytool.conf.KeytoolConfigs;
import com.minpay.posp.manager.keytool.log.KeytoolLogManager;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Redis客户端提供者
 */
public class RedisProvider {

	private static final Logger logger = KeytoolLogManager.getLogger();

	/** 连接池 */
	private static JedisPool Pool = null;

	static {
		initJedis();
	}

	private static void initJedis() {

		try {
		//这里的配置改为自己的配置
			String host = KeytoolConfigs.redisHost.configValue();
			Integer port = Integer.parseInt(KeytoolConfigs.redisPort.configValue());
			Integer database = Integer.parseInt(KeytoolConfigs.redisDatabase.configValue());
			String password = KeytoolConfigs.redisPassword.configValue();
			Integer maxActive = Integer.parseInt(KeytoolConfigs.redisMaxActive.configValue("32"));
			Integer maxIdle = Integer.parseInt(KeytoolConfigs.redisMaxIdle.configValue("8"));
			Integer minIdle = Integer.parseInt(KeytoolConfigs.redisMinIdle.configValue("1"));

			logger.info("初始化Redis配置:host={},port={},database={},password={},maxActive={},maxIdle={},minIdle={}", host,
					port, database, password, maxActive, maxIdle, minIdle);

			// 接池配置
			JedisPoolConfig jedisPool = new JedisPoolConfig();
			// 最大连接数
			jedisPool.setMaxTotal(maxActive);
			// 最大活动数
			jedisPool.setMaxIdle(maxIdle);
			/** 最小活动数 */
			jedisPool.setMinIdle(minIdle);
			jedisPool.setMaxWait(Duration.ofMillis(2000));

			Pool = new JedisPool(jedisPool, host, port, 2000, password, database);
		} catch (Exception err) {
			throw new RuntimeException("keytool 初始化Redis失败");
		}

	}

	/**
	 * 获取 Jedis连接客户端
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Jedis getJedis() throws Exception {
		if (Pool == null) {
			throw new Exception("Redis未配置");
		}
		return Pool.getResource();
	}
}

操作工具类示例
package com.minpay.posp.manager.keytool.utils;

import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSON;
import com.minpay.posp.manager.keytool.engine.RedisProvider;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.SetParams;

/**
 * Jedis工具类
 */
public class JedisUtil {

	/**
	 * 获取缓存
	 * 
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String get(String key) throws Exception {
		Jedis jedis = RedisProvider.getJedis();
		String result = null;
		try {
			result = jedis.get(key);
		} catch (Exception e) {
			throw e;
		} finally {
			jedis.close();
		}
		return result;
	}

	/**
	 * 获取缓存对象
	 * 
	 * @param <T>
	 * @param key
	 * @param clazz
	 * @return
	 * @throws Exception
	 */
	public static <T> T getObject(String key, Class<T> clazz) throws Exception {
		String result = get(key);
		return StringUtils.isEmpty(result) ? null : JSON.parseObject(result, clazz);
	}

	/**
	 * 设置缓存
	 * 
	 * @param key
	 * @param value
	 * @throws Exception
	 */
	public static void set(String key, String value) throws Exception {
		Jedis jedis = RedisProvider.getJedis();
		try {
			jedis.set(key, value);
		} catch (Exception e) {
			throw e;
		} finally {
			jedis.close();
		}
	}

	/**
	 * 设置缓存
	 * 
	 * @param key
	 * @param value
	 * @param expireTime 过期时间,单位毫秒
	 * @throws Exception
	 */
	public static void set(String key, String value, int expireTime) throws Exception {
		Jedis jedis = RedisProvider.getJedis();
		try {
			SetParams setParams = new SetParams();
			setParams.px(expireTime); // 设置过期时间
			jedis.set(key, value, setParams);
		} catch (Exception e) {
			throw e;
		} finally {
			jedis.close();
		}
	}

	/**
	 * 清除指定key的缓存
	 * 
	 * @param keys
	 * @throws Exception
	 */
	public static void del(String... keys) throws Exception {
		Jedis jedis = RedisProvider.getJedis();
		try {
			jedis.del(keys);
		} catch (Exception e) {
			throw e;
		} finally {
			jedis.close();
		}
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北海山人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值