JedisUtil


import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public final class JedisUtil {
private static final Logger LOGGER = Logger.getLogger(JedisUtil.class);
private static final String FILE_NAME = "redis.properties";
private static int DEFAULT_DB_INDEX = 0;

private static JedisPool jedisPool = null;

private JedisUtil() {
// private constructor
}

private static void initialPool() {
final Properties configurations = new Properties();
String filePath;
String address = "";
int port = 6379;

try {
filePath = JedisUtil.class.getResource("/").getPath() + "/" + FILE_NAME;
File file = new File(filePath);

configurations.load(new FileInputStream(file));
address = configurations.getProperty("address");
port = Integer.valueOf(configurations.getProperty("port"));

String strDbIndex = configurations.getProperty("db_index");
if (strDbIndex != null) {
DEFAULT_DB_INDEX = Integer.valueOf(strDbIndex);
}
LOGGER.info("Redis server info: " + address + ":" + port);

final JedisPoolConfig config = new JedisPoolConfig();
String strMaxActive = configurations.getProperty("pool_maxactive");
if (strMaxActive != null) {
config.setMaxActive(Integer.valueOf(strMaxActive));
}
String strMaxIdle = configurations.getProperty("pool_maxidle");
if (strMaxIdle != null) {
config.setMaxIdle(Integer.valueOf(strMaxIdle));
}
String strMaxWait = configurations.getProperty("pool_maxwait");
if (strMaxWait != null) {
config.setMaxWait(Integer.valueOf(strMaxWait));
}
config.setTestOnBorrow(false);

String strTimeout = configurations.getProperty("pool_connection_timeout");

int timeout = 2000;
if (strTimeout != null) {
timeout = Integer.valueOf(strTimeout);
}
jedisPool = new JedisPool(config, address, port, timeout);
} catch (Exception e) {
e.printStackTrace();
} finally {

}

}

public synchronized static Jedis getJedisInstance() {
if (jedisPool == null) {
initialPool();
}
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
resource.select(DEFAULT_DB_INDEX);
return resource;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

public synchronized static Jedis getJedisInstance(final int dbIndex) {
if (jedisPool == null) {
initialPool();
}
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
resource.select(dbIndex);
return resource;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

public static void returnResource(final Jedis jedis) {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
}

}
JedisUtil是一个Java Redis缓存工具类,它封装了Jedis客户端的基本操作,使得使用Redis缓存更加简单方便。 以下是JedisUtil的示例代码: ``` import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class JedisUtil { private static JedisPool jedisPool; static { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(1000); jedisPoolConfig.setMaxIdle(100); jedisPool = new JedisPool(jedisPoolConfig, "localhost", 6379); } public static void set(String key, String value) { try (Jedis jedis = jedisPool.getResource()) { jedis.set(key, value); } } public static String get(String key) { try (Jedis jedis = jedisPool.getResource()) { return jedis.get(key); } } public static void del(String key) { try (Jedis jedis = jedisPool.getResource()) { jedis.del(key); } } public static void expire(String key, int seconds) { try (Jedis jedis = jedisPool.getResource()) { jedis.expire(key, seconds); } } public static boolean exists(String key) { try (Jedis jedis = jedisPool.getResource()) { return jedis.exists(key); } } } ``` 在上面的代码中,我们使用了JedisPool来管理Jedis连接,它的作用是维护一定数量的Jedis连接,以便在需要时从池中获取连接,减少了创建和关闭连接的开销。 在使用JedisUtil时,我们只需要调用set、get、del、expire和exists等方法,就可以完成对Redis缓存的操作。 例如,要将一个键值对("name", "Tom")存入Redis中,可以使用以下代码: ``` JedisUtil.set("name", "Tom"); ``` 要获取键为"name"的值,可以使用以下代码: ``` String name = JedisUtil.get("name"); ``` 同时,JedisUtil还提供了删除、设置过期时间和判断键是否存在等方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值