Memcache Learning Materials

Source: Internet
Author: User
Tags install php zts

memcached
It is the development of foreign community website LiveJournal team to improve dynamic Web site performance by caching database query results and reducing database access times. Official site http://memcached.org/
Memcached Running flowchart

Memcached Features
? based on C/S architecture, the protocol is simple
? libevent-based event handling
? Autonomous Memory Storage processing
Storage mode is slab allocation
The principle of Slab allocation-dividing allocated memory into blocks of various sizes (chunk) and dividing the same size blocks into groups (chunk collections), each chunk collection is called Slab.
The memcached memory allocation is in page, and the page default value is 1M, which can be specified at startup by the-I parameter.
Slab are made up of multiple page, and the page is cut into multiple chunk by the specified size. The structure diagram is as follows:

Growth Factor
Memcached can specify the growth factor factor at startup by using the-f option. This value controls the difference between the slab and the chunk size. The default value is 1.25.
By memcached-tool the different slab states of the specified memcached instance, you can see that each item occupies a size (chunk size) gap of 1.25

Disadvantages of Slab allocation
Slab allocation can effectively resolve memory fragmentation problems, but in the following cases, it will result in a waste of memory:
The chunk size of each slab is fixed, and memory waste occurs when the item's footprint is actually less than the chunk size
The size of each slab is fixed (because the page is fixed), and when slab cannot be divisible by the chunk he owns, memory waste occurs
A memory waste occurs when a slab of a specified size is generated according to the growth factor factor, and a slab ID is not used at all
When the TEM is added via Memcache
1. Memcache calculate the size of the item (key+value+flags), select the appropriate slab (just to drop the item's slab)
2. If the item corresponds to a slab that does not appear, then apply 1 page (note that this 1M space whether or not the memcached use memory can be applied successfully) and add the item to slab in the chunk
3. If the item corresponding to the slab appears, then in the Slab preference expired (free_ chunks) and delete (the chunk of the delete in 1.2.2 have problems that cannot be reused) are stored chunk, followed by the selection of unused chunk (free_chunks_end) for storage. </p>
4. If the item corresponding to the slab appears, but the corresponding slab has been stored full, then will apply for a new page, the page is divided into the corresponding size of chunk, continue to store.
5. If the item corresponding to the slab appears, but the corresponding slab has been stored full and memcache also reached the maximum memory usage. The LRU algorithm will be used to clear the item (May erase the item that has not expired) and there will be eviction++
How data expires
? Lazy Expiration
Memcached internally does not monitor whether the record is out of date, but instead looks at the timestamp of the record at get and checks whether the record is out of date. This technique is called lazy (lazy) expiration. As a result, memcached does not consume CPU time on outdated monitoring.
? Lru
Memcached takes precedence over the space of a record that has timed out, but even so, there is a lack of space when appending a new record, and a space is allocated using the least recently used (LRU) mechanism. As the name implies, this is the mechanism for deleting "least recently used" records. Therefore, when there is insufficient memory space (when a new space cannot be obtained from the Slab class slab class), it is searched from records that have not been used recently, and its space is allocated to the new record. From a practical point of view of caching, the model is ideal.
memcached Installation and use
Yum install-y libevent memcached libmemcached
Start command:
/etc/init.d/memcached start
View process
PS aux |grep memcached
Default listener 127.0.0.1:11211, using memory 64M by default, this value can be adjusted
Vim/etc/sysconfig/memcached
Adjust CacheSize
The-D option is to start a daemon
-M is the amount of memory allocated to Memcache, in megabytes, 200MB
-U is the user running memcache, and if it is currently root, you need to specify the user with this parameter.
-L is the server IP address of the listener
-P is the port that sets Memcache listening, default is 11211
The-c option is the maximum number of concurrent connections that are running, by default 1024
-P is a PID file that is set to save Memcache
View memcached Run Status
Memcached-tool 127.0.0.1:11211 Stats
or echo stats |nc 127.0.0.1 11211 need to install NC tools Yum install-y NC
If you install libmemcached, you can use the command
MemStat--servers=127.0.0.1:11211 View memcached service status
Command-line usage:
Telnet 127.0.0.1 11211
Set Key2 0 60 3
Abc
STORED
Get Key2
VALUE Key2 0 3
Abc
END
Grammar rules
<command name> <key> <flags> <exptime> <bytes>\r\n <data block>\r\n
Note: \ r \ n is the ENTER key under Windows
A) <command name> can be "set", "Add", "replace".
"Set" means that the data is stored in accordance with the corresponding <key>, there is no time to increase, some overwrite.
"Add" means that the data is added according to the appropriate <key>, but the operation fails if the <key> already exists.
"Replace" means that the data is replaced by the appropriate <key>, but if the <key> does not exist, the operation fails.
Sometimes "add", "replace" will not be stored.
b) <key> the client needs to save the data key.
c) <flags> is a 16-bit unsigned integer (expressed in decimal notation).
The flag is stored along with the data that needs to be stored and is returned when the client get data.
Customers can use this flag for special purposes and this flag is opaque to the server.
D) The time of <exptime> expires.
A 0 indicates that the stored data is never obsolete (but can be replaced by the server algorithm: LRU, etc.).
If it is not 0 (Unix time or the number of seconds in the distance), when it expires, the server can guarantee that the user will not get the data (server time is the standard).
e) <bytes> the number of bytes to be stored, when the user wants to store empty data <bytes> can be 0
f) <data block> needs to store the content, after the input completes, the final client needs to add "\ r \ n" (Windows direct click Enter) as the "command header" End flag.
Set Key3 1 100 4
Abcd
STORED
Get Key3
VALUE Key3 1 4
Abcd
END
Replace Key3 1 200 5
Abcdx
STORED
Get Key3
VALUE Key3 1 5
Abcdx
END
Delete Key3
DELETED
Get Key3
END
Resources
memcached Common Commandshttp://www.cnblogs.com/jeffwongishandsome/archive/2011/11/06/2238265.html
PHP Connection using memcached
First, you need to install PHP memcache extensions
You can use the PECL installer that comes with PHP
#/usr/local/php/bin/pecl Install Memcache
can also be installed from the source code
# wget Http://www.lishiming.net/data/attachment/forum/memcache-2.2.3.tgz
# tar zxf memcache-2.2.3.tgz
# CD memcache-2.2.3
#/usr/local/php/bin/phpize
# .. /configure--with-php-config=/usr/local/php/bin/php-config
# Make && make install
After installation, there will be a hint like this:
Installing Shared extensions:/usr/local/servers/php5/lib/php/extensions/no-debug-non-zts-20060922/
Keep this in mind and then modify the php.ini to put
Extension_dir = "./"
Revision changed to
Extension_dir = "/usr/local/servers/php5/lib/php/extensions/no-debug-non-zts-20060922/"
and add a row
extension= "Memcache.so"
Inspection
#/usr/local/php/bin/php-m
Test PHP Scripts
wget www.apelearn.com/study_v2/.memcache.txt
mv. Memcache.txt test.php
/usr/local/php/bin/php test.php
Accessories: http://www.apelearn.com/bbs/thread-8660-1-1.html
Extended Learning
Maget Build memcached Cluster http://blog.snsgou.com/post-800.html
Memcached get all Keys method http://www.aikaiyuan.com/6433.html

Memcache Learning Materials

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.