redis( 3 )redis与Mybatis的无缝整合让MyBatis透明的管理缓存

redis的安装http://liuyieyer.iteye.com/blog/2078093

redis的主从高可用 http://liuyieyer.iteye.com/blog/2078095

Mybatis 的使用不多说。

Mybatis为了方便我们扩展缓存定义了一个Cache接口,看看ehcache-mybatis的源码就明白了。我们要使用自己的cache同样的实现Cache接口即可。直接上代码

Java代码   收藏代码
  1. public class RedisCache   implements Cache {  
  2.         private static Log logger = LogFactory.getLog(RedisCache.class);  
  3.         private Jedis redisClient = createClient();  
  4.         /** The ReadWriteLock. */  
  5.         private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();  
  6.           
  7.         private String id;  
  8.         public RedisCache(final String id) {  
  9.                 if (id == null) {  
  10.                         throw new IllegalArgumentException("Cache instances require an ID");  
  11.                 }  
  12.                 logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>MybatisRedisCache:id=" + id);  
  13.                 this.id = id;  
  14.         }  
  15.   
  16.         @Override  
  17.         public String getId() {  
  18.                 return this.id;  
  19.         }  
  20.   
  21.         @Override  
  22.         public int getSize() {  
  23.                 return Integer.valueOf(redisClient.dbSize().toString());  
  24.         }  
  25.   
  26.         @Override  
  27.         public void putObject(Object key, Object value) {  
  28.                 logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>putObject:" + key + "=" + value);  
  29.                 redisClient.set(SerializeUtil.serialize(key.toString()), SerializeUtil.serialize(value));  
  30.         }  
  31.   
  32.         @Override  
  33.         public Object getObject(Object key) {  
  34.                 Object value = SerializeUtil.unserialize(redisClient.get(SerializeUtil.serialize(key.toString())));  
  35.                 logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>getObject:" + key + "=" + value);  
  36.                 return value;  
  37.         }  
  38.   
  39.         @Override  
  40.         public Object removeObject(Object key) {  
  41.                 return redisClient.expire(SerializeUtil.serialize(key.toString()), 0);  
  42.         }  
  43.   
  44.         @Override  
  45.         public void clear() {  
  46.                 redisClient.flushDB();  
  47.         }  
  48.   
  49.         @Override  
  50.         public ReadWriteLock getReadWriteLock() {  
  51.                 return readWriteLock;  
  52.         }  
  53.          
  54.         protected  static Jedis createClient() {  
  55.                 try {  
  56.                         JedisPool pool = new JedisPool(new JedisPoolConfig(), "172.60.0.172");  
  57.                        return pool.getResource();  
  58.                 } catch (Exception e) {  
  59.                         e.printStackTrace();  
  60.                 }  
  61.                 throw new RuntimeException("初始化连接池错误");  
  62.         }  
  63.           
  64.     
  65.           
  66. }  
  67.   
  68.   
  69. class SerializeUtil {  
  70.         public static byte[] serialize(Object object) {  
  71.                 ObjectOutputStream oos = null;  
  72.                 ByteArrayOutputStream baos = null;  
  73.                 try {  
  74.                         // 序列化  
  75.                         baos = new ByteArrayOutputStream();  
  76.                         oos = new ObjectOutputStream(baos);  
  77.                         oos.writeObject(object);  
  78.                         byte[] bytes = baos.toByteArray();  
  79.                         return bytes;  
  80.                 } catch (Exception e) {  
  81.                         e.printStackTrace();  
  82.                 }  
  83.                 return null;  
  84.         }  
  85.   
  86.         public static Object unserialize(byte[] bytes) {  
  87.                 if(bytes == null)return null;  
  88.                 ByteArrayInputStream bais = null;  
  89.                 try {  
  90.                         // 反序列化  
  91.                         bais = new ByteArrayInputStream(bytes);  
  92.                         ObjectInputStream ois = new ObjectInputStream(bais);  
  93.                         return ois.readObject();  
  94.                 } catch (Exception e) {  
  95.                         e.printStackTrace();  
  96.                 }  
  97.                 return null;  
  98.         }  
  99. }  

 

在看ehcache-mybatis的源码 它真正使用cache的方式是通过集成org.apache.ibatis.cache.decorators.LoggingCache 这个类实现的,照猫画虎,直接我们也继承

Java代码   收藏代码
  1. public class LoggingRedisCache extends LoggingCache {  
  2.   
  3.         public LoggingRedisCache(String id) {  
  4.                 super(new RedisCache(id));  
  5.         }  
  6.         
  7. }  

 

在mapper.xml中添加如下cache标签

 

Xml代码   收藏代码
  1. <!-- 启用缓存 -->  
  2.     <cache type="cn.seafood.cache.LoggingRedisCache" />   

 在mybatis的核心文件中开启缓存

Xml代码   收藏代码
  1. <settings>  
  2.     <!-- 这个配置使全局的映射器启用或禁用缓存 -->  
  3.     <setting name="cacheEnabled" value="true" />  
  4.  <!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->      
  5.         <setting name="multipleResultSetsEnabled" value="true"/>  
  6.     <!-- 配置默认的执行器。SIMPLE 执行器没有什么特别之处。REUSE 执行器重用预处理语句。BATCH 执行器重用语句和批量更新 -->  
  7.     <setting name="defaultExecutorType" value="REUSE" />  
  8.     <!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 -->  
  9.     <setting name="lazyLoadingEnabled" value="false" />  
  10.     <setting name="aggressiveLazyLoading" value="true" />  
  11.     <!-- <setting name="enhancementEnabled" value="true"/> -->  
  12.     <!-- 设置超时时间,它决定驱动等待一个数据库响应的时间。 -->  
  13.     <setting name="defaultStatementTimeout" value="25000" />  
  14. </settings>  

 

<setting name="lazyLoadingEnabled" value="false" />

<setting name="aggressiveLazyLoading" value="true" />

注意着两个属性,需要把属性延迟加载和关联对象加载给关闭了,不然放进redis中的cglib代理对象,在对数据发生更改的时候,会出错。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值