shiro.properties:
#shiro
password.algorithmName=md5
password.hashIterations=2
redis.host.ip=163.1.9.37
redis.host.port=6379
redis.pool.maxActive=10
redis.pool.maxIdle=2
redis.pool.maxWaitTime=100
redis.pool.testOnBorrow=true
package com.ebj.util;
import java.io.IOException;
import java.util.Properties;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* redis工具类
* @author
*
*/
public class RedisUtil {
private static JedisPool pool = null;
/**
* 构建redis连接池
*
* @param ip
* @param port
* @return JedisPool
* @throws IOException
*/
public static JedisPool getPool() throws IOException {
if (pool == null) {
JedisPoolConfig config = new JedisPoolConfig();
//控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;
//如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
// config.setMaxActive(500);
config.setMaxTotal(500);
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
config.setMaxIdle(5);
//表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
//config.setMaxWait(1000 * 100);
config.setMaxWaitMillis(1000 * 100);
//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
config.setTestOnBorrow(true);
FileUtil fileUtil = new FileUtil();
Properties properties = fileUtil.loadProperty("/config/shiro.properties");
String redisIp = properties.getProperty("redis.host.ip");
String redisPort = properties.getProperty("redis.host.port");
System.out.println("redisIp=================" + redisIp + ",redisPort===============" + redisPort);
pool = new JedisPool(config, redisIp, Integer.parseInt(redisPort));
}
return pool;
}
/**
* 返还到连接池
*
* @param pool
* @param redis
*/
public static void returnResource(JedisPool pool, Jedis redis) {
if (redis != null) {
pool.returnResource(redis);
}
}
/**
* 获取数据
*
* @param key
* @return
*/
public static String get(String key){
String value = null;
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
value = jedis.get(key);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
} finally {
//返还到连接池
returnResource(pool, jedis);
}
return value;
}
/**
* 删除数据
*
* @param key
* @return
*/
public static long del(String key){
long value = 0;
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
value = jedis.del(key);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
} finally {
//返还到连接池
returnResource(pool, jedis);
}
return value;
}
/**
* 删除数据
*
* @param key
* @return
*/
public static boolean exists(String key){
boolean value = false;
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
value = jedis.exists(key);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
} finally {
//返还到连接池
returnResource(pool, jedis);
}
return value;
}
}
import com.ebj.util.RedisUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class test{
@RequestMapping("/list")
public String customerList(HttpServletRequest request,Model model) {
JedisPool jedisPool=null;
Jedis jedis=null;
try{
jedisPool = RedisUtil.getPool();
jedis = jedisPool.getResource();
//给userCardController用
jedis.set("callerNo_"+userSeq, caller);
}catch(Exception e){
logger.info("缓存=="+e);
request.setAttribute("errorMsg","你所访问的页面不存在!");
return "common/404";
}finally{
//返还到连接池
RedisUtil.returnResource(jedisPool, jedis);
}
}
}