ImageVerifierCode 换一换
格式:DOCX , 页数:9 ,大小:17.92KB ,
资源ID:4770989      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/4770989.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Java环境下Memcached应用详解.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Java环境下Memcached应用详解.docx

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