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

来源:互联网 发布:java filedialog 编辑:程序博客网 时间:2024/04/28 16:37

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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://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 0
原创粉丝点击