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

[经验分享] spring中配置memcache

[复制链接]

尚未签到

发表于 2015-11-18 08:12:12 | 显示全部楼层 |阅读模式
  

一. 安装memcache  
  1.下载memcache
  下载地址 http://memcached.org/  最新版本v1.4.15
  下载libevent  地址http://libevent.org/  (通信程序,memcache所依赖的异步程序通知库)
  linux安装命令:
  先安装libevent
  
# tar zxvf libevent-1.4.9-stable.tar.gz
# cdlibevent-1.4.9-stable
# ./configure--prefix=/usr
# make
# make install  测试libevent是否安装成功:
  #ll /usr/lib| grep libevent
  运行memcached
  # /usr/local/bin/memcached -p 11211 –d  -m 512 -vv
  检查是否正常启动
  # telnet localhost 11211
  2.windows下安装memcache
  
  安装完成后服务中会出现
  启动服务后就可以连接memcache服务器了
  
  
  一些基本的使用就不在这里介绍了。接下来进入主题在spring中配置memcache。
  
  二、在spring中配置memcache
  1、新建配置文件 :spring-memcache.xml的spring配置文件
  
  
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot; xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;
xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd&quot;>
<bean id=&quot;memcachedPool&quot; class=&quot;com.danga.MemCached.SockIOPool&quot;
factory-method=&quot;getInstance&quot; init-method=&quot;initialize&quot; destroy-method=&quot;shutDown&quot;>

<constructor-arg>
<value>memCachedPool</value>
</constructor-arg>
<property name=&quot;servers&quot;>
<list>
<value>${memcache.server}</value>
</list>
</property>

<property name=&quot;initConn&quot;>
<value>${memcache.initConn}</value>
</property>
<property name=&quot;minConn&quot;>
<value>${memcache.initConn}</value>
</property>

<property name=&quot;maxConn&quot;>
<value>${memcache.initConn}</value>
</property>
<property name=&quot;maintSleep&quot;>
<value>${memcache.maintSleep}</value>
</property>

<property name=&quot;nagle&quot;>
<value>${memcache.nagle}</value>
</property>
<property name=&quot;socketTO&quot;>
<value>${memcache.socketTO}</value>
</property>
</bean>
<bean id=&quot;memCachedClient&quot; class=&quot;com.danga.MemCached.MemCachedClient&quot;>
<constructor-arg>
<value>memCachedPool</value>
</constructor-arg>
</bean>
</beans>


  
  


  2、在web.xml中配置新建的文件
  <!-- 配置spring的监听器,加载Spring配置文件-->
  

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext,
classpath*:spring-*.xml,
classpath*:applicationContext-*.xml
</param-value>
</context-param>

3、修改spring的配置文件applicationContext.xml,并添加一个spring工具类,便于以后调用  
  <bean
class=&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;>
<property name=&quot;locations&quot;>
<list>
<value>classpath:memcached.properties</value>
<value>classpath:mysql.properties</value>
</list>
</property>
</bean>

4、创建memcache的资源文件和spring工具类

memcache.properties
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


  
  

spring工具类:
  

package com.hope.common;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContextapplicationContext =new ClassPathXmlApplicationContext(&quot;spring-memcache.xml&quot;);
public void setApplicationContext(ApplicationContext applicationContext){
SpringContextHolder.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext(){
checkApplicationContext();
return applicationContext;
}
@SuppressWarnings(&quot;unchecked&quot;)
public static <T> T getBean(String name){
checkApplicationContext();
return (T) applicationContext.getBean(name);
}
@SuppressWarnings(&quot;unchecked&quot;)
public static <T> T getBean(Class<T> clazz){
checkApplicationContext();
Map beanMaps = applicationContext.getBeansOfType(clazz);
if (beanMaps != null && !beanMaps.isEmpty()) {
return (T) beanMaps.values().iterator().next();
} else {
return null;
}
}
private static void checkApplicationContext(){
if (applicationContext == null) {
throw new IllegalStateException(
&quot;applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder&quot;);
}
}
}

  
  
  



配置memcache自己的工具类:
  

import com.danga.MemCached.MemCachedClient;
public class MemcacheUtil {
public static MemCachedClient getMemCachedClient(){
return SpringContextHolder.getBean(&quot;memCachedClient&quot;);
}
}


  
  



最后用junit测试是否成功:
  

package com.hope;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.danga.MemCached.MemCachedClient;
public class TestMemcache {
MemCachedClient memCachedClient;
@Before
public void beforeTest(){
ApplicationContext atx = new ClassPathXmlApplicationContext(&quot;spring-memcache.xml&quot;);
memCachedClient = (MemCachedClient)atx.getBean(&quot;memCachedClient&quot;);
}
@Test
public void TestMem(){
memCachedClient.set(&quot;name&quot;, &quot;liu&quot;);
System.out.println(memCachedClient.get(&quot;name&quot;));
}
}

  



版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 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-140475-1-1.html 上篇帖子: redis、memcache、mongoDB 对比 下篇帖子: 关于memcache 测试
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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