设为首页 收藏本站
查看: 1399|回复: 0

[经验分享] memcache与spring集成 实例

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-8-31 08:25:40 | 显示全部楼层 |阅读模式
  学习了memcache,这是个好东西,分享一下自己的小实例,也方便以后查找使用
  一、前期准备
  1)  下载memcached服务端memcached-1.2.6-win32-bin.zip,地址:http://code.jellycan.com/memcached/


2)  下载java版客户端 java_memcached-release_2.6.1.zip

3)  解压缩memcached-1.2.6-win32-bin.zip到指定目录,例如:D:\memcached-1.2.6-win32 ,在终端(即cmd命令行界面)



D:\memcached-1.2.6-win32\memcached.exe -d install

D:\memcached\memcached.exe -d start



这样memcache就会作为windows系统服务在每次开机时启动memcache服务。



常用命令



-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB
-M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25
-n 最小分配空间,key+value+flags默认是48
-h 显示帮助







二、实例代码

spring-memcache.xml  配置pool和memcache客户端




1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <beans xmlns="http://www.springframework.org/schema/beans"
4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5     xmlns:context="http://www.springframework.org/schema/context"
6     xmlns:aop="http://www.springframework.org/schema/aop"
7     xmlns:tx="http://www.springframework.org/schema/tx"
8     xsi:schemaLocation="http://www.springframework.org/schema/beans
9     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
10     http://www.springframework.org/schema/context
11     http://www.springframework.org/schema/context/spring-context-2.5.xsd
12     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
13     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
14
15
16     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
17         <property name="locations">
18             <list>
19                 <value>classpath:properties/memcache.properties</value>
20             </list>
21         </property>
22     </bean>
23
24     <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"
25         factory-method="getInstance" init-method="initialize"
26         destroy-method="shutDown">
27
28         <constructor-arg>
29             <value>memCachedPool</value>
30         </constructor-arg>
31         
32         <property name="servers">
33             <list>
34                 <value>${memcache.server}</value>
35             </list>
36         </property>
37         
38         <property name="initConn">
39             <value>${memcache.initConn}</value>
40         </property>
41         
42         <property name="minConn">
43             <value>${memcache.minConn}</value>
44         </property>
45
46         <property name="maxConn">
47             <value>${memcache.maxConn}</value>
48         </property>
49
50         <property name="maintSleep">
51             <value>${memcache.maintSleep}</value>
52         </property>
53
54         <property name="nagle">
55             <value>${memcache.nagle}</value>
56         </property>
57
58         <property name="socketTO">
59             <value>${memcache.socketTO}</value>
60         </property>
61     </bean>
62
63     <bean id="memCachedClient" class="com.danga.MemCached.MemCachedClient">
64         <constructor-arg>
65             <value>memCachedPool</value>
66         </constructor-arg>
67     </bean>
68
69 </beans>
  


memcache.properties memcache的连接属性 这是本机做服务器的,如果是其它机器,换ip 端口即可




memcache.server=127.0.0.1:11211
memcache.initConn=20
memcache.minConn=10
memcache.maxConn=50
memcache.maintSleep=3000
memcache.nagle=false
memcache.socketTO=3000
  
  TestMemcache.java测试类   用的是junit4



1 package com.pis.memcache;
2
3 import org.junit.Before;
4 import org.junit.Test;
5 import org.springframework.context.ApplicationContext;
6 import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8 import com.danga.MemCached.MemCachedClient;
9
10 public class TestMemcache {
11     MemCachedClient memCachedClient;
12     @Before
13     public void beforeTest(){
14         
15         ApplicationContext atx = new ClassPathXmlApplicationContext("/spring/spring-memcache.xml");
16         memCachedClient = (MemCachedClient)atx.getBean("memCachedClient");
17     }
18     
19     
20     @Test
21     public void TestMem(){
22         memCachedClient.set("name", "han");
23         
24         System.out.println(memCachedClient.get("name"));
25     }
26     
27     
28     
29 }
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-106535-1-1.html 上篇帖子: Memcache学习第二课:在C#项目中应用Memcache 下篇帖子: Memcache 协议 (译)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表