what is memcached.
Memcached is a software developed by Brad Fitzpatric, a Danga Interactive company in LiveJournal. It has become an important factor in improving Web application extensibility in many services such as Mixi, Hatena, Facebook, Vox, and LiveJournal.
Many Web applications save data to an RDBMS, where the application server reads the data and displays it in the browser. However, with the increase of data volume and the concentration of access, the burden of RDBMS, database response deterioration, site display delay and other significant impact.
This is the time to memcached. Memcached is a high-performance distributed memory cache server. The general purpose is to reduce the number of database accesses by caching database query results to improve the speed and scalability of dynamic Web applications.
Fig. 1 Characteristics of the use memcached of memcached under normal circumstances
Memcached, as a distributed cache server running at high speed, has the following characteristics. Protocol simple libevent-based event handling built-in memory storage memcached the distributed protocols that do not communicate with each other are simple
memcached Server client communication does not use a format such as complex XML, but uses a simple text-line-based protocol. As a result, you can also save data and get data on memcached by using Telnet. Here is an example.
$ telnet localhost 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 located in the source code of the memcached, or you can refer to the following URL. Http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt event handling based on Libevent
Libevent is a library that encapsulates event-handling functions such as Linux's Epoll, BSD-class operating system kqueue, and so on as a unified interface. The Performance of O (1) can be played even if the number of connections to the server increases. Memcached uses this libevent library, so it can perform its high performance on Linux, BSD, Solaris and other operating systems. About event handling is no longer detailed here, you can refer to Dan Kegel's c10k problem. Libevent:http://www.monkey.org/~provos/libevent/the c10k problem:http://www.kegel.com/c10k.html Internal Memory storage method
To improve performance, the data saved in memcached is stored in Memcached's built-in memory storage space. Because the data exists only in memory, restarting the memcached and restarting the operating system will cause all data to disappear. Additionally, when the content capacity reaches the specified value, the unused cache is automatically deleted based on the LRU (Least recently used) algorithm. The memcached itself is a server designed for caching, so there is not too much consideration for permanent data issues. For more information on memory storage, the second part of this series will be introduced in the former Osaka, please refer to it at that time. memcached distributed without communication with each other
Memcached Although it is a "distributed" cache server, there is no distributed functionality on the server side. Each memcached does not communicate with each other to share information. So, how to distribute it. This depends entirely on the client's implementation. This series will also introduce the distribution of memcached.
Figure 2 memcached distributed
The following is a brief introduction to how memcached is used. Installing memcached
Memcached installation is relatively simple, here a little explanation.
Memcached supports many platforms. Linux FreeBSD Solaris (memcached 1.2.5 or later) Mac OS X
It can also be installed on Windows. This is illustrated using Fedora Core 8. installation of memcached
Running memcached requires the Libevent library that is described at the beginning of this article. Fedora 8 has a ready-made RPM package that can be installed with the Yum command.
$ sudo yum install libevent libevent-devel
Memcached source code can be downloaded from the memcached website. The latest version of this article is 1.2.5. Fedora 8 also contains the memcached rpm, but the version is older. Because the source code is not difficult to install, the RPM is not used here. Download memcached:http://www.danga.com/memcached/download.bml
The memcached installation is the same as the general application, configure, make, and make install on the line.
$ wget http://www.danga.com/memcached/dist/memcached-1.2.5.tar.gz
$ tar zxf memcached-1.2.5.tar.gz
$ cd memcached-1.2.5
$./configure
$ make
$ sudo make install
By default, memcached is installed under/usr/local/bin. memcached Start-up
Start memcached by entering the following command from the terminal.
$/usr/local/bin/memcached-p 11211-m 64m-vv
Slab class 1:chunk size perslab 11915
Slab class 2: Chunk size perslab 9362
Slab class 3:chunk size 144 perslab 7281
Middle omitted
slab Class 38:chunk size 391224 perslab 2
Slab class 39:chunk size 489032 perslab 2
<23 Server listening
<24 send buffer is 110592, now 268435456
<24 server Listening (UDP)
<24 server L Istening (UDP)
<24 server Listening (UDP)
<24 server listening (UDP)
The debugging information is shown here. This launches the memcached at the foreground, listening to TCP port 11211 with a maximum memory usage of 64M. The content of the debug information is mostly about the stored information, the next time the serial is specified.
When booting as a daemon background, simply
$/usr/local/bin/memcached-p 11211-m 64m-d
The contents of the Memcached startup options used here are as follows.
Options |
Description |
-P |
The TCP port used. Default is 11211 |
-M |
Maximum memory size. Default is 64M |
-vv |
Start with very vrebose mode, debug information and error output to console |
-D |
Start in the background as a daemon |
The above four is a common startup option, and many others, through
$/usr/local/bin/memcached-h
Commands can be displayed. Many of the options can change the behavior of memcached and recommend reading. Connect with Client
Many languages implement a client that connects Memcached, with Perl and PHP as the main. Only the languages listed on the memcached website are Perl PHP Python Ruby C/C + + Lua
Wait a minute. memcached Client api:http://www.danga.com/memcached/apis.bml
Here's how to link memcached through the Perl library that Mixi is using. using cache::memcached
Perl's Memcached client has cache::memcached cache::memcached::fast cache::memcached::libmemcached
Wait for several CPAN modules. The cache::memcached described here is Memcached's author Brad Fitzpatric's work, which should be considered the most widely used module in the memcached client. cache::memcached-search.cpan.org:http://search.cpan.org/dist/cache-memcached/ using Cache::Memcached connection Memcached
The following source code is an example of a memcached that was just started by cache::memcached connection.
#!/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 the instance. Cache::memcached common options are shown below.
Options |
Description |
Servers |
specifying memcached servers and ports with arrays |
Compress_threshold |
Values to use when compressing data |
Namespace |
Specifies the prefix to add to the key |
In addition, cache::memcached through the storable module can be the Perl complex data serialization and then save, so hash, array, object, etc. can be saved directly to memcached. Save Data
The method of saving data to memcached has the add replace set
They are used in the same way:
My $add = $memcached->add (' key ', ' value ', ' term ');
My $replace = $memcached->replace (' key ', ' value ', ' term ');
My $set = $memcached->set (' key ', ' value ', ' term ');
You can specify a period of time (in seconds) when you save data to memcached. When you do not specify a period, Memcached saves the data according to the LRU algorithm. The differences between the three methods are as follows:
Options |
Description |
Add |
Save only if there is no data with the same key in the storage space |
Replace |
Save only if there is data with the same key in the storage space |
Set |
Unlike add and replace, save whenever |
Get Data
Get data can use the Get and Get_multi methods.
My $val = $memcached->get (' key ');
My $val = $memcached->get_multi (' Key 1 ', ' Key 2 ', ' Key 3 ', ' Key 4 ', ' key 5 ');
Use Get_multi when more than one data is obtained at a time. Get_multi can simultaneously obtain multiple key values synchronously, at a speed dozens of times times faster than a loop call get. Delete Data
Deleting data uses the Delete method, but it has a unique feature.
$memcached->delete (' key ', ' block time (s) ');
Deletes the data for the key specified by the first parameter. The second parameter specifies a time value that prevents new data from being saved with the same key. This feature can be used to prevent incomplete caching of data. Note, however, that the set function ignores the block and saves data by one and minus one operation as usual.
You can use a specific key value on the memcached as a counter.
My $ret = $memcached->incr (' key ');
$memcached->add (' key ', 0) unless defined $ret;
Increment one and minus one are atomic operations, but do not automatically assign 0 when the initial value is not set. Therefore, error checking should be performed and initialization should be added if necessary. Also, the server side does not check for more than 2 32 o'clock behavior. Summary
This is a brief introduction to Memcached, its installation method, and the use of Perl client cache::memcached. As long as you know, the use of memcached is very simple enough.
The next time the memcached is explained by the former Sakamoto, the internal structure. Understanding the internal structure of memcached, you can know how to use memcached to make your Web application faster. Please continue reading the next chapter.
Transferred from: http://tech.idv2.com/2008/07/10/memcached-001/