1.1 What is memcached?
Memcached is a software developed by Brad fitzpatric, danga interactive under livejournal. Now
It has become an important factor in improving the scalability of Web applications among many services such as Mixi, hatena, Facebook, Vox, and livejournal.
Many Web applications save data to RDBMS. The application server reads data from the data and displays it in the browser. However
As the data volume increases and access is concentrated, the burden on RDBMS increases, the database response deteriorates, and the website display latency increases significantly.
Impact.
In this case, memcached is ready to use. Memcached is a high-performance distributed memory cache server. General Purpose
Yes. By caching database query results, you can reduce the number of database accesses to speed up dynamic web applications and improve scalability.
.
Figure 1.1:
General purpose of memcached
5
Idv2.com Chapter 1 basics of memcached
1.2 memcached features
As a high-speed distributed cache server, memcached has the following features.
• Simple protocol
• Libevent-based event processing
• Built-in memory storage
• Distributed memcached does not communicate with each other
Simple Protocol
For memcached Server Client Communication, simple text line-based protocols are used instead of complex XML and other formats.
Therefore, you can use Telnet to save and retrieve data on memcached. The following is an example.
$ Telnet local host 11211
Trying 127.0.0.1...
Connected to localhost. localdomain (127.0.0.1 ).
Escape Character is '^]'.
Set Foo 0 0 3 (save command)
Bar (data)
Stored (result)
Get Foo (GET command)
Value Foo 0 3 (data)
Bar (data)
The protocol document is in the memcached Source code You can also refer to the following URL.
Http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt
Libevent-based event processing
Libevent is Program Library, which encapsulates the epoll, BSD operating system kqueue, and other event processing functions in Linux into a unified
Interface. O (1) performance can be used even if the number of connections to the server increases. Memcached uses this libevent library, so
High Performance in Linux, BSD, Solaris, and other operating systems. We will not detail the event processing here. You can
Refer to the c10k problem of Dan Kegel.
• Libevent: http://www.monkey.org /~ Provos/libevent/
• The c10k problem: http://www.kegel.com/c10k.html
Built-in memory storage
To improve performance, data stored in memcached is stored in the memory storage space built in memcached. Because the data is only
Because it exists in the memory, restarting memcached and the operating system will cause all data to disappear. In addition, the content capacity reaches the specified
After the value is set, it is based on LRU (least recently used) Algorithm The unused cache is automatically deleted. Memcached itself is a cache
Therefore, the design of the server does not take into account the permanent problem of data too much. For more information about memory storage, see this article
The contents described in the beginning of chapter 1.
Distributed memcached does not communicate with each other
Although memcached is a "distributed" cache server, the server does not have distributed functions. Memcached does not interact with each other
Communication to share information. So, how to implement distributed? This depends entirely on the implementation of the client. This article will also introduce
Distributed memcached.
6
Idv2.com Chapter 1 basics of memcached
Figure 1.2: distributed memcached
Next, we will briefly introduce how to use memcached.
1.3 install memcached
Memcached is easy to install.
Memcached supports many platforms.
• Linux
• FreeBSD
• Solaris (memcached 1.2.5 or later)
• Mac OS X
It can also be installed on Windows. Here, Fedora Core 8 is used for description.
Install memcached
To run memcached, you must first introduce the libevent library. The ready-made RPM package exists in fedora 8 and is installed using the yum command
You can.
$ Sudo Yum install libevent libeventdevel
Memcached Source Code It can be downloaded from the memcached website. The latest version of this article is 1.2.5. Although
It also contains the RPM of memcached, but the version is old. Because it is not difficult to install the source code, rpm is not used here.
• Download memcached: http://www.danga.com/memcached/download.bml
The memcached installation is the same as that of common applications. You can use configure, make, and make install.
$ Wget http://www.danga.com/memcached/dist/memcached1.2.5.
Tar.gz
$ Tar zxf memcached1.2.5.
Tar.gz
$ CD memcached1.2.5
$./Configure
$ Make
7
Idv2.com Chapter 1 basics of memcached
$ Sudo make install
By default, memcached is installed in/usr/local/bin.
Start memcached
Enter the following command from the terminal to start memcached.
$/Usr/local/bin/memcached P
11211 m
64 m VV
Slab Class 1: Chunk Size 88 perslab 11915
Slab Class 2: Chunk Size 112 perslab 9362
Slab Class 3: Chunk Size 144 perslab 7281
Omitted
Slab Class 38: Chunk Size 391224 perslab 2
Slab class 39: Chunk Size 489032 perslab 2
<23 server listening
<24 send buffer was 110592, now 268435456
<24 server listening (UDP)
<24 server listening (UDP)
<24 server listening (UDP)
<24 server listening (UDP)
Debugging information is displayed here. In this way, memcached is started on the front-end, and the maximum memory usage of TCP port 11211 is
64 m. The content of debugging information is mostly about storage information, which will be described in the next chapter.
When started as a daemon background, you only need
$/Usr/local/bin/memcached P
11211 m
64 m d
The content of the memcached startup option used here is as follows.
Option description
P
The TCP port used. The default value is 11211.
M
Maximum memory size. The default value is 64 MB.
VV
Start in very vrebose mode, and output debugging information and errors to the console.
D
Start daemon in the background
The above four are commonly used startup options, and there are many other options.
$/Usr/local/bin/memcached H
Command to display. Many options can change the behaviors of memcached. Read-Only is recommended.
1.4 connect with a client
In many languages, clients connected to memcached are implemented, including Perl and PHP. List only on memcached website
The language
• Perl
• PHP
• Python
• Ruby
• C #
8
Idv2.com Chapter 1 basics of memcached
• C/C ++
• Lua
And so on.
• Memcached client API: http://www.danga.com/memcached/apis.bml
Here we will introduce how to link memcached through the Perl library being used by Mixi.
1.5 Use cache: memcached
The memcached client of Perl has
• Cache: memcached
• Cache: memcached: fast
• Cache: memcached: libmemcached
And other CPAN modules. Cache: memcached is the work of Brad fitzpatric, creator of memcached.
This is the most widely used module in the memcached client.
• Cache: memcached search.
Cpan.org: http://search.cpan.org/dist/CacheMemcached/
Use cache: memcached to connect to memcached
The source code below is an example of connecting memcached just started through cache: memcached.
#! /Usr/bin/perl
Use strict;
Use warnings;
Use cache: memcached;
My $ key = "foo ";
My $ value = "bar ";
'My $ expires = 3600; #1 hour
My $ memcached = cache: memcached>
New ({
Servers => ["127.0.0.1: 11211"],
Compress_threshold => 10_000
});
$ Memcached>
Add ($ key, $ value, $ expires );
My $ ret = $ memcached>
Get ($ key );
Print "$ RET" N ";
Here, the IP address of the memcached server and an option are specified for Cache: memcached to generate an instance.
Cache: Common memcached options are as follows.
Option description
Servers uses arrays to specify the memcached server and Port
Compress_threshold value used for Data Compression
Namespace specifies the prefix to add to the key
In addition, cache: memcached uses the Storable module to serialize and save complex Perl data. Therefore,
Arrays and objects can be directly stored in memcached.
9
Idv2.com Chapter 1 basics of memcached
Save data
The methods for saving data to memcached are as follows:
• Add
• Replace
• Set
They are used in the same way:
My $ add = $ memcached>
Add ('key', 'value', 'deadline ');
My $ replace = $ memcached>
Replace ('key', 'value', 'duration ');
My $ set = $ memcached>
Set ('key', 'value', 'duration ');
You can specify the period (in seconds) when saving data to memcached ). If the period is not specified, memcached saves data according to the LRU algorithm.
The differences between the three methods are as follows:
Option description
Add is saved only when no data with the same key exists in the bucket.
Replace is saved only when data with the same key exists in the bucket.
Set is different from ADD and replace.
Get Data
You can use the get and get_multi methods to obtain data.
My $ val = $ memcached>
Get ('key ');
My $ val = $ memcached>
Get_multi ('key 1', 'key 2', 'key 3', 'key 4', 'key 5 ');
Use get_multi to retrieve multiple data records at a time. Get_multi can obtain multiple key values non-synchronously at the same time, and its speed is faster than that of cyclic calls
Get is dozens of times faster.
Delete data
The delete method is used to delete data, but it has a unique function.
$ Memcached>
Delete ('key', 'blocking time (seconds )');
Deletes the data of the key specified by the first parameter. The second parameter specifies a time value. You cannot use the same key to save new data.
This function can be used to prevent incomplete cached data. However, note that the Set function ignores the blocking and saves data as usual.
Add and subtract operations
You can use a specific key value on memcached as a counter.
My $ ret = $ memcached>
Incr ('key ');
$ Memcached>
Add ('key', 0) Unless defined $ ret;
Increment and subtract 1 are atomic operations, but when the initial value is not set, it is not automatically assigned to 0. Therefore, an error check should be conducted. If necessary
Add initialization operations. In addition, the server does not check the behavior when the number exceeds 232.
10
Idv2.com Chapter 1 basics of memcached
1.6 conclusion
This article briefly introduces memcached and its installation method and the usage of Perl Client Cache: memcached. As long as you know,
Memcached is easy to use.
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