高並發之Memcached實戰第10課-“Memcached Get擷取資料”部分代碼分享,memcached實戰

來源:互聯網
上載者:User

高並發之Memcached實戰第10課-“Memcached Get擷取資料”部分代碼分享,memcached實戰
高並發之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);        } }

碰到任何問題,請回複,共同討論,謝謝!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.