高并发之Memcached实战第10课-“Memcached Get获取数据”部分代码分享
高并发之Memcached实战第10课-“Memcached Get获取数据”部分代码分享
先写个要存的数据的相关类:
public class Student implements java.io.Serializable { public String Name; public int Age; }
然后用java写Memcached客户端写和读操作:
import java.io.IOException; import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutionException; import net.spy.memcached.MemcachedClient; import net.spy.memcached.internal.OperationFuture; public class MemcachedJava { public static void main(String[] args) throws IOException, InterruptedException, ExecutionException { //Connecting to Memcached server on localhost List<InetSocketAddress> list=new ArrayList<InetSocketAddress>(); list.add(new InetSocketAddress("127.0.0.1", 11211)); MemcachedClient mcc = new MemcachedClient(list); System.out.println("Connection to server sucessfully"); Student s=new Student(); s.Name="Jumping"; s.Age=29; OperationFuture<Boolean> of=mcc.set("s1", 900, s); // of.get() 确保之前(mcc.set())操作已经结束,并且获取结果 System.out.println("set status:" +of.get()); Student sout=(Student)mcc.get("s1"); System.out.println("Get from Cache:" + sout.Name); Student s2=new Student(); s2.Name="Jumping2"; s2.Age=29; Student s3=new Student(); s3.Name="Jumping3"; s3.Age=29; List<Student> ss=new ArrayList<Student>(); ss.add(s2); ss.add(s3); OperationFuture<Boolean> of2=mcc.set("ss", 900, ss); System.out.println("set ss status:" +of2.get()); ArrayList<Student> s2out=(ArrayList<Student>)mcc.get("ss"); System.out.println("Get from ss:" + ((Student)s2out.toArray()[0]).Name); System.out.println("Get from ss:" + ((Student)s2out.toArray()[1]).Name); System.exit(0); } }
碰到任何问题,请回复,共同讨论,谢谢!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。