高并发之Memcached实战第10课-“Memcached Get获取数据”部分代码分享
2015-03-26 23:08
323 查看
高并发之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); } }
碰到任何问题,请回复,共同讨论,谢谢!
相关文章推荐
- 高并发之Memcached实战第10课-“Memcached Get获取数据”部分代码分享2
- 【分享】我们用了不到200行代码实现的文件日志系统,极佳的IO性能和高并发支持,附压力测试数据
- 500G python web、爬虫、数据分析、机器学习、大数据、前端实战项目视频代码免费分享
- 大恶人吉日嘎拉之走火入魔闭门造车之.NET疯狂架构经验分享系列之(一)后台控制逻辑代码部分
- treeview 控件动态绑定数据部分实例代码
- C# 写的 CPU 浮点运算测试工具.并且还有获取CPU种种数据的方法(图)(代码)
- asp下利用XMLHTTP 从其他页面获取数据的代码
- AjaxPro.NET实现TextBox智能获取服务端数据功能(Asp.net 2.0)(示例代码下载)
- 【python实战】获取双色球开奖数据之简易版
- treeview 控件动态绑定数据部分实例代码
- vbs xmldom初次实战获取QQ签名的代码
- InfoPath开发实战(3):获取SQL Server中的数据
- 用代码获取SharePoint站点中InfoPath表单的数据
- 后置代码中获取前台SqlDataSource1控件生成的数据
- php 表单数据的获取代码
- 使用IdHttp获取网页代码出现的问题,只能得到部分代码,为什么,请大家帮忙
- 用代码获取SharePoint站点中InfoPath表单的数据
- [黑客技术][分享]直接获取access、SQL Server等数据库数据
- AjaxPro.NET实现TextBox智能获取服务端数据功能(Asp.net 2.0)(示例代码下载) 转载
- InfoPath开发实战(2):获取XML文件中的数据