我是靠谱客的博主 优秀向日葵,最近开发中收集的这篇文章主要介绍Java与Memcached连接(三):Add;Replace;Append;CAS;Get;Gets;Delete;Incr;Decr操作实例,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
public class MainTest {
@Test
public void testAdd() {
//添加
try {
MemcachedClient mcc2 = new MemcachedClient(/*addr*/new InetSocketAddress("192.168.1.102", 11211));
System.out.println("服务器连接成功");
Future fo = mcc2.set("runoob", 900, "memcached");
System.out.println("set status:"+fo.get());
System.out.println("runoob value:"+mcc2.get("runoob"));
//add添加已有的key
fo = mcc2.add("runoob", 900, "addtest");
System.out.println("add status:"+fo.get());
System.out.println("runoob value:"+mcc2.get("runoob"));
//add添加新key
fo = mcc2.add("addtest", 900, "addtest");
System.out.println("add status:"+fo.get());
System.out.println("add value:"+mcc2.get("addtest"));
if(mcc2 != null) {
mcc2.shutdown();
}
}
catch(IOException e){
System.out.println("服务器连接失败");
System.out.println("IOException");
System.out.println(e.getMessage());
}
catch(Exception e){
System.out.println("服务器连接失败");
System.out.println("Exception");
System.out.println(e.getMessage());
}
}
@Test
public void testReplace() {
//替换
try {
MemcachedClient mcc2 = new MemcachedClient(/*addr*/new InetSocketAddress("192.168.1.102", 11211));
System.out.println("服务器连接成功");
Future fo = mcc2.set("runoob", 900, "memcached");
System.out.println("set status:"+fo.get());
System.out.println("runoob value:"+mcc2.get("runoob"));
fo = mcc2.replace("runoob", 900, "memcachedplus");
System.out.println("replace status:"+fo.get());
System.out.println("runoob value:"+mcc2.get("runoob"));
if(mcc2 != null) {
mcc2.shutdown();
}
}
catch(IOException e){
System.out.println("服务器连接失败");
System.out.println("IOException");
System.out.println(e.getMessage());
}
catch(Exception e){
System.out.println("服务器连接失败");
System.out.println("Exception");
System.out.println(e.getMessage());
}
}
@Test
public void testAppend() {
//后缀
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("192.168.1.102",11211));
System.out.println("Connection successful");
Future fo = mcc.set("runoob", 900, "memcached");
System.out.println("set status"+fo.get());
System.out.println("runoob value"+mcc.get("runoob"));
//append
fo = mcc.append(900, "runoob", " Java");
System.out.println("append status:"+fo.get());
System.out.println("runoob value:"+mcc.get("runoob"));
if(mcc != null) {
mcc.shutdown();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testPrepend() {
//前缀
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("192.168.1.102",11211));
System.out.println("Connection successful");
Future fo = mcc.set("runoob", 900, "memcached");
System.out.println("set status"+fo.get());
System.out.println("runoob value"+mcc.get("runoob"));
//prepend
fo = mcc.prepend(900, "runoob", "Java ");
System.out.println("prepend status:"+fo.get());
System.out.println("runoob value:"+mcc.get("runoob"));
if(mcc != null) {
mcc.shutdown();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testCAS() {
//令牌
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("192.168.1.102",11211));
System.out.println("Connection successful");
Future fo = mcc.set("runoob", 900, "memcached");
System.out.println("set status"+fo.get());
System.out.println("runoob value"+mcc.get("runoob"));
//CAS
CASValue casvalue = mcc.gets("runoob");
System.out.println("cas value:"+casvalue);
CASResponse casResponse = mcc.cas("runoob", casvalue.getCas(), "new memcached");
System.out.println("cas status:"+casResponse);
System.out.println("runoob value:"+mcc.get("runoob"));
if(mcc != null) {
mcc.shutdown();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testGet() {
//获取
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("192.168.1.102",11211));
System.out.println("Connection successful");
Future fo = mcc.set("runoob", 900, "memcached");
System.out.println("set status:"+fo.get());
System.out.println("runoob value:"+mcc.get("runoob"));
if(mcc != null) {
mcc.shutdown();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testGets() {
//获取令牌
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("192.168.1.102",11211));
System.out.println("Connection successful");
Future fo = mcc.set("runoob", 900, "memcached");
System.out.println("set status:"+fo.get());
System.out.println("runoob value:"+mcc.get("runoob"));
CASValue casValue = mcc.gets("runoob");
System.out.println("cas value:"+casValue);
CASResponse casResponse = mcc.cas("runoob", casValue.getCas(), "aaaaa");
System.out.println("cas status:"+fo.get());
System.out.println("casRunoob value:"+mcc.get("runoob"));
if(mcc != null) {
mcc.shutdown();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testDelete1() {
//删除
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("192.168.1.102",11211));
System.out.println("Connection successful");
Future fo = mcc.set("runoob", 900, "memcached");
System.out.println("set status:"+fo.get());
System.out.println("runoob value:"+mcc.get("runoob"));
fo = mcc.delete("runoob");
System.out.println("Delete status:"+fo.get());
System.out.println("runoob value:"+mcc.get("runoob"));
mcc.shutdown();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testDelete2() {
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("192.168.1.102",11211));
System.out.println("Connection successful");
Future fo = mcc.set("runoob", 900, "memcached");
Object o1 = mcc.get("runoob");
System.out.println("o1:"+o1);
fo = mcc.delete("runoob");
System.out.println("Delete Value:"+fo.get());
mcc.set("runoob", 900, "memcached");
Object o2 = mcc.get("runoob");
System.out.println("o2:"+o2);
System.out.println("比较:"+o1==o2);
//结论:值和对象都删除;
// ==地址比较 equals 值比较
if(mcc != null) {
mcc.shutdown();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testIncrDecr() {
//增加减少数据
try {
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("192.168.1.102",11211));
System.out.println("Connecation successful");
Future fo = mcc.set("runoob", 900, "10");
System.out.println("set status:"+fo.get());
System.out.println("runoob value:"+mcc.get("runoob"));
long incr = mcc.incr("runoob", 5);
long decr = mcc.decr("runoob", 10);
System.out.println("update before value:"+mcc.get("runoob"));
System.out.println("incr After vlaue:"+incr);
System.out.println("decr After vlaue:"+decr);
if(mcc != null) {
mcc.shutdown();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
最后
以上就是优秀向日葵为你收集整理的Java与Memcached连接(三):Add;Replace;Append;CAS;Get;Gets;Delete;Incr;Decr操作实例的全部内容,希望文章能够帮你解决Java与Memcached连接(三):Add;Replace;Append;CAS;Get;Gets;Delete;Incr;Decr操作实例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复