spymemcache java的demo

来源:互联网 发布:对网络的认识和感受 编辑:程序博客网 时间:2024/05/21 10:03

最新学了spymemcache,发现网上关于memcache的学习资料不是很多,很多资料都是php的demo,没有找到java的demo,于是想自己写一个。

代码需要spymemcached和junit的jar包,需要安装memcache,以下附源码。

import net.spy.memcached.CASResponse;import net.spy.memcached.CASValue;import net.spy.memcached.MemcachedClient;import net.spy.memcached.internal.GetFuture;import net.spy.memcached.internal.OperationFuture;import org.junit.After;import org.junit.Before;import org.junit.Test;import java.io.IOException;import java.net.InetSocketAddress;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.Set;import java.util.concurrent.ExecutionException;import java.util.concurrent.Future;import java.util.concurrent.TimeUnit;import java.util.concurrent.TimeoutException;public class Main {    MemcachedClient client=null;    @Before    public void before(){        try {            client = new MemcachedClient(new InetSocketAddress("localhost",11211));        } catch (IOException e) {            System.out.println(e.getMessage());        }    }    @After    public void after(){        client.shutdown();    }    @Test    public void get(){        try{            client.add("name", 60, "小明");            System.out.println(client.get("name"));        }catch(Exception e){           e.printStackTrace();        }    }    @Test    public void getUserList(){        try{            //实体类需要序列化            User user1=new User();            user1.setId(1L);            user1.setName("张三");            user1.setAge(10);            User user2=new User();            user2.setId(2L);            user2.setName("李四");            user2.setAge(12);            List<User> list=new ArrayList<User>();            list.add(user1);            list.add(user2);            client.add("userList",60,list);            List<User> returnUserList=(List<User>)client.get("userList");            if(returnUserList!=null&&returnUserList.size()>0){                for(User user:returnUserList){                    System.out.println(user.toString());                }            }        }catch(Exception e){            e.printStackTrace();        }    }    @Test    public void asyncGet(){        client.set("name", 60, "小明");        //异步获取        GetFuture<Object> f=client.asyncGet("name");        try {            //f.get的参数是指获取数据的时间上限,如果超出该时间,则会抛出异常。            System.out.println(f.get(5, TimeUnit.SECONDS));        } catch (InterruptedException e) {            //没有catch到异常,则可以取消这次请求。            f.cancel(true);            System.out.println(e.getMessage());        } catch (TimeoutException e) {            f.cancel(true);            System.out.println(e.getMessage());        } catch (ExecutionException e) {            f.cancel(true);            System.out.println(e.getMessage());        }catch(Exception e){            System.out.println(e.getMessage());        }    }    @Test    public void cas(){        try{            client.set("title",60,"hello");            retry("title"," world",2);        }catch(Exception e){            e.printStackTrace();        }    }    public void retry(String key,String value,int type){        CASValue<Object> casValue=client.gets(key);        System.out.println(casValue.getCas()+"----"+casValue.getValue());        CASResponse casResponse=null;        if(type==1){            //覆蓋值        }else if(type==2){            //追加值            casResponse=client.cas("title",casValue.getCas(),casValue.getValue()+value);        }        if(casResponse.toString().equals("OK")){            CASValue<Object> casValue2=client.gets("title");            System.out.println(casValue2.getCas()+"---"+casValue2.getValue());        }else{           retry(key,value,type);        }    }    @Test    public void append(){        try{            client.set("content",60,"hello");            CASValue<Object> casValue=client.gets("content");            OperationFuture<Boolean> operationFuture=client.append(casValue.getCas(),"content"," world");            System.out.println(operationFuture.get()+"---"+operationFuture.getKey()+"---"+operationFuture.getStatus().isSuccess()+"---"+operationFuture.isDone());            if(!operationFuture.get().equals(true)){                append();            }        }catch(Exception e){            e.printStackTrace();        }    }    @Test    public void asyncCAS(){        try{            client.set("asynccas",60,"hello");            CASValue<Object> casValue=client.gets("asynccas");            System.out.println(casValue.getCas()+"---"+casValue.getValue());            Future<CASResponse> future= client.asyncCAS("asynccas",casValue.getCas(),"hi");            CASResponse casResponse=future.get();            System.out.println(casResponse.toString());            if(!casResponse.toString().equals("OK")){                asyncCAS();            }else{                casValue=client.gets("asynccas");                System.out.println(casValue.getCas()+"---"+casValue.getValue());            }        }catch(Exception e){            e.printStackTrace();        }    }    @Test    public void decr(){        try{            //注意值需为String类型,如何为int类型会将其转化为字符串,不能调用incr decr方法。            client.set("age",60,"30");            CASValue<Object> casValue=client.gets("age");            System.out.println(casValue.getCas()+"--"+casValue.getValue());            //值必須大於等於0,否則會報invalid numeric delta argument            //client.decr("age",-5);            client.decr("age",5);            casValue=client.gets("age");            System.out.println(casValue.getCas()+"--"+casValue.getValue());            //第三個為def,如果decr操作失敗,則將def的值加到value上。            client.decr("age",3,4L);            casValue=client.gets("age");            System.out.println(casValue.getCas()+"--"+casValue.getValue());        }catch(Exception e){            e.printStackTrace();        }    }    @Test    public void asyncDecr(){        try{            client.set("age",60,"30");            OperationFuture<Long> operationFuture=client.asyncDecr("age",5);            System.out.println(operationFuture.get()+"---"+operationFuture.getKey()+"---"+operationFuture.getStatus().isSuccess()+"---"+operationFuture.isDone());            if(!operationFuture.getStatus().isSuccess()){                asyncDecr();            }        }catch(Exception e){            e.printStackTrace();        }    }    @Test    public void incr(){        try{            //注意值需为String类型,如何为int类型会将其转化为字符串,不能调用incr decr方法。            client.set("age",60,"30");            CASValue<Object> casValue=client.gets("age");            System.out.println(casValue.getCas()+"--"+casValue.getValue());            //值必須大於等於0,否則會報invalid numeric delta argument            //client.incr("age",-5);            client.incr("age",5);            casValue=client.gets("age");            System.out.println(casValue.getCas()+"--"+casValue.getValue());            //第三個為def,如果incr操作失敗,則將def的值加到value上。            client.incr("age",3,4L);            casValue=client.gets("age");            System.out.println(casValue.getCas()+"--"+casValue.getValue());        }catch(Exception e){            e.printStackTrace();        }    }    @Test    public void asyncIncr(){        try{            client.set("age",60,"30");            OperationFuture<Long> operationFuture=client.asyncIncr("age",5);            System.out.println(operationFuture.get()+"---"+operationFuture.getKey()+"---"+operationFuture.getStatus().isSuccess()+"---"+operationFuture.isDone());            if(!operationFuture.getStatus().isSuccess()){                asyncIncr();            }        }catch(Exception e){            e.printStackTrace();        }    }    @Test    public void prepend(){        try{            client.set("prepend",60," world");            CASValue<Object> casValue=client.gets("prepend");            OperationFuture<Boolean> operationFuture=client.prepend(casValue.getCas(),"prepend","hello");            casValue=client.gets("prepend");            System.out.println(casValue.getValue());            if (!operationFuture.get().equals(true)) {                prepend();            }        }catch(Exception e){            e.printStackTrace();        }    }    @Test    public void flush(){        try{            client.set("flush",60,"hello");            //清除服務器所有緩存            client.flush();            //在10秒后清除服務器所有緩存//            client.flush(10);            CASValue<Object> casValue=client.gets("flush");            System.out.println(casValue==null);        }catch(Exception e){            e.printStackTrace();        }    }    @Test    public void getBulk(){        try{            client.set("bulk1",60,"111");            client.set("bulk2",60,"222");            client.set("bulk3",60,"333");            List<String> keyList=new ArrayList<String>();            keyList.add("bulk1");            keyList.add("bulk2");            keyList.add("bulk3");            Map<String,Object> map=client.getBulk(keyList);            if(map!=null){                Set<String> keySet=map.keySet();                for(String key:keySet){                    System.out.println(key+"---"+map.get(key));                }            }        }catch(Exception e){            e.printStackTrace();        }    }    public static void main(String[] args) {    }}