High concurrency: Memcached practice lesson 10th-code sharing in part of "Memcached Get data"

Source: Internet
Author: User

High concurrency: Memcached practice lesson 10th-code sharing in part of "Memcached Get data"
High concurrency: Memcached practice lesson 10th-code sharing of "Memcached Get data"


First, write a class related to the data to be stored:


public class Student  implements java.io.Serializable {public String Name;public int Age;}

Then write and read the Memcached client using java:


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); //. get () before (mcc. the set () operation has been completed and the System is returned. out. println ("set status:" +. 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 );}}

If you have any questions, please reply and discuss them together. Thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.