




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、一 Memcached服务端的安装:1. 将memcached-1.2.1-win32.zip(也可以下载),解压放某个盘下面,比如在c:memcached2. 在终端(也即cmd命令界面)下输入 c:memcachedmemcached.exe -d install 安装memcached成为服务,这样才能正常运行,否则运行失败!3. 再输入: c:memcachedmemcached.exe -d start 启动memcached的。二 Memcached客户端的安装1.解压java_memcached-release_2.0.1.zip,将里面的java_memcached-relea
2、se_2.0.1.jar拷贝到WEB_INF/lib中2.在src目录下新建一个dhcache.xml文件,内容如下: <ehcache><diskStore path="java.io.tmpdir" /><defaultCache name="default" maxElementsInMemory="300" eternal="false" timeToIdleSeconds="500" timeToLiveSeconds="500" ov
3、erflowToDisk="true" diskPersistent="false" diskExpiryThreadIntervalSeconds="110" /><cache name="org.taha.cache.LOCAL_CACHE" maxElementsInMemory="300" eternal="false" timeToIdleSeconds="500" timeToLiveSeconds="500"
4、overflowToDisk="true" /> </ehcache>3.创建一个类,用来封装memcache的各种操作(连接,添加,删除,关闭等)public class CacheServiceprivate Cache localCache;String cacheServerList;String cacheServerWeights;boolean cacheCluster = false;int initialConnections = 10;int minSpareConnections = 5;int maxSpareConnections
5、 = 50;long maxIdleTime = 1000 * 60 * 30; / 30 minutes long maxBusyTime = 1000 * 60 * 5; / 5 minutes long maintThreadSleep = 1000 * 5; / 5 seconds int socketTimeOut = 1000 * 3; / 3 seconds to block on reads int socketConnectTO = 1000 * 3; / 3 seconds to block on initial boolean failover = false; / tu
6、rn off auto-failover in event of server / down boolean nagleAlg = false; / turn off Nagle's algorithm on all sockets in / pool MemCachedClient mc;public CacheService()mc = new MemCachedClient();mc.setCompressEnable(false);public void setLocalCache(Cache localCache)this.localCache = localCache;pu
7、blic void setCacheServerList(String cacheServerList)this.cacheServerList = cacheServerList;public void setCacheServerWeights(String cacheServerWeights)this.cacheServerWeights = cacheServerWeights;public void setCacheCluster(boolean cacheCluster)this.cacheCluster = cacheCluster;/* * 放入 * */public voi
8、d put(String key, Object obj)if (this.cacheCluster)mc.set(key, obj); elseElement element = new Element(key, (Serializable) obj);localCache.put(element);/* * 删除 */public void remove(String key)if (this.cacheCluster)mc.delete(key); elselocalCache.remove(key);/* * 得到 */public Object get(String key)Obje
9、ct rt = null;if (this.cacheCluster)rt = mc.get(key); elseElement element = null;tryelement = localCache.get(key); catch (CacheException cacheException)throw new DataRetrievalFailureException("Cache failure: "+ cacheException.getMessage();if (element != null)rt = element.getValue();return r
10、t;/* * 判断是否存在 * */public boolean exist(String key)if (this.cacheCluster)return mc.keyExists(key); elsereturn this.localCache.isKeyInCache(key);private void init()if (this.cacheCluster)String serverlist = cacheServerList.split(",");/Integer weights = this.split(cacheServerWeights); String t
11、emps = cacheServerWeights.split(",");Integer weights = new Integertemps.length;for (int t = 0; t < temps.length; t+)weightst = new Integer(tempst);/ initialize the pool for memcache servers SockIOPool pool = SockIOPool.getInstance();pool.setServers(serverlist);pool.setWeights(weights);p
12、ool.setInitConn(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
13、eAlg);pool.setHashingAlg(SockIOPool.NEW_COMPAT_HASH);pool.initialize();private void destory()if (this.cacheCluster)SockIOPool.getInstance().shutDown();4.Spring配置文件中配置memcache<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><prop
14、erty name="configLocation"><value>classpath:ehcache.xml</value></property></bean><bean id="localCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean"><property name="cacheManager"><ref local="cacheMa
15、nager" /></property><property name="cacheName"><value>org.taha.cache.LOCAL_CACHE</value></property></bean><bean id="cacheService" class="com.chinayes.usercenter.services.CacheService" init-method="init" destroy-method="destory"><property name="cacheServerList" value="127.0.0.1:11211" /><property nam
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 科技论文考试题及答案
- 考评员考试题及答案
- 餐具及厨具制作工标准化作业考核试卷及答案
- 物料输送及烟气净化工技能操作考核试卷及答案
- 酒师考试题及答案
- 冷却筛分工测试考核试卷及答案
- 隧道巡视养护工晋升考核试卷及答案
- 钒铁浸滤工岗位操作技能考核试卷及答案
- 2025年教师招聘之《幼儿教师招聘》通关试题库含答案详解【新】
- 戒毒条例考试题及答案
- 2025年乡村振兴应知应会考试题及答案
- 建筑工程岗位招聘笔试题与参考答案(某大型国企)2025年
- 第21章 二次根式 单元测试卷(含答案)-2025-2026学年数学华东师大版九年级上册
- 2025-2030中国废旧汽车拆解材料分类利用与循环经济园区建设报告
- 2025-2026学年教科版(2024)小学科学二年级上册(全册)每课教学反思
- 2025-2026学年人教鄂教版(2024)小学科学三年级上册(全册)教学设计(附目录P137)
- 2025版金融业务合同委托管理协议
- 部队手榴弹使用课件
- 2025江苏南京市麒麟科创园所属企业部分招聘8人备考试题及答案解析
- 社区网格员考试试题(含答案)
- 2025年宜都市总工会公开招聘乡镇工会协理员3人考试参考题库附答案解析
评论
0/150
提交评论