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();
}
}
}