使用redis之前需要咋电脑上安装redis;
使用spring+mybaties+redis的本质是扩展类 org.apache.ibatis.cache.Cache;在我们自己扩展的Cache里面使用redis的api;
一:需要引入的依赖:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
</dependency>
二:在mybaties-config.xml配置文件中开启缓存:
<settings>
<!-- 这个配置使全局的映射器启用或禁用缓存 -->
<setting name="cacheEnabled" value="true"/>
<!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 -->
<setting name="lazyLoadingEnabled" value="false"/>
<setting name="aggressiveLazyLoading" value="true"/>
</settings>
此处需要注意的是:
属性延迟加载和关联对象加载(lazyLoadingEnabled)给关闭了,不然放进redis中的cglib代理对象,在对数据发生更改的时候,会出错。
三:写属于自己的扩展类(org.apache.ibatis.cache.Cache类的扩展类):
类一如下:
package com.rayeye.law.app.utils.redis;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.cache.Cache;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* Created by Sean on 2016/9/13.
*/
public class RedisCache implements Cache {
private static Log logger = LogFactory.getLog(RedisCache.class);
private Jedis redisClient = createClient();
/** 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;
}
@Override
public String getId() {
return this.id;
}
@Override
public int getSize() {
return Integer.valueOf(redisClient.dbSize().toString());
}
@Override
public void putObject(Object key, Object value) {
logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>putObject:" + key + "=" + value);
redisClient.set(SerializeUtil.serialize(key.toString()), SerializeUtil.serialize(value));
}
@Override
public Object getObject(Object key) {
Object value = SerializeUtil.unserialize(redisClient.get(SerializeUtil.serialize(key.toString())));
logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>getObject:" + key + "=" + value);
return value;
}
@Override
public Object removeObject(Object key) {
return redisClient.expire(SerializeUtil.serialize(key.toString()), 0);
}
@Override
public void clear() {
redisClient.flushDB();
}
@Override
public ReadWriteLock getReadWriteLock() {
return readWriteLock;
}
protected static Jedis createClient() {
try {
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
return pool.getResource();
} catch (Exception e) {
e.printStackTrace();
}
throw new RuntimeException("初始化连接池错误");
}
}
class SerializeUtil {
public static byte[] serialize(Object object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
// 序列化
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] bytes = baos.toByteArray();
return bytes;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Object unserialize(byte[] bytes) {
if(bytes == null)return null;
ByteArrayInputStream bais = null;
try {
// 反序列化
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
类二如下:
package com.rayeye.law.app.utils.redis;
import org.apache.ibatis.cache.decorators.LoggingCache;
/**
* Created by Sean on 2016/9/13.
*/
public class LoggingRedisCache extends LoggingCache {
public LoggingRedisCache(String id) {
super(new RedisCache(id));
}
}
四:在mapper.xml文件中直接引用缓存
- <!-- 启用缓存 -->
- <cache type="cn.seafood.cache.LoggingRedisCache" />
如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.rayeye.law.app.dao.LawAssistantMapper">
<cache type="com.rayeye.law.app.utils.redis.LoggingRedisCache"/>
<select id="getWFK" parameterType="java.util.Map" resultType="java.util.Map">
select * from weifaku where entityStatus=0
</select>
</mapper>
以上为spring+mybaties+redis简单整合的流程,
redis的优势:
1.性能极高,
2.丰富的数据类型,
3.操作具有原子性(联合的操作也具有原子性),
缺点:
1.收到物理内存的限制(redis是一个独立的模块,独占一定内存),不能适用于海量数据的读写(适用于较小数据量的高性能操作)。
总结:Redis受限于特定的场景,专注于特定的领域之下,速度相当之快,目前还未找到能替代使用产品。