memcached Java calls, note points and basics

Source: Internet
Author: User


    • 1. Memcached Client Brief Introduction
      Memcached client currently has 3 types:
      Memcached Client for Java
      Spymemcached
    • Xmemcached
      The first two kinds of Http://pan.baidu.com/s/1dEMWfuD

      These three types of client-general perceptions are:
      Memcached Client for Java is more stable, earlier and wider than spymemcached;
      Spymemcached is more efficient than Memcached Client for Java;
      Xmemcached is more effective than spymemcache concurrency.
2.Java Call memcached use case 2.1 Memcached Client for Java use case
Import Com.whalin.memcached.memcachedclient;import Com.whalin.memcached.sockiopool;public class TestMemcached { public static void Main (string[] args) {/* Initialize Sockiopool, manage memcached connection pool */string[] Servers = {"120.143.23.240:12000"};/ /Note that the memcached here can be multiple sockiopool pool = sockiopool.getinstance ();p ool.setservers (servers);p Ool.setfailover (true); Pool.setinitconn (10);//Initial link number pool.setminconn (5);//minimum number of links pool.setmaxconn (250);//maximum number of links pool.setmaintsleep (30); Pool.setnagle (False);p Ool.setsocketto (3000);//Timeout Time Pool.setalivecheck (true);p ool.initialize ();/* Establish Memcachedclient instance */memcachedclient memcachedclient = new Memcachedclient (); for (int i = 0; i < 5; i++) {/* Add object to MEMCA Ched cache */boolean Success = Memcachedclient.set ("" + I, "hello!"); Boolean yes = Memcachedclient.set ("Yuanyirui", "ni shi yi ge da cai bi!"); * Object */string result = (String) memcachedclient.get ("" + i) from memcached cache by key value; System.out.println (String.Format ("Set (%d):%s", I, success)); System.out.println (String.Format ("Get (%d):%s", I, Result));}}} 
Each cache is a server; 2.2 spymemcached use case
Import Java.net.inetsocketaddress;import Java.util.concurrent.future;import net.spy.memcached.MemcachedClient; public class Mclientset {public static void main (string[] args) {try{/* establishes the Memcachedclient instance and specifies the IP address and port number of the memcached service */ memcachedclient mc = new Memcachedclient (New Inetsocketaddress ("120.143.23.240", 12000)); Future<boolean> B = null;/* will be the key value, the expiration time (in seconds), and the object to be cached set into memcached */b = Mc.set ("Yuanyirui", "", "ni shi yi ge da cai Nia O11 ");//object C = mc.get (" Yuanyirui ");//    System.out.println (c.tostring ()); if (B.get (). Booleanvalue () ==true) { Mc.shutdown ();//}}catch (Exception ex) {ex.printstacktrace ();}}}
3. Supplementary knowledge: five basic memcached commands add: Code style= "FONT-SIZE:14PX; Line-height:26px ">add   command does not add a key-value pair to the cache. If the key already exists in the cache, the previous value will remain the same and you will get a response; Replace: only if the replace key already exists command will not replace the key in the cache. If the key does not exist in the cache, a not_stored response will be accepted from the memcached server ; set: There is no data to add;Get : Get--nothing to say;Delete : used to delete any existing values in the memcached. You will use a key call delete and if the key exists in the cache, delete the value. Returns a NOT_FOUND message if it does not exist ; Summary: The above Chinese explanation is very clear. Use Add to avoid malicious attacks. Question: Is this how the second kill is designed:
4. Use note points:
Memcached key, to eliminate the use of space, and the length of control in 250 characters.
memcached value, to control the volume, must be less than 1MB, when necessary to use compression.
Failure time, 0 is permanent, maximum value must not exceed 30 days (2592000s), otherwise recalculation may be cached for only 1 seconds
Memcached only supports the LRU algorithm and is fully applicable to your needs.
Try not to throw a list of this heavy volume object into the memcached, transmission, storage will create bottlenecks.
Use a consistent hashing algorithm to improve multiple MEMCACEHD server utilization.
5.memcached basic Knowledge:5.1 default port:11211
telnet localhost 11211
5.2 Start Command:

Memcached-d-P 12000-u nobody-c 1024-m64

-P Listening Port
-L connected IP address, default is native
-D Start memcached service
-D Restart Restart memcached service
-D Stop|shutdown Close the running memcached service
-D Install memcached service
-d Uninstall Uninstall memcached service
-U Run as (only valid when running as root)
-m maximum memory usage, in megabytes. Default 64MB
-M running out of memory and returning an error instead of deleting an item
-c Maximum number of simultaneous connections, default is 1024
-F Block size growth factor, default is 1.25
-N Minimum allocated space, key+value+flags default is 48
-H Display Help

5.3 Views:

Ps-ef|grep memcached

5.4 Turn off memcached
  kill - 9   ' cat /var/run/memcached.pid '


memcached Java calls, note points and basics

Related Article

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.