• Java使用Memcached


    这些天在设计SNA的架构,接触了一些远程缓存、集群、session复制等的东西,以前做企业应用的时候感觉作用不大,现在设计面对internet的系统架构时就非常有用了,而且在调试后看到压力测试的情况还是比较爽的。 
    在缓存的选择上有过很多的思考,虽然说memcached结合java在序列化上性能不怎么样,不过也没有更好的集群环境下的缓存解决方案了,就选择了memcached。本来计划等公司买的服务器到位装个linux再来研究memcached,但这两天在找到了一个windows下的Memcached版本,就动手开始调整现有的框架了。 

    windows下的Server端很简单,不用安装,双击运行后默认服务端口是11211,没有试着去更改端口,因为反正以后会用unix版本,到时再记录安装步骤。下载客户端的java API包,接口非常简单,参考API手册上就有现成的例子。 

    目标,对旧框架缓存部分进行改造: 
    1、缓存工具类 
    2、hibernate的provider 
    3、用缓存实现session机制 

    今天先研究研究缓存工具类的改造,在旧框架中部分函数用了ehcache对执行结果进行了缓存处理,现在目标是提供一个缓存工具类,在配置文件中配置使用哪种缓存(memcached或ehcached),使其它程序对具体的缓存不依赖,同时使用AOP方式来对方法执行结果进行缓存。 
    首先是工具类的实现: 
    在Spring中配置 
    Java代码
    1. <!-- EhCache Manager -->   
    2. <bean id="cacheManager"  
    3.     class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">   
    4.     <property name="configLocation">   
    5.         <value>classpath:ehcache.xml</value>   
    6.     </property>   
    7. </bean>   
    8.   
    9. <bean id="localCache"  
    10.     class="org.springframework.cache.ehcache.EhCacheFactoryBean">   
    11.     <property name="cacheManager" ref="cacheManager" />   
    12.     <property name="cacheName"  
    13.         value="×××.cache.LOCAL_CACHE" />   
    14. </bean>   
    15.   
    16. <bean id="cacheService"  
    17.     class="×××.core.cache.CacheService" init-method="init" destroy-method="destory">   
    18.     <property name="cacheServerList" value="${cache.servers}"/>   
    19.     <property name="cacheServerWeights" value="${cache.cacheServerWeights}"/>   
    20.     <property name="cacheCluster" value="${cache.cluster}"/>   
    21.     <property name="localCache" ref="localCache"/>   
    22. </bean>  
    在properties文件中配置${cache.servers} ${cache.cacheServerWeights} ${cache.cluster} 
    具体工具类的代码 
    Java代码
    1. /**  
    2.  * @author Marc  
    3.  *   
    4.  */  
    5. public class CacheService {   
    6.     private Log logger = LogFactory.getLog(getClass());   
    7.   
    8.     private Cache localCache;   
    9.   
    10.     String cacheServerList;   
    11.   
    12.     String cacheServerWeights;   
    13.   
    14.     boolean cacheCluster = false;   
    15.   
    16.     int initialConnections = 10;   
    17.   
    18.     int minSpareConnections = 5;   
    19.   
    20.     int maxSpareConnections = 50;   
    21.   
    22.     long maxIdleTime = 1000 * 60 * 30// 30 minutes   
    23.   
    24.     long maxBusyTime = 1000 * 60 * 5// 5 minutes   
    25.   
    26.     long maintThreadSleep = 1000 * 5// 5 seconds   
    27.   
    28.     int socketTimeOut = 1000 * 3// 3 seconds to block on reads   
    29.   
    30.     int socketConnectTO = 1000 * 3// 3 seconds to block on initial   
    31.                                     // connections. If 0, then will use blocking   
    32.                                     // connect (default)   
    33.   
    34.     boolean failover = false// turn off auto-failover in event of server   
    35.                                 // down   
    36.   
    37.     boolean nagleAlg = false// turn off Nagle's algorithm on all sockets in   
    38.                                 // pool   
    39.   
    40.     MemCachedClient mc;   
    41.   
    42.     public CacheService(){   
    43.         mc = new MemCachedClient();   
    44.         mc.setCompressEnable(false);   
    45.     }   
    46.     /**  
    47.      * 放入  
    48.      *   
    49.      */  
    50.     public void put(String key, Object obj) {   
    51.         Assert.hasText(key);   
    52.         Assert.notNull(obj);   
    53.         Assert.notNull(localCache);   
    54.         if (this.cacheCluster) {   
    55.             mc.set(key, obj);   
    56.         } else {   
    57.             Element element = new Element(key, (Serializable) obj);   
    58.             localCache.put(element);   
    59.         }   
    60.     }   
    61.     /**  
    62.      * 删除   
    63.      */  
    64.     public void remove(String key){   
    65.         Assert.hasText(key);   
    66.         Assert.notNull(localCache);   
    67.         if (this.cacheCluster) {   
    68.             mc.delete(key);   
    69.         }else{   
    70.             localCache.remove(key);   
    71.         }   
    72.     }   
    73.     /**  
    74.      * 得到  
    75.      */  
    76.     public Object get(String key) {   
    77.         Assert.hasText(key);   
    78.         Assert.notNull(localCache);   
    79.         Object rt = null;   
    80.         if (this.cacheCluster) {   
    81.             rt = mc.get(key);   
    82.         } else {   
    83.             Element element = null;   
    84.             try {   
    85.                 element = localCache.get(key);   
    86.             } catch (CacheException cacheException) {   
    87.                 throw new DataRetrievalFailureException("Cache failure: "  
    88.                         + cacheException.getMessage());   
    89.             }   
    90.             if(element != null)   
    91.                 rt = element.getValue();   
    92.         }   
    93.         return rt;   
    94.     }   
    95.     /**  
    96.      * 判断是否存在  
    97.      *   
    98.      */  
    99.     public boolean exist(String key){   
    100.         Assert.hasText(key);   
    101.         Assert.notNull(localCache);   
    102.         if (this.cacheCluster) {   
    103.             return mc.keyExists(key);   
    104.         }else{   
    105.             return this.localCache.isKeyInCache(key);   
    106.         }   
    107.     }   
    108.     private void init() {   
    109.         if (this.cacheCluster) {   
    110.             String[] serverlist = cacheServerList.split(",");   
    111.             Integer[] weights = this.split(cacheServerWeights);   
    112.             // initialize the pool for memcache servers   
    113.             SockIOPool pool = SockIOPool.getInstance();   
    114.             pool.setServers(serverlist);   
    115.             pool.setWeights(weights);   
    116.             pool.setInitConn(initialConnections);   
    117.             pool.setMinConn(minSpareConnections);   
    118.             pool.setMaxConn(maxSpareConnections);   
    119.             pool.setMaxIdle(maxIdleTime);   
    120.             pool.setMaxBusyTime(maxBusyTime);   
    121.             pool.setMaintSleep(maintThreadSleep);   
    122.             pool.setSocketTO(socketTimeOut);   
    123.             pool.setSocketConnectTO(socketConnectTO);   
    124.             pool.setNagle(nagleAlg);   
    125.             pool.setHashingAlg(SockIOPool.NEW_COMPAT_HASH);   
    126.             pool.initialize();   
    127.             logger.info("初始化memcached pool!");   
    128.         }   
    129.     }   
    130.   
    131.     private void destory() {   
    132.         if (this.cacheCluster) {   
    133.             SockIOPool.getInstance().shutDown();   
    134.         }   
    135.     }   
    136. }  

    然后实现函数的AOP拦截类,用来在函数执行前返回缓存内容 
    Java代码
    1. public class CachingInterceptor implements MethodInterceptor {   
    2.   
    3.     private CacheService cacheService;   
    4.     private String cacheKey;   
    5.   
    6.     public void setCacheKey(String cacheKey) {   
    7.         this.cacheKey = cacheKey;   
    8.     }   
    9.   
    10.     public void setCacheService(CacheService cacheService) {   
    11.         this.cacheService = cacheService;   
    12.     }   
    13.   
    14.     public Object invoke(MethodInvocation invocation) throws Throwable {   
    15.         Object result = cacheService.get(cacheKey);   
    16.         //如果函数返回结果不在Cache中,执行函数并将结果放入Cache   
    17.         if (result == null) {   
    18.             result = invocation.proceed();   
    19.             cacheService.put(cacheKey,result);   
    20.         }   
    21.         return result;   
    22.     }   
    23. }  
    Spring的AOP配置如下: 
    Java代码
    1. <aop:config proxy-target-class="true">   
    2.         <aop:advisor   
    3.             pointcut="execution(* ×××.PoiService.getOne(..))"  
    4.             advice-ref="PoiServiceCachingAdvice" />   
    5.     </aop:config>   
    6.   
    7.     <bean id="BasPoiServiceCachingAdvice"  
    8.         class="×××.core.cache.CachingInterceptor">   
    9.         <property name="cacheKey" value="PoiService" />   
    10.         <property name="cacheService" ref="cacheService" />   
    11.     </bean>  
  • 相关阅读:
    第四季-专题8-Linux系统调用
    第四季-专题7-Linux内核链表
    Python3 运算符
    Python2和Python3有什么区别?
    python常见的PEP8规范
    机器码和字节码
    域名是什么?为什么域名是www开头?
    selenium自动化登录qq邮箱
    xpath+selenium工具爬取腾讯招聘全球岗位需求
    ArrayList
  • 原文地址:https://www.cnblogs.com/daichangya/p/12958629.html
Copyright © 2020-2023  润新知