JedisUtil.java
package cn.ssm.utils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisUtil {
private static String ADDR = "127.0.0.1";
private static int PORT = 6379;
private static String AUTH ;
private static int MAX_ACTIVE = 100;
private static int MAX_IDLE = 20;
private static int MAX_WAIT = 10000;
private static int TIMEOUT = 10000;
private static boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null;
static {
try{
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(MAX_IDLE);
config.setMaxTotal(MAX_ACTIVE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config,ADDR,PORT,TIMEOUT,AUTH);
}catch (Exception e) {
e.printStackTrace();
}
}
public synchronized static Jedis getJedis(){
try{
if(jedisPool != null){
Jedis jedis = jedisPool.getResource();
return jedis;
}else{
return null;
}
}catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void returnResource(final Jedis jedis){
if(jedis != null){
//jedisPool.returnResource(jedis);
jedisPool.close();
}
}
// 出现异常释放资源
public synchronized void returnBrokenResource(Jedis jedis) {
if (jedis != null) {
//jedisPool.returnBrokenResource(jedis);
jedisPool.close();
}
}
}
SerializeUtil.java
package cn.ssm.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
*
* @Description: 序列化反序列化工具
*/
public class SerializeUtil {
/**
*
* 序列化
*/
public static byte[] serialize(Object obj){
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
//序列化
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
byte[] byteArray = baos.toByteArray();
return byteArray;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
*
* 反序列化
* @param bytes
* @return
*/
public static Object unSerialize(byte[] bytes) {
if(bytes!=null){
ByteArrayInputStream bais = null;
try {
//反序列化为对象
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}
RedisCache.java
package cn.ssm.cache;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.ibatis.cache.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.ssm.utils.*;
/*
* 使用第三方缓存服务器,处理二级缓存
*/
public class RedisCache implements Cache {
private static Logger logger = LoggerFactory.getLogger(RedisCache.class);
/** The ReadWriteLock. */
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private String id;
public RedisCache(final String id) {
if (id == null) {
throw new IllegalArgumentException("Cache instances require an ID");
}
logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>MybatisRedisCache:id="+id);
this.id = id;
}
public String getId() {
return this.id;
}
public void putObject(Object key, Object value) {
logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>putObject:"+key+"="+value);
JedisUtil.getJedis().set(SerializeUtil.serialize(key.toString()), SerializeUtil.serialize(value));
//JedisUtil.getJedis().set(SerializeUtil.serialize(key.toString()),SerializeUtil.serialize(value));
}
public Object getObject(Object key) {
Object value = SerializeUtil.unSerialize(JedisUtil.getJedis().get(SerializeUtil.serialize(key.toString())));
//Object value = SerializeUtil.unserialize(JedisUtil.getJedis().get(SerializeUtil.serialize(key.toString())));
logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>getObject:"+key+"="+value);
return value;
}
public Object removeObject(Object key) {
return JedisUtil.getJedis().expire(SerializeUtil.serialize(key.toString()),0);
}
public void clear() {
JedisUtil.getJedis().flushDB();
}
public int getSize() {
return Integer.valueOf(JedisUtil.getJedis().dbSize().toString());
}
public ReadWriteLock getReadWriteLock() {
return readWriteLock;
}
}
注意: 在反序列化时一定要判断byte [] 数组是否为空,否则会出现java.lang.NullPointerException异常!