对于网上关于memcached缓存更新策略 数不胜数,但是没有一遍完整的,看起来都很费劲,由于项目中用到的memcache,自然就想到了memcache缓存更新策略的实现。

你可以把你更新缓存的代码嵌套你的代码中,但是这样很不好,混换了你service的代码,要是以后再换别的缓存产品,那么你还要每个类去找,去修改很是麻烦。由于之前是这样写的,很是痛苦,所以这次要用spring aop来实现。

下面说以下具体更新策略的实现思路:

首先我们会定义两个注解类,来说明是插入(Cache)缓存还是删除(Flush)缓存,这两个类可以方法上,来对你service需要进行缓存方法操作进行注解标记,注解类内用key的前缀,和缓存的有效时间,接着spring aop 拦截service层含有这个两个注解的方法,获得key的前缀+方法明+参数组装成key值,存入到一张临时表内,如果是Cache注解的话,先判断,缓冲中有没,有从缓存中取得,没有从数据库中查询,然后存缓存,返回结果。如果是Flush注解,说明是删除缓存,那么首先获得注解中key值的前缀,查询库中所以以这个为前缀的key,查询出来 ,删除数据库中的数据,最后在删除缓存中所有的符合这些key的缓存,来达到更新缓存。另外这张临时表,可以做个定时任务日终的时候 ,删除一些无用的数据。

具体代码:

 Cache注解

  1. package com.woaika.loan.commons.annotation;  
  2.   
  3. import java.lang.annotation.Documented;  
  4. import java.lang.annotation.ElementType;  
  5. import java.lang.annotation.Inherited;  
  6. import java.lang.annotation.Retention;  
  7. import java.lang.annotation.RetentionPolicy;  
  8. import java.lang.annotation.Target;  
  9.   
  10. /** 
  11.  * 用于在查询的时候 ,放置缓存信息 
  12.  * @author ajun 
  13.  * @email [email protected] 
  14.  * @blog http://blog.csdn.net/ajun_studio 
  15.  * 2012-2-27 上午10:42:06 
  16.  */  
  17. @Target(ElementType.METHOD)     
  18. @Retention(RetentionPolicy.RUNTIME)      
  19. @Documented      
  20. @Inherited   
  21. public @interface Cache {  
  22.       
  23.     String prefix();//key的前缀,如咨询:zx   
  24.   
  25.     long expiration() default 1000*60*60*2;//缓存有效期 1000*60*60*2==2小时过期   
  26.       
  27.       
  28. }  
Flush注解
  1. package com.woaika.loan.commons.annotation;  
  2.   
  3. import java.lang.annotation.Documented;  
  4. import java.lang.annotation.ElementType;  
  5. import java.lang.annotation.Inherited;  
  6. import java.lang.annotation.Retention;  
  7. import java.lang.annotation.RetentionPolicy;  
  8. import java.lang.annotation.Target;  
  9.   
  10. /** 
  11.  * 用于删除缓存 
  12.  * @author ajun 
  13.  * @email [email protected] 
  14.  * @blog http://blog.csdn.net/ajun_studio 
  15.  * 2012-2-27 上午10:53:03 
  16.  */  
  17. @Target(ElementType.METHOD)     
  18. @Retention(RetentionPolicy.RUNTIME)      
  19. @Documented      
  20. @Inherited   
  21. public @interface Flush {  
  22.     String prefix();//key的前缀,如咨询:zx   
  23. }  
临时表用于存储key值
  1. CREATE TABLE `cache_log` (                                     
  2.              `id` bigint(20) NOT NULL AUTO_INCREMENT,                     
  3.              `prefix` varchar(50) DEFAULT NULL COMMENT 'key的前缀',    
  4.              `cache_key` varchar(300) DEFAULT NULL COMMENT 'key值',      
  5.              `add_time` datetime DEFAULT NULL,                            
  6.              PRIMARY KEY (`id`)                                           
  7.            ) ENGINE=MyISAM DEFAULT CHARSET=utf8    
memcache客户端代码:需要java_memcached-release_2.6.2.jar
  1. package com.woaika.loan.commons.cache;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import com.danga.MemCached.*;   
  6. import com.woaika.loan.commons.constants.CacheConstant;  
  7. public class Memcache {  
  8.   
  9.     static MemCachedClient memCachedClient=null;  
  10.     static{  
  11.           
  12.         String[] servers = { CacheConstant.SERVIERS};    
  13.         SockIOPool pool = SockIOPool.getInstance();    
  14.           
  15.         pool.setServers(servers);    
  16.         pool.setFailover(true);    
  17.      // 设置初始连接数、最小和最大连接数以及最大处理时间    
  18.     /*    pool.setInitConn(5); 
  19.         pool.setMinConn(5); 
  20.         pool.setMaxConn(250); 
  21.         pool.setMaxIdle(1000 * 60 * 60 * 6); */  
  22.         pool.setInitConn(10);    
  23.         pool.setMinConn(5);    
  24.         pool.setMaxConn(250);    
  25.         pool.setMaintSleep(30);  // 设置主线程的睡眠时间    
  26.      // 设置TCP的参数,连接超时等    
  27.         pool.setNagle(false);    
  28.         pool.setSocketTO(3000);    
  29.         pool.setAliveCheck(true);   
  30.           
  31.         pool.initialize();    
  32.          
  33.         memCachedClient = new MemCachedClient();      
  34.         memCachedClient.setPrimitiveAsString(true);//锟斤拷锟叫伙拷   
  35.     }  
  36.     public static Object  get(String key)  
  37.     {  
  38.         return memCachedClient.get(key);  
  39.     }  
  40. //  public static Map<String,Object> gets(String[] keys)   
  41. //  {          
  42. //      return memCachedClient.getMulti(keys);   
  43. //  }   
  44.     public static boolean set(String key,Object o)  
  45.     {  
  46.         return memCachedClient.set(key, o);       
  47.     }  
  48.     public static boolean set(String key,Object o,Date ExpireTime)  
  49.     {         
  50.         return memCachedClient.set(key, o, ExpireTime);  
  51.     }  
  52.     public static boolean exists(String key)  
  53.     {  
  54.         return memCachedClient.keyExists(key);  
  55.     }  
  56.     public static boolean delete(String key)  
  57.     {  
  58.         return memCachedClient.delete(key);  
  59.     }  
  60. }