The Memcache cache system builds a

Source: Internet
Author: User
Tags null null

In today's efficient society, how to apply this efficiency to their own procedures is a worthy pursuit and worthy of discussion. Because this memcache can improve the retrieval speed, improve the user experience, and it is important to reduce the database access. This greatly improves the grade of the whole application system. Therefore, adding such a technology to your application development is undoubtedly the icing on the cake for your program. The moment will appear tall, like a lot of Web applications, will be some basic information directly into the remote cache system, and the cache key in the effective time can be arbitrarily set. This is more humane, more convenient to use! All right, nonsense. To say that practice is the only standard for testing truth.

   itred      Email:[email protected]       blog:http://www.cnblogs.com/itred

A About Memcache:

I think you should know what this is about to be done before you start working on Memcache. Memcached is based on a hashmap that stores key/value pairs. Its daemon is written in C, but the client's application operations can be written in any language and communicated with the daemon through the memcached protocol. The server side can be Windows or Linux. This post will first describe the installation and configuration of the cache server system under Windows, and later on in the blog post to describe the setup and configuration in Linux.

Memcached is a high-performance distributed memory cache server. Equivalent to open up a chunk of memory for storage, the general purpose is to reduce the number of database visits by caching database query results, to improve the speed of dynamic Web applications and improve scalability.

Memcached's official website: http://memcached.org/, any version and some operations API can be downloaded from the above.

Two Install Server side:

    1. First, you need to download Memcache's Windows stable version. That is, there are memcache.exe files, and this installation is not directly under Windows Double-click to start the installation, but in the DOS command line, the installation of command-line form;
    2. Put the downloaded file into a closer path, so that in DOS operation is more convenient, I directly placed in the E-disk directory under the Memcached folder under the memcached121 folder;
    3. The installation process, the first is to find the path of the Cmd.exe file on your machine, in general, Windows cmd.exe is in the C disk/windows/system32 file. Go directly to this file, then find Cmd.exe, right mouse button, run this program as Administrator , in the Open DOS window, enter the following command to install:
> E: > CD memcached > CD memcached121 > Install >memcached.exe–d start

The installation process is as follows:

4. Verify that the installation is successful:

After the installation is complete, you need to verify that your memcache is installed successfully, and this time, open the Windows service interface to see if the installed Memcache is in the list, and you can see whether the service has been started.

Effect

  5. Now that the installation is complete, we can experience the role of the cache server in DOS first. Open the DOS window and enter the following command:

> 127.0. 0.1 11211

Note that the default port for this is 11211

The input is completed, the confirmation will go to the cache server interface, then you can use the command to operate.

Basic command:

  Format:<name><key><  Flags><exptime><bytes  >       <block>

    解释:

<command name>

Set/add/replace/get, etc.

<key>

Find keywords

<flags>

The client uses it to store extra information about key-value pairs

<exptime>

The survival time of this data, 0 means forever

<bytes>

Number of bytes stored

<data block>

Stored data blocks (can be directly understood as value in the key-value structure)

Icon:

The username is stored on the cache server, the time is 20s, and when I enter get username in 20 seconds, I get the itred value that was set at the beginning. The equivalent is key-value = = = "Username-itred (key value pair); However, when the validity period of the username is over, that is, after more than 20 seconds, when I go to fetch the username value, the end,null null value appears. Because it has lapsed. As shown in the following:

6. The next thing to do is to use a Java program:

Create a new Java project in MyEclipse and import the jar package. Shown

Post the source code for the test and attach the code comment. The test results after running are exactly the same as under DOS. Test method, after running the program, in DOS with the Get method to see if you can get the value of the start setting.

 Packagecom.red.memcached;Importjava.util.Date;Importcom.danga.MemCached.MemCachedClient;ImportCom.danga.MemCached.SockIOPool; Public classCache { Public Static voidMain (string[] args) {memcachedclient memcacheclient=Newmemcachedclient (); String[] Address= {"127.0.0.1:11211"};//Cache Server Listinteger[] Weights= {3 }; Sockiopool Pool=sockiopool.getinstance ();       Pool.setservers (address);       Pool.setweights (weights); Pool.setinitconn (5); Pool.setminconn (5); Pool.setmaxconn (200); Pool.setmaxidle (1000 * 30 * 30);//to set the maximum wait time for an available connection poolPool.setmaintsleep (30); Pool.setnagle (false); //set whether to use the Nagle algorithm, because our traffic volume is usually relatively large (relative to TCP control data) and requires prompt response, so this value needs to be set to False (by default is True)Pool.setsocketto (30);//set the Read wait time-out value for the socketPool.setsocketconnectto (0); Pool.initialize ();//Putting data into the cacheMemcacheclient.set ("username", "Red"); //put data into the cache and set the expiration timeDate Date=NewDate (2000000); Memcacheclient.set ("User", "itred", date); //Delete cached Data//Memcacheclient.delete ("User"); Memcacheclient.delete ("Username"); //Get Cached DataString str= (String) memcacheclient.get ("username");    System.out.println (str); }}

This case is only implemented in the Java language, in fact, with memcached client can be implemented in any language, if you understand the program, but you do not know how to add this cache to your program? In fact, this is very good to do, just need to put this demo into the toolkit, the demo as a tool class, need to use, just want to call this method can be. It's simple, isn't it? Ha ha. In the next blog post, you will be introduced to build a memcached cache system in a Linux environment. In contrast, I think the second one should be used by many companies at present.

Itred        
Email: [Email protected] Blog: http: // www.cnblogs.com/itred * * * copyright notice:
This article is copyrighted by the author and the blog Park, Welcome to reprint, but please mark the article in a conspicuous position. I reserve all rights to be held accountable for his use without my written consent.

Memcache Cache System build one

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.