您的位置:首页 > 数据库 > Memcache

分布式缓存- Spring中Memcache的使用

2016-12-19 16:51 519 查看
1、安装Memcached

按照官网文档安装

2、启动Memcache服务

按照官方文档启动服务

3、新建Maven项目



4、配置pom.xml,添加Spring和Memcache的依赖

<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>com.whalin</groupId>
<artifactId>Memcached-Java-Client</artifactId>
<version>3.0.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>


5、在Spring应用程序上下文文件中配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<bean id="memCachedPool" class="com.whalin.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize" destroy-method="shutDown">
<constructor-arg>
<value>memCachedPool</value>
</constructor-arg>
<property name="servers">
<list>
<value>192.168.1.234:11211</value>
</list>
</property>
<property name="initConn">
<value>20</value>
</property>

<property name="minConn">
<value>10</value>
</property>

<property name="maxConn">
<value>50</value>
</property>

<property name="maintSleep">
<value>3000</value>
</property>

<property name="nagle">
<value>false</value>
</property>

<property name="socketTO">
<value>3000</value>
</property>
</bean>
<bean id="memCachedClient" class="com.whalin.MemCached.MemCachedClient">
<constructor-arg>
<value>memCachedPool</value>
</constructor-arg>
</bean>
</beans>
6、编写测试代码

public static void main(String[] args) {
// System.out.println( "Hello World!" );
ApplicationContext ctx = new ClassPathXmlApplicationContext("xml/applicationContext.xml");
MemCachedClient memCachedClient = ctx.getBean("memCachedClient", MemCachedClient.class);
/************************************ 配置Memcached **************************************/
SockIOPool sockIOPool = SockIOPool.getInstance();

sockIOPool.setServers(new String[] { "127.0.0.1:11211" });// 设置memcached服务器地址
sockIOPool.setWeights(new Integer[] { 3 }); // 设置每个MemCached服务器权重
sockIOPool.setFailover(true); // 当一个memcached服务器失效的时候是否去连接另一个memcached服务器.
sockIOPool.setInitConn(10); // 初始化时对每个服务器建立的连接数目
sockIOPool.setMinConn(10); // 每个服务器建立最小的连接数
sockIOPool.setMaxConn(100); // 每个服务器建立最大的连接数
sockIOPool.setMaintSleep(30); // 自查线程周期进行工作,其每次休眠时间
sockIOPool.setNagle(false); // Socket的参数,如果是true在写数据时不缓冲,立即发送出去。Tcp的规则是在发送一个包之前,包的发送方会等待远程接收方确认已收到上一次发送过来的包;这个方法就可以关闭套接字的缓存——包准备立即发出。
sockIOPool.setSocketTO(3000); // Socket阻塞读取数据的超时时间
sockIOPool.setAliveCheck(true); // 设置是否检查memcached服务器是否失效
sockIOPool.setMaxIdle(1000 * 30 * 30); // 设置最大处理时间
sockIOPool.setSocketConnectTO(0); // 连接建立时对超时的控制

sockIOPool.initialize(); // 初始化连接池

memCachedClient = new MemCachedClient();
memCachedClient.setPrimitiveAsString(true); // 是否将基本类型转换为String方法

if (memCachedClient != null) {
System.out.println("----------------设置缓存值------------");
if(memCachedClient.add("testkey", "key 为1的值")){
System.out.println("----------------获取缓存值------------");
System.out.println("key为1的值为:"+memCachedClient.get("testkey"));
}else {
System.out.println("添加键值失败");
}
}
}

7、执行结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐