1、Java环境下Memcached应用详解Java环境下Memcached应用详解来自网络,共享与大家。爱好技术的朋友,一起努力!这里将介绍Java环境下Memcached应用,Memcached主要是集群环境下的缓存解决方案,希望本文对大家有所帮助。本文将对在Java环境下Memcached应用进行详细介绍。Memcached主要是集群环境下的缓存解决方案,可以运行在Java或者.NET平台上,这里我们主要讲的是Windows下的Memcached应用。这些天在设计SNA的架构,接触了一些远程缓存、集群、session复制等的东西,以前做企业应用的时候感觉作用不大,现在设计面对internet
2、的系统架构时就非常有用了,而且在调试后看到压力测试的情况还是比较好的。在缓存的选择上有过很多的思考,虽然说memcached结合java在序列化上性能不怎么样,不过也没有更好的集群环境下的缓存解决方案了, 就选择了memcached。本来计划等公司买的服务器到位装个linux再来研究memcached,但这两天在找到了一个windows下的 Memcached版本,就动手开始调整现有的框架了。Windows下的Server端很简单,不用安装,双击运行后默认服务端口是11211,没有试着去更改端口,因为反正以后会用Unix版本,到时再记录安装步骤。下载客户端的JavaAPI包,接口非常简单,参考
3、API手册上就有现成的例子。目标,对旧框架缓存部分进行改造:1、缓存工具类2、hibernate的provider3、用缓存实现session机制今天先研究研究缓存工具类的改造,在旧框架中部分函数用了ehcache对执行结果进行了缓存处理,现在目标是提供一个缓存工具类,在配置文件 中配置使用哪种缓存(memcached或ehcached),使其它程序对具体的缓存不依赖,同时使用AOP方式来对方法执行结果进行缓存。首先是工具类的实现:在Spring中配置Java代码 classpath:ehcache.xmlvalue property bean bean bean classpath:ehca
4、che.xmlvalue property bean bean bean在properties文件中配置$cache.servers $cache.cacheServerWeights $cache.cluster具体工具类的代码Java代码/* *authorMarc * */ publicclassCacheService privateLoglogger=LogFactory.getLog(getClass(); privateCachelocalCache; StringcacheServerList; StringcacheServerWeights; booleancacheClu
5、ster=false; intinitialConnections=10; intminSpareConnections=5; intmaxSpareConnections=50; longmaxIdleTime=1000*60*30;/30minutes longmaxBusyTime=1000*60*5;/5minutes longmaintThreadSleep=1000*5;/5seconds intsocketTimeOut=1000*3;/3secondstoblockonreads intsocketConnectTO=1000*3;/3secondstoblockoniniti
6、al /connections.If0,thenwilluseblocking /connect(default) booleanfailover=false;/turnoffauto-failoverineventofserver /down booleannagleAlg=false;/turnoffNaglesalgorithmonallsocketsin /pool MemCachedClientmc; publicCacheService() mc=newMemCachedClient(); mc.setCompressEnable(false); /* *放入 * */ publi
7、cvoidput(Stringkey,Objectobj) Assert.hasText(key); Assert.notNull(obj); Assert.notNull(localCache); if(this.cacheCluster) mc.set(key,obj); else Elementelement=newElement(key,(Serializable)obj); localCache.put(element); /* *删除 */ publicvoidremove(Stringkey) Assert.hasText(key); Assert.notNull(localCa
8、che); if(this.cacheCluster) mc.delete(key); else localCache.remove(key); /* *得到 */ publicObjectget(Stringkey) Assert.hasText(key); Assert.notNull(localCache); Objectrt=null; if(this.cacheCluster) rt=mc.get(key); else Elementelement=null; try element=localCache.get(key); catch(CacheExceptioncacheExce
9、ption) thrownewDataRetrievalFailureException(Cachefailure: +cacheException.getMessage(); if(element!=null) rt=element.getValue(); returnrt; /* *判断是否存在 * */ publicbooleanexist(Stringkey) Assert.hasText(key); Assert.notNull(localCache); if(this.cacheCluster) returnmc.keyExists(key); else returnthis.lo
10、calCache.isKeyInCache(key); privatevoidinit() if(this.cacheCluster) Stringserverlist=cacheServerList.split(,); Integerweights=this.split(cacheServerWeights); /initializethepoolformemcacheservers SockIOPoolpool=SockIOPool.getInstance(); pool.setServers(serverlist); pool.setWeights(weights); pool.setI
11、nitConn(initialConnections); pool.setMinConn(minSpareConnections); pool.setMaxConn(maxSpareConnections); pool.setMaxIdle(maxIdleTime); pool.setMaxBusyTime(maxBusyTime); pool.setMaintSleep(maintThreadSleep); pool.setSocketTO(socketTimeOut); pool.setSocketConnectTO(socketConnectTO); pool.setNagle(nagl
12、eAlg); pool.setHashingAlg(SockIOPool.NEW_COMPAT_HASH); pool.initialize(); logger.info(初始化memcachedpool!); privatevoiddestory() if(this.cacheCluster) SockIOPool.getInstance().shutDown(); /* *authorMarc * */ publicclassCacheService privateLoglogger=LogFactory.getLog(getClass(); privateCachelocalCache;
13、 StringcacheServerList; StringcacheServerWeights; booleancacheCluster=false; intinitialConnections=10; intminSpareConnections=5; intmaxSpareConnections=50; longmaxIdleTime=1000*60*30;/30minutes longmaxBusyTime=1000*60*5;/5minutes longmaintThreadSleep=1000*5;/5seconds intsocketTimeOut=1000*3;/3second
14、stoblockonreads intsocketConnectTO=1000*3;/3secondstoblockoninitial /connections.If0,thenwilluseblocking /connect(default) booleanfailover=false;/turnoffauto-failoverineventofserver /down booleannagleAlg=false;/turnoffNaglesalgorithmonallsocketsin /pool MemCachedClientmc; publicCacheService() mc=new
15、MemCachedClient(); mc.setCompressEnable(false); /* *放入 * */ publicvoidput(Stringkey,Objectobj) Assert.hasText(key); Assert.notNull(obj); Assert.notNull(localCache); if(this.cacheCluster) mc.set(key,obj); else Elementelement=newElement(key,(Serializable)obj); localCache.put(element); /* *删除 */ public
16、voidremove(Stringkey) Assert.hasText(key); Assert.notNull(localCache); if(this.cacheCluster) mc.delete(key); else localCache.remove(key); /* *得到 */ publicObjectget(Stringkey) Assert.hasText(key); Assert.notNull(localCache); Objectrt=null; if(this.cacheCluster) rt=mc.get(key); else Elementelement=nul
17、l; try element=localCache.get(key); catch(CacheExceptioncacheException) thrownewDataRetrievalFailureException(Cachefailure: +cacheException.getMessage(); if(element!=null) rt=element.getValue(); returnrt; /* *判断是否存在 * */ publicbooleanexist(Stringkey) Assert.hasText(key); Assert.notNull(localCache);
18、if(this.cacheCluster) returnmc.keyExists(key); else returnthis.localCache.isKeyInCache(key); privatevoidinit() if(this.cacheCluster) Stringserverlist=cacheServerList.split(,); Integerweights=this.split(cacheServerWeights); /initializethepoolformemcacheservers SockIOPoolpool=SockIOPool.getInstance();
19、 pool.setServers(serverlist); pool.setWeights(weights); pool.setInitConn(initialConnections); pool.setMinConn(minSpareConnections); pool.setMaxConn(maxSpareConnections); pool.setMaxIdle(maxIdleTime); pool.setMaxBusyTime(maxBusyTime); pool.setMaintSleep(maintThreadSleep); pool.setSocketTO(socketTimeO
20、ut); pool.setSocketConnectTO(socketConnectTO); pool.setNagle(nagleAlg); pool.setHashingAlg(SockIOPool.NEW_COMPAT_HASH); pool.initialize(); logger.info(初始化memcachedpool!); privatevoiddestory() if(this.cacheCluster) SockIOPool.getInstance().shutDown(); 然后实现函数的AOP拦截类,用来在函数执行前返回缓存内容Java代码publicclassCachingInterceptorimplementsMethodInterceptor privateCacheServicecacheService; privateStringcacheKey; publicvoidsetCacheKey(StringcacheKey) this.cacheKey=cacheKey;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1