本文共 5341 字,大约阅读时间需要 17 分钟。
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载
fastjson-1.2.3
,slf4j-api-1.7.5
,xmemcached-2.3.2
main方法中加入
MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("127.0.0.1:11211")); builder.setSessionLocator(new KetamaMemcachedSessionLocator()); try { MemcachedClient memcachedClient =builder.build(); //在这里写入测试代码 memcachedClient.shutdown(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (MemcachedException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); }
memcachedClient.set("testTime",2,"testTimeValue"); String testTime = memcachedClient.get("testTime"); System.out.println("testTime = " + testTime); TimeUnit.SECONDS.sleep(4); System.out.println("4s 过去了"); String valueExist = memcachedClient.get(testTime); System.out.println("valueExist = " + valueExist);
输出:
testTime = testTimeValue 4s 过去了 valueExist = null
//存储,然后关闭掉服务memcachedClient.set("test",0,"testValue");String testTime=memcachedClient.get("test");System.out.println("testTime="+testTime);System.out.println("存储成功");
输出:
testTime = testValue 存储成功
//关闭掉服务后的重启String testTime = memcachedClient.get("test");System.out.println("testTime = " + testTime);System.out.println("取值失败");
输出:
testTime = null 取值失败
memcachedClient.set("test",0,"testValue");String getVal = memcachedClient.get("test");System.out.println("getVal = " + getVal);memcachedClient.delete("test");System.out.println("after delete ...");getVal = memcachedClient.get("test");System.out.println("getVal = " + getVal);
输出:
getVal = testValue after delete ... getVal = null
//三个参数,第一个指定键,第二个指定递增的幅度大小,第三个指定当key不存在的情况下的初始值for (int i = 0; i < 5; i++) { memcachedClient.incr("博客的赞",1,20); String point = memcachedClient.get("博客的赞"); System.out.println("point = " + point);}
输出:
point = 20 point = 21 point = 22 point = 23 point = 24
关于incr的用法,值得警惕的是,它的值虽然看起来是一个数字,实际上正如代码中的String point = memcachedClient.get("博客的赞");
其实是一个字符串,所以会出现如下错误memcachedClient.set("博客的赞1",0,10);int str = memcachedClient.get("博客的赞1");System.out.println("str1 = " + str);memcachedClient.incr("博客的赞1",2,22);str = memcachedClient.get("博客的赞1");System.out.println("str2 = " + str);
输出:
net.rubyeye.xmemcached.exception.MemcachedClientException: cannot increment or decrement non-numeric value,key=博客的赞1 at net.rubyeye.xmemcached.command.Command.decodeError(Command.java:267) .. at com.google.code.yanf4j.nio.impl.NioController.onRead(NioController.java:157) at com.google.code.yanf4j.nio.impl.Reactor.dispatchEvent(Reactor.java:323) at com.google.code.yanf4j.nio.impl.Reactor.run(Reactor.java:180) str1 = 10
输出的顺序不同,注意输出的异常栈信息的第一条和后面的几条就指明nio.impl.Reactor.run,线程的,这儿就不深入展开了
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:spring-config.xml")public class TestMem { @Autowired private MemcachedClient memcachedClient; @Test public void test1(){ //测试代码部分 }}
memcachedClient.set("springData",3,"dataVal");String str = memcachedClient.get("springData");System.out.println("str = " + str);
输出:
str = dataVal
还可以存储对象,不过该对象必须实现Serializable接口,不然会报错java.io.NotSerializableException: Teacher
,
实现接口后
import lombok.Data;import java.io.Serializable;@Datapublic class Teacher implements Serializable { private int age; private String name;}
关于@Data关我在另一篇博客中有介绍
Teacher teacher = new Teacher();teacher.setAge(3);teacher.setName("23");memcachedClient.set("te", 0, teacher);Teacher teacher1 = memcachedClient.get("te");System.out.println("teacher1 = " + teacher1);
输出:
teacher1 = Teacher(age=3, name=23)
4.0.0 com.selton DemoMemSpring 1.0 org.projectlombok lombok 1.18.0 provided org.springframework spring-context 4.3.11.RELEASE com.googlecode.xmemcached xmemcached 2.0.0 org.springframework spring-context org.springframework spring-test 4.3.11.RELEASE junit junit 4.12 test
127.0.0.1:11211
1
转载地址:http://xusoa.baihongyu.com/