这里简易封装了一下对redis缓存对象的管理,这个连接池只针对单独服务器,如果要使用集群的话这个就不适用了,需要使用ShardedJedisPool
List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("localhost",6379), new JedisShardInfo("localhost",6380)); ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards);
-------------------------------------------------------------------------------------------
package com.ctweb.util.redis;
import javax.annotation.PostConstruct;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.ctweb.util.ProcessCodeEnum;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
*
* @author Dylan
* @date 2015年12月10日
*/
@Component
public class RedisClinet {
private static Logger log = LogManager.getLogger(RedisClinet.class);
@Value("${redis.host}")
private String host;
@Value("${redis.port}")
private String port;
@Value("${redis.auth}")
private String auth;
@Value("${redis.maxTotal}")
private int maxTotal;
@Value("${redis.maxIdle}")
private int maxIdle;
@Value("${redis.maxWaitMillis}")
private long maxWaitMillis;
private JedisPool jedisPool;
/*public RedisClinet(){
initialPool();
}*/
/**
* 初始化非切片池
*/
@PostConstruct
private void initialPool()
{
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
//连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
//config.setBlockWhenExhausted(true);
//设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
//config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
//是否启用pool的jmx管理功能, 默认true
//config.setJmxEnabled(true);
//config.setJmxNamePrefix("pool");
//是否启用后进先出, 默认true
//config.setLifo(true);
//最大空闲连接数, 默认8个
config.setMaxIdle(maxIdle <= 0 ? 10 : maxIdle);
//最大连接数, 默认8个
config.setMaxTotal(maxTotal <= 0 ? 50 : maxTotal);
//获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
config.setMaxWaitMillis(maxWaitMillis <= 0 ? 100000 : maxWaitMillis);
//逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
//config.setMinEvictableIdleTimeMillis(1800000);
//最小空闲连接数, 默认0
//config.setMinIdle(0);
//每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
//config.setNumTestsPerEvictionRun(3);
//对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断 (默认逐出策略)
//config.setSoftMinEvictableIdleTimeMillis(1800000);
//在获取连接的时候检查有效性, 默认false
//config.setTestOnBorrow(false);
//在空闲时检查有效性, 默认false
//config.setTestWhileIdle(false);
//逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
//config.setTimeBetweenEvictionRunsMillis(-1);
jedisPool = new JedisPool(config, host, Integer.parseInt(null == port?"0":port));
log.debug("init jedis connection pool success -> host:"+host+",port:"+port);
}
/**
* 连接返回连接池
* @param jedis
* @param isBroken
*/
private void release(Jedis jedis, boolean isBroken) {
if (jedis != null) {
if (isBroken) {
jedisPool.returnBrokenResource(jedis);
} else {
jedisPool.returnResource(jedis);
}
log.debug("release jedis connection to pool ,active num:"+jedisPool.getNumActive()+",idel num:"+jedisPool.getNumIdle()+",wait num:"+jedisPool.getNumWaiters());
}
}
/**
* 获取jedis client
* @return
*/
private Jedis getJedis(){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.auth(auth);
log.debug("get jedis client from connection pool success");
} catch (Exception e) {
throw ProcessCodeEnum.PROCESS_ERR.buildProcessException("get jedis client from connection pool fail or jedis connection pool init have error,check the host/port/auth!",e);
}
return jedis;
}
/**
* 对象置入缓存
* @param object
* @param key
* @param args 失效时间(单位:秒) 小于等于0认为永不过期
* @return
*/
public void set(Object object,String key,int... args) {
boolean isBroken = false;
if(null == object || StringUtils.isBlank(key))
throw ProcessCodeEnum.PROCESS_ERR.buildProcessException("jedis set object or key can not be null");
Jedis jedis = null;
int validTime = ArrayUtils.isNotEmpty(args) ? args[0] : 0;
try {
jedis = getJedis();
jedis.set(key.getBytes(), SerializeUtil.serialize(object));
if(validTime > 0)
jedis.expire(key.getBytes(), validTime);
} catch (Exception e) {
isBroken = true;
throw ProcessCodeEnum.PROCESS_ERR.buildProcessException("jedis set object have error",e);
}finally{
release(jedis, isBroken);
}
}
/**
* 从缓存中取值
* @param key
* @return
*/
public Object get(String key){
boolean isBroken = false;
if(StringUtils.isBlank(key))
throw ProcessCodeEnum.PROCESS_ERR.buildProcessException("jedis get key can not be null");
Jedis jedis = null;
Object result = null;
try {
jedis = getJedis();
byte[] value = jedis.get(key.getBytes());
result = SerializeUtil.unSerialize(value);
} catch (Exception e) {
isBroken = true;
throw ProcessCodeEnum.PROCESS_ERR.buildProcessException("jedis get object have error",e);
}finally{
release(jedis, isBroken);
}
return result;
}
/**
* 从缓存中删除
* @param key
* @return
*/
public boolean del(String key){
boolean isBroken = false;
if(StringUtils.isBlank(key))
throw ProcessCodeEnum.PROCESS_ERR.buildProcessException("jedis del key can not be null");
Jedis jedis = null;
boolean isSuc = false;
try {
jedis = getJedis();
isSuc = jedis.del(key.getBytes())>0;
} catch (Exception e) {
isBroken = true;
throw ProcessCodeEnum.PROCESS_ERR.buildProcessException("jedis del object have error",e);
}finally{
release(jedis, isBroken);
}
return isSuc;
}
}
相关推荐
redis页面缓存html使用redis实现页面缓存.docx
redis缓存 redis缓存
Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。从2010年3月15日起,Redis的开发工作由VMware主持。从2013年5月开始,Redis的开发由Pivotal...
commons-pool2-2.3.jar,jedis-2.8.0.jar,spring-data-redis-1.6.0.RELEASE.jar,spring-session-1.1.1.RELEASE.jar,Spring-data-redis(Version 1.6.0.RC1)中文版.pdf
SpringBoot项目 MybatisPlus使用 Redis缓存.zip
mybatis二级缓存 + reads做第三级缓存
redis本地缓存与redis缓存
Redis高性能缓存
这个主要是用作在Mybatis中的Mapping文件中的二级缓存
两级缓存在redis的方案上做一步优化,在缓存到远程redis的同时,缓存一份到本地进程ehcache(此处的ehcache不用做集群,避免组播带来的开销),取缓存的时候会先取本地,没有会向redis请求,这样会减少应用服务器<–...
使用Redis存放Session RedisManager
redis缓存服务器
spark一个Linux下使用C++学习编写的并发服务器,使用redis作为缓存和MySQL作为数据库,并利用GoogleTest编写单元测试和webbench进行压力测试。.zipspark一个Linux下使用C++学习编写的并发服务器,使用redis作为缓存...
tomcat 集群 nginx 使用redis 保证session同步
05-一线大厂Redis高并发缓存架构实战与性能优化_ev.rar05-一线大厂Redis高并发缓存架构实战与性能优化_ev.rar05-一线大厂Redis高并发缓存架构实战与性能优化_ev.rar05-一线大厂Redis高并发缓存架构实战与性能优化_ev...
redis分布式缓存+spring整合及 集群、分片等配置使用例子
springboot 使用 redis guava caffeine 缓存示例
tomcat-redis实现session共享
连接redis服务器,管理缓存数据,新增、修改、删除、刷新