(1) memcached application

Source: Internet
Author: User

(a) memcached introduction

(1) memcached is a distributed cache tool. We often deal with the caching technology, is the browser's own cache, we know that such as images, CSS, JS and other static files can be cached by the browser. When we visit the same Web page again, these cached files go directly to the browser to fetch instead of the request server. Reduce the pressure on the server.

(2) The so-called static files, as the name implies are some infrequently updated files; But the content of our site is dynamic, so-called dynamic, that is, these data are stored in the database, each request to the database to obtain. Requesting a database is also a disk operation and is time consuming and laborious. Although dynamic, there are some things that are not changed in a short period of time, and the data can be completely cached. Category navigation like shopping sites. Memcached is playing such a role.

(3) memcached is distributed, that is, the memcached Server software can be installed in the cluster servers, or a server installed several memcached. These memcached servers are co-ordinated to handle requests. The so-called synergy, that is, each memcached will be assigned a weight, if the weight of a large number of access to the probability is also large, if the weight value is less likely to be accessed.

(ii) memcached service-side installation

(1) The memcached server is written in C language. On the official website only gave the version of Linux, very hard to search the Internet to the Win32 version, links published as follows:

win32_memcached Server Software Download

(2) Unzip this compressed package and come out with a "deer Head" Memcached.exe file, yes! That's it. Run it, a window will appear, the window has been opened to indicate that the memcached service has been running, then it can receive the client's request. The client is free to play, can be implemented in a variety of languages, this article is introduced in Java.

Check on the Internet, now the more popular Java client one is Ali's classmate wrote, the other is written by the foreigner, by the way on git downloaded it down to look at the source code. After the source code will be introduced, now only say the application.

This only describes the memcached Java client written by the foreigner, we need to download the memcached client jar package and its dependent jar package.

(iii) creation of the project

(1) Now we can start our project. Build a memecached class that provides methods for storing data and fetching data into the cache. (The default operating port for memcached Server software is: 11211)

1  Public classmemcached{2     Private StaticMemcachedclient mcc=Newmemcachedclient ();3     Private StaticMemCached memcached=NewMemCached ();4 5     //Service Area Group6     Private Static FinalString[] services={"192.168.56.1:11211"};7     //the respective weights of the server farms8     Private Static FinalInteger[] Weights={1};9     //Initialize the number of connectionsTen     Private Static FinalInteger init_connection=5; One     //Maximum number of connections A     Private Static FinalInteger max_connection=50; -     //Maximum idle time 60*60*6 -     Private Static FinalLong max_idle=21600l; the      -      -     Static { -         //get the instance object for the socket connection pool +Sockiopool pool=sockiopool.getinstance (); - pool.setservers (services); + pool.setweights (weights); A Pool.setinitconn (init_connection); at Pool.setminconn (init_connection); - Pool.setmaxconn (max_connection); - Pool.setmaxidle (max_idle); -          -         //setting TCP parameters, connection timeouts, etc. -Pool.setnagle (false); inPool.setsocketto (3000); -Pool.setsocketconnectto (0); to          +         //Initializing the connection pool - pool.initialize (); the          *         //compress over specified size $Mcc.setcompressenable (true);Panax NotoginsengMcc.setcompressthreshold (65536L); -     } the      +     PrivateMemCached () {} A      the      Public StaticMemCached getinstance () { +         returnmemCached; -     } $      $      Public BooleanAdd (String key,object value) { -         returnMcc.add (key, value); -     } the      -      Public BooleanAdd (String key,object value,date expiry) {Wuyi         returnMcc.add (key,value, expiry); the     } -      Wu      Public BooleanReplace (String key,object value) { -         returnmcc.replace (key, value); About     } $      Public Booleanreplace (String key,object value,date expiry) { -         returnMcc.replace (key,key,expiry); -     } -      A      PublicObject get (String key) { +         returnMcc.get (key); the     } - 

(2) test method

 1  public  class   Test { 2   static  void   main (string[] args) { 3  MemCached cache=memcached.getinstance ();  4  cache.add ("username", "Feiji"  5  System.out.println ("The value taken from the cache is:" +cache.get ("username" )); 6   7  8 } 

This allows us to put the key "username", the value of "Feiji" key value pairs into the cache and can be removed from it.

Then we delete the 4th line of code, as long as the memcached server is not closed, or can be queried.

Of course, we can also add an expiration time for this key value pair to be deposited.

This is like an application scenario, a mobile SMS verification code. The program sends the verification code to the designated phone, which is assumed to be valid within 50S. Then this verification code in this 50S obviously to be able to find, certainly can not use the database to save it!! This is where memcached comes in handy.

The test code is as follows:

1  Public classTest {2      Public Static voidMain (string[] args) {3MemCached cache=memcached.getinstance ();4          Longnow=System.currenttimemillis ();5          LongExpiry=now+1000*50;6Date date=NewDate (expiry);7Cache.Add ("username", "Feiji", date);8System.out.println ("The value taken from the cache is:" +cache.get ("username"));9     }Ten}

(1) memcached application

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.