NET Spy memcached using demo

Source: Internet
Author: User
Tags memcached

Package Memcached;import Java.io.ioexception;import Java.net.inetsocketaddress;import Net.spy.memcached.memcachedclient;public class Client {private final int expiredseconds = 100;private final Inetsocketaddress Server = new Inetsocketaddress ("127.0.0.1", 10101);p rivate memcachedclient memcachedclient;public void Init () {try {memcachedclient = new memcachedclient (server);} catch (IOException e) {e.printstacktrace ();}} public void put (String key, Object obj) {memcachedclient.set (key, expiredseconds, obj);} public void Delete (String key) {memcachedclient.delete (key);} Public Object get (String key) {return memcachedclient.get (key);} public static void Main (String args[]) {Client c = new Client (), C.init (); Long begin = System.currenttimemillis (); for (int i =0;i<100;i++) {c.put (i+ "", I); System.out.println (C.get (i+ "));} System.out.println ((System.currenttimemillis ()-begin) + "MS");}}
The first is to set the expiration time, in seconds, note that when put, the value as a parameter of set, but if the value is greater than 30 days of the number of seconds, it will be the Unix time to set the expiration time instead of offset.
The second line is a local memcached server address and port, call Memcacedclient instantiate a Memcachedclient object, the following is the custom Put,get,delete common method can be
The direct results are as follows:
2014-08-22 09:35:01.891 INFO net.spy.memcached.MemcachedConnection:Added {QA sa=/127.0.0.1:10101, #Rops =0, #Wops = 0, # Iq=0, Toprop=null, Topwop=null, towrite=0, interested=0} to connect queue2014-08-22 09:35:01.902 WARN Net.spy.memcached.MemcachedConnection:Could not redistribute to another node, retrying primary node for 0.2014-08-22 09: 35:01.904 WARN net.spy.memcached.MemcachedConnection:Could not redistribute to another node, retrying primary node for 0 .012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 656667686970717273747576777879808182838485868788899091929394959697989992ms


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.