Complete memcached analysis-1. basic memcached-PHP Tutorial

Source: Internet
Author: User
Complete memcached analysis-1. the basis of memcached. Published on: 200872 original link: gihyo. jpdevfeature01memcached0001 this article series links here: 1st times: www.phpchina.comhtml29n-35329.html

Posting Date: 2008/7/2


Link: http://gihyo.jp/dev/feature/01/memcached/0001


Here is the link to this series of articles:

  • 1st times: http://www.phpchina.com/html/29/n-35329.html
  • 2nd times: http://www.phpchina.com/html/30/n-35330.html
  • 3rd times: http://www.phpchina.com/html/31/n-35331.html
  • 4th times: http://www.phpchina.com/html/32/n-35332.html
  • 5th times: http://www.phpchina.com/html/32/n-35333.html


I am Nagano from the system operation group of mixi Development Department. Routine operation of the program. Starting from today, we will discuss several times about memcached, a hot topic in the extensibility field of Web applications, and describe its internal structure and use together with the preparations of our development department research and development team.

  • What is memcached?
  • Memcached features
    • Simple Protocol
    • Libevent-based event processing
    • Built-in memory storage
    • Distributed memcached does not communicate with each other
  • Install memcached
    • Install memcached
    • Start memcached
  • Connect with a client
  • Use Cache: Memcached
    • Use Cache: Memcached to connect to memcached
    • Save data
    • Get Data
    • Delete data
    • Add and subtract operations
  • Summary
What is memcached?

Memcached is a software developed by Brad Fitzpatric, Danga Interactive under LiveJournal. 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.

In this case, memcached is ready to use. 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, so as to speed up dynamic Web applications and improve scalability.

General purpose of memcached

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 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 memcached. 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 a library that encapsulates the epoll, kqueue, and other event processing functions of Linux operating systems 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 to achieve high performance in Linux, BSD, Solaris, and other operating systems. For more information about event processing, see The C10K Problem of Dan Keel.

  • 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 only exists in the memory, restarting memcached and the operating system will cause all data to disappear. In addition, when the content capacity reaches the specified value, the unused cache is automatically deleted based on the LRU (Least Recently Used) algorithm. Memcached itself is a server designed for caching, so it does not take the permanent data into consideration. For more information about memory storage, refer to the introduction in the future.

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 communicate with each other to share information. So, how to implement distributed? This depends entirely on the implementation of the client. This section also describes the distribution of memcached.

Distributed memcached

Next, we will briefly introduce how to use memcached.

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. There is a ready-made rpm Package in Fedora 8. you can install it using the yum command.

$ sudo yum install libevent libevent-devel

The source code of memcached can be downloaded from the memcached website. The latest version of this article is 1.2.5. Although rpm Ora 8 also contains the memcached rpm, 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/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 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 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 MB. Most of the debugging information is about the storage information.

When started as a daemon background, you only need

$ /usr/local/bin/memcached -p 11211 -m 64m -d

The content of the memcached startup option used here is as follows.

Option description-TCP port used by p. The default value is the maximum memory size from 11 to M. By default, 64M-vv is started in very vrebose mode. debugging information and errors are output to the console-d as daemon.

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.

Connect with a client

In many languages, clients connected to memcached are implemented, including Perl and PHP. Only the languages listed on the memcached website have

  • Perl
  • PHP
  • Python
  • Ruby
  • C #
  • 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.

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. it should be regarded as the most widely used module in the memcached client.

  • Cache: Memcached-search.cpan.org: Http://search.cpan.org/dist/Cache-Memcached/
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 data compression value namespace to specify the prefix added to the key

In addition, Cache: Memcached can serialize and save complex Perl data through the Storable module. Therefore, hash, array, and object can be directly stored in 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', 'duration'); 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 stores replace only when no data with the same key exists in the bucket. set is different from add and replace only when data with the same key exists in the bucket, save at any timeGet Data

You can use the get and get_multi methods to 

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 synchronously, which is dozens of times faster than loop get.

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. Note that,The set function ignores this 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 performed and initialization should be performed if necessary. In addition, the server does not check the behavior that exceeds 2 32.

Summary

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.

Next, we will explain the internal structure of memcached from the beginning. By understanding the internal structure of memcached, you can know how to use memcached to accelerate Web applications. Welcome to the next chapter.

Copyright notice: It can be reproduced at will, but the original author charlee, original link, and this statement must be indicated during reprinting.



Original article links: http://gihyo.jp/dev/feature/01/memcached/0001 this article series links here: 1st times: http://www.phpchina.com/html/29/n-35329.html...




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.