Jedis缓存实现类

来源:互联网 发布:淘宝一件代发货源 编辑:程序博客网 时间:2024/06/08 09:29

pom.xml导入架包

[html] view plain copy
  1. <!-- 导入jedis架包 -->  
  2. <dependency>  
  3.   <groupId>redis.clients</groupId>  
  4.   <artifactId>jedis</artifactId>  
  5.   <version>2.7.1</version>  
  6. </dependency>  


Jedis缓存实现类

[java] view plain copy
  1. package cn.et.mybatis.buff;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.ObjectInputStream;  
  7. import java.io.ObjectOutputStream;  
  8. import java.util.Set;  
  9. import java.util.concurrent.locks.ReadWriteLock;  
  10. import java.util.concurrent.locks.ReentrantReadWriteLock;  
  11.   
  12. import org.apache.commons.pool2.impl.GenericObjectPoolConfig;  
  13. import org.apache.ibatis.cache.Cache;  
  14.   
  15. import redis.clients.jedis.Jedis;  
  16. import redis.clients.jedis.JedisPool;  
  17.   
  18. public class JedisCache implements Cache {  
  19.   
  20.     /** 
  21.      * 序列化和反序列化的操作类 
  22.      * @author Administrator 
  23.      * 
  24.      */  
  25.     static class SeqUtils{  
  26.           
  27.         /** 
  28.          * 对象序列化成字节数组 
  29.          * @param obj 
  30.          * @return 
  31.          * @throws IOException 
  32.          */  
  33.         public static byte[] ser(Object obj) throws IOException{  
  34.             ByteArrayOutputStream baos=new ByteArrayOutputStream();  
  35.             ObjectOutputStream obos=new ObjectOutputStream(baos);  
  36.             obos.writeObject(obj);  
  37.             return baos.toByteArray();  
  38.         }  
  39.   
  40.         /** 
  41.          * 反序列化为对象 
  42.          * @param bt 
  43.          * @return 
  44.          * @throws IOException 
  45.          * @throws ClassNotFoundException 
  46.          */  
  47.         static public Object deSer(byte[] bt) throws IOException, ClassNotFoundException{  
  48.             ByteArrayInputStream bais=new ByteArrayInputStream(bt);  
  49.             ObjectInputStream ois = new ObjectInputStream(bais);  
  50.             return ois.readObject();  
  51.         }  
  52.     }  
  53.       
  54.     static JedisPool jp = null;  
  55.     private String id;  
  56.       
  57.     public JedisCache(String id){  
  58.         if(jp==null){  
  59.             try {  
  60.                 GenericObjectPoolConfig gopc=new GenericObjectPoolConfig();  
  61.                 gopc.setMaxTotal(100);  
  62.                 gopc.setMaxIdle(10);  
  63.                 jp=new JedisPool(gopc, "localhost");  
  64.                 //去数据库连接一下,这样出错就会抛异常  
  65.                 Jedis jedis = jp.getResource();  
  66.                 jp.returnResourceObject(jedis);  
  67.             } catch (Exception e) {  
  68.                 //e.printStackTrace();  
  69.                 jp=null;  
  70.             }  
  71.         }  
  72.         this.id=id;  
  73.     }  
  74.       
  75.     /** 
  76.      * 清空所有的缓存 
  77.      */  
  78.     public void clear() {  
  79.         Jedis jedis = jp.getResource();  
  80.         jedis.flushAll();  
  81.         jp.returnResourceObject(jedis);  
  82.     }  
  83.   
  84.     /** 
  85.      * 返回当前的id 
  86.      */  
  87.     public String getId() {  
  88.         return this.id;  
  89.     }  
  90.   
  91.     /** 
  92.      * maBatis会自动调用该方法,判断返回值是否为null 
  93.      *      如果为空就去数据查 
  94.      *      不为空就直接返回缓存对象 
  95.      */  
  96.     public Object getObject(Object key) {  
  97.         //从连接池中获取一条连接  
  98.         Jedis jedis = jp.getResource();  
  99.         try {  
  100.             byte[] bt = jedis.get(SeqUtils.ser(key));  
  101.             if(bt!=null){  
  102.                 Object obj = SeqUtils.deSer(bt);  
  103.                 //将redis对象回收到连接池中  
  104.                 jp.returnResourceObject(jedis);  
  105.                 return obj;  
  106.             }  
  107.         } catch (Exception e) {  
  108.             e.printStackTrace();  
  109.         }  
  110.         return null;  
  111.     }  
  112.   
  113.     /** 
  114.      * non-block io 非阻塞式IO 
  115.      * 读写锁 
  116.      * 同一时间只能有一个线程访问 
  117.      */  
  118.     public ReadWriteLock getReadWriteLock() {  
  119.         return new ReentrantReadWriteLock();  
  120.     }  
  121.   
  122.     /** 
  123.      * 用于读取redis中缓存了多少元素 
  124.      */  
  125.     public int getSize() {  
  126.         Jedis jedis=jp.getResource();  
  127.         Set<String> allElements=jedis.keys("*");  
  128.         return allElements.size();  
  129.     }  
  130.   
  131.     /** 
  132.      * 第一次查询数据库后mybatis会自动调用该方法将数据写入缓存 
  133.      */  
  134.     public void putObject(Object key, Object value) {  
  135.         Jedis jedis=jp.getResource();  
  136.         try {  
  137.             jedis.set(SeqUtils.ser(key), SeqUtils.ser(value));  
  138.             //将redis对象回收到连接池中  
  139.             jp.returnResourceObject(jedis);  
  140.         } catch (IOException e) {  
  141.             e.printStackTrace();  
  142.         }  
  143.     }  
  144.   
  145.     /** 
  146.      * 调用了myBatis flush方法自动清空缓存 
  147.      *      自动调用removeObject 需要从redis中删除该元素 
  148.      */  
  149.     public Object removeObject(Object key) {  
  150.         Jedis jedis=jp.getResource();  
  151.         try {  
  152.             jedis.del(SeqUtils.ser(key));  
  153.             //将redis对象回收到连接池中  
  154.             jp.returnResourceObject(jedis);  
  155.         } catch (IOException e) {  
  156.             e.printStackTrace();  
  157.         }  
  158.         return null;  
  159.     }  
  160.   

原创粉丝点击