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

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、执行结果



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RonTech

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值