Redis(二)------采坑之路
程序员文章站
2022-05-19 10:10:13
...
前言
首先我用的是JAVA语言,如果您也遇到了与我类似的问题,即使用JedisPool的时候出现Could not get a resource from the pool类似的问题,请看下去!该实例保证你不会出现该错误!
正文
依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
<type>jar</type>
</dependency>
配置
spring-config.xml
<!-- 配置redis数据库连接池 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="minIdle" value="${redis.pool.minIdle}" />
<property name="maxWaitMillis" value="${redis.pool.maxWait}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
<property name="testOnReturn" value="${redis.pool.testOnReturn}" />
<property name="testWhileIdle" value="${redis.pool.testWhileIdle}" />
<property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}" />
</bean>
<!-- 配置redis共享连接池,业务层主要通过该bean访问redis服务器 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1" value="${redis.host}" />
<constructor-arg index="2" value="${redis.port}" />
<constructor-arg index="3" value="${redis.pool.timeout}" />
</bean>
<bean id="redisClient" class="com.lk.sydn.mahout.cache.RedisUtil">
<property name="jedisPool" ref="jedisPool"/>
</bean>
redis.properties
#最大分配的对象数(如果超过最大idel状态的对象数时,池中会继续增加知道达到该最大值)
redis.pool.maxTotal=1000
#最大能够保持idel状态的对象数
redis.pool.maxIdle=100
#最小空闲的对象数。2.5.1以上版本有效
redis.pool.minIdle=50
#当池内没有返回对象时,最大等待时间
redis.pool.maxWait=100000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=true
#当调用return Object方法时,是否进行有效性检查
redis.pool.testOnReturn=true
#向调用者输出“链接”对象时,是否检测它的空闲超时
redis.pool.testWhileIdle=true
#“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1
redis.pool.timeBetweenEvictionRunsMillis=30000
#获取池中对象时的超时时间
redis.pool.timeout=100000
代码
Java工具类
package com.xxx.yyy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* Created by Silence on 2018/4/13.
*/
public class RedisUtil {
private Logger logger = LoggerFactory.getLogger(RedisUtil.class);
@Autowired
private JedisPool jedisPool;
public Jedis getRedisClient() {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
} catch (Exception ex) {
logger.error("RedisUtil Get Jedis Error!", ex);
}
return jedis;
}
/**
* 设置单个值
*
* @param key
* @param value
*/
public void set(String key, String value) {
Jedis jedis = getRedisClient();
if (jedis == null) {
logger.error("RedisUtil No Available Jedis!");
return;
}
try {
jedis.set(key, value);
} catch (Exception ex) {
logger.error("RedisUtil Set Error!", ex);
} finally {
//将用完的实例还回到池中,好借好还,再借不难
jedis.close();
}
}
/**
* 设置单个值(附带有效)
* @param key
* @param value
* @param seconds
*/
public void setWithExpire(String key, String value, int seconds) {
Jedis jedis = getRedisClient();
if (jedis == null) {
logger.error("RedisUtil No Available Jedis!");
return;
}
try {
jedis.set(key, value);
jedis.expire(key, seconds);
} catch (Exception ex) {
logger.error("RedisUtil SetWithExpire Error!", ex);
} finally {
//将用完的实例还回到池中,好借好还,再借不难
jedis.close();
}
}
/**
* 获取单个值
* @param key
* @return
*/
public String get(String key) {
String result = null;
Jedis jedis = getRedisClient();
if (jedis == null) {
logger.error("RedisUtil No Available Jedis!");
return null;
}
try {
result = jedis.get(key);
} catch (Exception ex) {
logger.error("RedisUtil Get Error!", ex);
} finally {
//将用完的实例还回到池中,好借好还,再借不难
jedis.close();
}
return result;
}
public void setJedisPool(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
}
上一篇: Python环境采坑小记