redis在项目中的使用

本文介绍了如何在项目中运用jedis结合commons-pool库创建Redis连接池,通过配置redis.properties文件来设定相关连接参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jedis使用commoms-pool池化实现redis,项目需导入commons-pool.jar

1.编写redis.properties文件,配置信息如下:

    #最大分配的对象数  
    redis.pool.maxActive=1024  
    #最大能够保持idel状态的对象数  
    redis.pool.maxIdle=200  
    #当池内没有返回对象时,最大等待时间  
    redis.pool.maxWait=1000  
    #当调用borrow Object方法时,是否进行有效性检查  
    redis.pool.testOnBorrow=true  
    #当调用return Object方法时,是否进行有效性检查  
    redis.pool.testOnReturn=true  
    #IP  
    redis.ip=10.11.20.140  
    #Port  
    redis.port=6379
   redis.pwd=
2.编写RedisConfig.java文件

public class RedisConfig {

	public static JedisPool examRedisPool;

	/* 绑定redis配置文件,并生成examRedisPool */
	static {
		examRedisPool = createPool("config/redis/redis");
		if(examRedisPool==null){
		throw new RuntimeException("无法连接到redis");
		}
	}
	
	static JedisPool createPool(String redisProName){
		ResourceBundle bundle = ResourceBundle.getBundle(redisProName);
		if (bundle == null) {
			throw new RuntimeException("redis配置文件找不到");
		}
		JedisPoolConfig config = new JedisPoolConfig();
		JedisPool pool = null;
		config.setMaxActive(Integer.valueOf(bundle.getString("redis.pool.maxActive")));
		config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));
		config.setMaxWait(Integer.valueOf(bundle.getString("redis.pool.maxWait")));
		config.setTestOnBorrow(Boolean.valueOf("redis.pool.testOnBorrow"));
		config.setTestOnReturn(Boolean.valueOf("redis.pool.testOnReturn"));
		String pwd = bundle.getString("redis.pool.pwd");
		if (pwd.isEmpty()) {
			pool = new JedisPool(config, bundle.getString("redis.pool.ip"),
					Integer.valueOf(bundle.getString("redis.pool.port")));
			return pool;
		} else {
			pool = new JedisPool(config, bundle.getString("redis.pool.ip"),
					Integer.valueOf(bundle.getString("redis.pool.port")), 2000, pwd);
			return pool;
		}
	}
}
3.编写JedisUtils.java类,操作redis中的数据

public class JedisUtils {

	/**
	 * 从Redis 中获取对象
	 * 
	 * @param <T>
	 * 
	 * @param key
	 *            键值
	 * @return
	 * @return 在Redis存储的键值为key的值
	 */
	protected static <T> T getObject(JedisPool pool, String key) {
		Jedis cliet = null;
		try {
			cliet = pool.getResource();
			byte[] result = cliet.get(key.getBytes());
			if (result != null && result.length > 0) {
			  T	t = (T) SerializeUtil.unserialize(result);
				return t;
			} else {
				return null;
			}

		} catch (Exception e) {
			e.printStackTrace();
			pool.returnBrokenResource(cliet);
		} finally {
			pool.returnResource(cliet);
		}
		return null;
	}

	/**
	 * 获取键值为key的字符串
	 * 
	 * @param key
	 *            键值
	 * @return 键值为key的字符串
	 */
	protected static String getStr(JedisPool pool, String key) {
		Jedis cliet = null;
		try {
			cliet = pool.getResource();
			return cliet.get(key);

		} catch (Exception e) {
			e.printStackTrace();
			pool.returnBrokenResource(cliet);
		} finally {
			pool.returnResource(cliet);
		}
		return null;
	}

	/**
	 * 存储对象值
	 * 
	 * @param key
	 *            键值
	 * @param value
	 *            值
	 * @param expire
	 *            失效时间(以秒为单位)
	 */
	protected static void setObject(JedisPool pool, String key, Object value, int expire) {
		if (value == null)
			return;
		Jedis cliet = null;
		try {
			cliet = pool.getResource();
			if (expire <= 0) {
				cliet.set(key.getBytes(), SerializeUtil.serialize(value));
			} else {
				cliet.setex(key.getBytes(), expire, SerializeUtil.serialize(value));
			}
		} catch (Exception e) {
			e.printStackTrace();
			pool.returnBrokenResource(cliet);
		} finally {
			pool.returnResource(cliet);
		}
	}
	
	/**
	 * 删除对象值
	 * 
	 * @param key
	 *            键值
	 * @param value
	 *            值
	 * @param expire
	 *            失效时间(以秒为单位)
	 */
	protected static void removeObject(JedisPool pool, String key) {
		if (key == null)
			return;
		Jedis cliet = null;
		try {
			cliet = pool.getResource();
			cliet.del(key.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
			pool.returnBrokenResource(cliet);
		} finally {
			pool.returnResource(cliet);
		}
	}

	/**
	 * 存储字符串信息
	 * 
	 * @param key
	 *            键值
	 * @param value
	 *            值
	 * @param expire
	 *            失效时间(以秒为单位)
	 */
	protected static void setStr(JedisPool pool, String key, String value, int expire) {
		Jedis cliet = null;
		try {
			cliet = pool.getResource();
			if (expire <= 0) {
				cliet.set(key, value);
			} else {
				cliet.setex(key, expire, value);
			}

		} catch (Exception e) {
			e.printStackTrace();
			pool.returnBrokenResource(cliet);
		} finally {
			pool.returnResource(cliet);
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值