Memcached the second article----installation use

Source: Internet
Author: User
Tags memcached apache tomcat

Summary: Set add replace get delete gets CAs stats and Flush_all commands

gets all keys. You can use the Memcachedclient statsitems (), statsslabs () combination to get key. This is the so-called fast keyset, but the return of the key has expired, if you want to get the key in real time, need to send a FETCH request verification, efficiency can be imagined


Reference: http://www.czhphp.com/archives/252

How to integrate memcached into your environment.

Before you start installing and using using memcached, we need to understand how to integrate memcached into your environment. Although memcached can be used anywhere, I find that memcached often works best when I need to perform several recurring queries in the database tier. I often set up a series of memcached instances between the database and the application server and use a simple pattern to read and write to these servers. Figure 1 can help you understand how to set up your application architecture:


Figure 1. Sample application Architecture using memcached

Architecture is fairly easy to understand. I built a WEB layer that included some Apache instances. The next layer is the application itself. This layer typically runs on Apache Tomcat or other open source application servers. The next layer is where the memcached instance is configured-between the application server and the database server. When using this configuration, a slightly different way is needed to perform read and write operations on the database.

Read

The order in which I perform the read operation is to get the request from the WEB layer (a database query needs to be executed once) and to check the results of the query previously stored in the cache. If I find the value I want, I return it. If not found, executes the query and stores the results in the cache before returning the results to the WEB layer.

Write

When you write data to a database, you first need to perform a database write operation, and then set any previously cached results that are affected by this write operation to be invalid. This process helps prevent data inconsistencies between the cache and the database.

Install memcached

Memcached supports some operating systems, including linux®, Windows®, Mac OS, and Solaris. In this article, I'll explain in detail how to build and install memcached from a source file. The main reason for this is that I can view the source code when I encounter a problem.

Libevent

Libevent is the only prerequisite for installing memcached. It is the asynchronous event notification library that memcached relies on. You can find the source file on the monkey.org on the libevent. Next, locate the source file for its latest version. For this article, we use a stable version of 1.4.11. After you get the archive, unzip it to a convenient location, and then execute the command in Listing 1:


Listing 1. Build and install Libevent

				
CD libevent-1.4.11-stable/

./configure make make
				
install

Memcached

Obtain the memcached source file from Danga Interactive, and still select the latest distribution. At the time of writing this article, the latest version is 1.4.0. Extract the tar.gz to a convenient location and execute the commands in Listing 2:


Listing 2. Build and install memcached

				
CD memcached-1.4.0/

./configure make make
				
install

After you complete these steps, you should have a memcached working copy installed, and you can use it. Let's make a brief introduction and then use it.

Using memcached

To start using memcached, you first need to start the memcached server and then connect to it using the Telnet client.

To start memcached, execute the command in Listing 3:


Listing 3. Start memcached

				
./memcached-d-M 2048-l 10.0.0.40-p 11211

This starts memcached (-D) in the form of a daemon, assigns it 2GB memory (-M 2048), and specifies the listener localhost, Port 11211. You can modify these values as needed, but the above settings are sufficient to complete the exercises in this article. Next, you need to connect to memcached. You will connect to the memcached server using a simple Telnet client.

Most operating systems provide a built-in Telnet client, but if you are using a windows-based operating system, you need to download a third-party client. I recommend using PuTTy.

After you install the Telnet client, execute the command in Listing 4:


Listing 4. Connect to Memcached

				
telnet localhost 11211

If everything works, you should get a telnet response that instructs Connected to localhost (already connected to localhost). If you do not get this response, you should return to the previous steps and ensure that the source files for both libevent and memcached have been successfully generated.

You are now logged on to the memcached server. Thereafter, you will be able to communicate with memcached through a series of simple commands. 9 memcached Client commands can be grouped into three categories: basic advanced Management

Basic memcached Client Command

You will use the five basic memcached commands to perform the simplest operation. These commands and actions include: Set add replace get delete

The first three commands are standard modification commands for manipulating key-value pairs stored in memcached. They are all very easy to use, and each uses the syntax shown in Listing 5:


Listing 5. Modify command syntax

				
Command <key> <flags> <expiration time> <bytes>
<value>

Table 1 defines the parameters and usages of the memcached Modify command.


Table 1. memcached Modify Command parameters

Parameters usage
Key Key is used to find cached values
Flags You can include integer parameters for key-value pairs that the client uses to store additional information about key-value pairs
Expiration time The length of time the key value pair is saved in the cache (in seconds, 0 for eternity)
bytes Byte points stored in the cache
Value Stored value (always in the second row)

Now, let's take a look at the actual use of these commands.

Set
The set command is used to add a new key-value pair to the cache. If the key already exists, the previous value will be replaced.

Note the following interactions, which use the SET command:

Set userId 0 0 5
12345
STORED

If a key-value pair is correctly set using the SET command, the server responds with the word STORED. This example adds a key-value pair to the cache whose key is userId and has a value of 12345. And the expiration time is set to 0, which notifies memcached that you want to store this value in the cache until you delete it.

Add
The add command adds a key-value pair to the cache only if the key does not exist in the cache. If the key already exists in the cache, the previous value will remain the same, and you will get a response not_stored.

The following are standard interactions using the Add command:

Set userId 0 0 5
12345
STORED

add userId 0 0 5
55555
not_stored

add companyid 0 0 3
564
     
      stored

     

Replace
The Replace command replaces the key in the cache only if the key already exists. If the key does not exist in the cache, you will receive a not_stored response from the memcached server.

The following are standard interactions using the Replace command:

Replace AccountId 0 0 5
67890
not_stored

set accountid 0 0 5
67890
STORED

replace AccountId 0 0 5< c7/>55555
STORED

The last two basic commands are get and delete. These commands are fairly easy to understand and use a similar syntax, as follows:

Command <key>

Next look at the application of these commands.

Get
The GET command retrieves the value associated with the previously added key value pair. You will use get to perform most of the retrieval operations.

The following are typical interactions using the GET command:

Set userId 0 0 5
12345
STORED get

userId
VALUE userId 0 5
12345
End Get Bob End

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.