Memcache distributed cache

Source: Internet
Author: User

1. What is memcache and what is memcache?

Memcache is a high-performance distributed memory object cache system. By maintaining a unified and huge hash table in the memory, memcache can be used to store data in various formats, including image, video, file, and database retrieval results. Simply put, the data is called to the memory and then read from the memory, which greatly improves the reading speed.

2. download and install the SDK (I hope you will be more impressed with it)

Download memcache

Go to CMD and switch to the directory where the memcached.exe file is located.

Memcached.exe-D install

(1) Start memcached

First, click Start at the service.

Second, command line

Memcached.exe-m 200 MB-d start [started in deamon mode, default 64 m]

 

If you fail to start Windows 7 at startup, you can use the following method:

Memcached.exe-P port number


Use netstat-An to enable OK if port 11211 is listening.

3. Operate memcache

1. log on to telnet to connect to the memcached service.

Telnet 127.0.0.1 11211 (putty, xshell, or cmd can be connected)

4. How to Use the PHP program to operate our memcached Service

(1) copy the php_memcache.dll file to ext of PHP.

Different versions of PHP use different versions of php_memcache.dll.

(2) modify the php. ini file and load php_memcache.dll (this file encapsulates a bunch of Functions)

; Load the php_memcache.dll File

Extension = php_memcache.dll

(3) Restart Apache (check whether memcache exists in phpinfo)

(4) write a program to complete the curd operation.

Common methods (this is also very simple if you do not describe too much here. You can take a look at the query by adding, deleting, updating, and so on)

  • Memcache-memcache class

    • Memcache: Add-Add an entry to the Cache Server

    • Memcache: addserver-Add a memcache server to the connection pool

    • Memcache: Close-close memcache connection

    • Memcache: connect-open a memcached server connection

    • Memcache: decrement-reduce the element value

    • Memcache: delete-delete an element from the server

    • Memcache: flush-clean (delete) all elements that have been stored

    • Memcache: Get-an element retrieved from the server

    • Memcache: getextendedstats-statistics on all servers in the cache server pool

    • Memcache: getserverstatus-used to obtain the online/offline status of a server

    • Memcache: getstats-Get server statistics

    • Memcache: getversion-the server version is returned.

    • Memcache: Increment-Add the value of an element

    • Memcache: pconnect-open a persistent connection to the server

    • Memcache: replace-replace the value of an existing element

    • Memcache: set-store data at the server

    • Memcache: setcompressthreshold-Enable Automatic Compaction

    • Memcache: setserverparams-Modify server parameters and status during runtime

  • Memcache Functions

    • Memcache_debug-switch debug output on/off


5. deep understanding of memcached Mechanism

1. Simple C/S-based architecture Protocol

2. libevent-based event processing (the encapsulation of a cross-platform event processing interface can be compatible with various operating systems. memcached uses libevent to process concurrent network connections, which can be well maintained under high concurrency. response Speed)

3. memcache data is stored in the memory and deleted using the LRU algorithm when the data is full.

The so-called LRU algorithm is the least recently used algorithm by least recently used. An algorithm used by the operating system. For data blocks that are in the memory but are not recently used, LRU is called. The operating system will remove the data belonging to LRU from the memory, to free up space for loading additional data.

4 client-based distributed

[PHP]View plaincopy

  1. <PRE name = "code" class = "php"> <? PHP

  2. $ Memcache = new memcache;

  3. // Add two memcache instances

  4. $ Memcache-> addserver ('IP one', 11211 );

  5. $ Memcache-> addserver ('IP two', 11211 );

  6. $ Memcache-> set ('twos1', 'nihao );

  7. $ Memcache-> set ('tes2', 'wohao );

We can see the memcache to which tes1 and tes2 are saved. We don't need to worry about the automatic back-to-back of memcache. We can use distributed algorithms to store the corresponding data in the memcache.

How can we retrieve data?

[PHP]View plaincopy

  1. $ Memcache = new memcache;

  2. $ Memcache-> addserver ('IP one', 11211 );

  3. $ Memcache-> addserver ('IP two', 11211 );

  4. Echo $ memcache-> get ('tes1 ');

We only need to write out the key name memcache that we need to automatically retrieve the data. We don't have to worry about how to retrieve the data. This operation is transparent to us.

Pay attention to details;

When addserver is executed, it does not connect to the mem service immediately, but is computed and hashed before deciding which mem service to connect to. Therefore, when you add a large number of servers to the connection pool, there is no additional overhead.

We know that memcache stores data in the memory, so as long as the memcache server is restarted, the data will be cleared or the saved value will be cleared if the expiration time is reached.

6. Finally, let's talk about how to put the session in memcache.

Steps:

1. modify the configuration file of PHP. ini.

[Sesson. save_handler has user | files | memcache] files indicates that the session is saved by a file, that is, the default session storage mode of PHP. the user-defined mode is used for saving the session, including opening the write read and clearing the session file) today we will introduce how to save it in memcache.

You need to configure the parameters.

Session. save_handler = memcache // indicates that the session file should be saved in memcache.

Session. save_path = "TCP: // 127.0.0.1: 11211" // the corresponding memcache service address is in the form of IP: Port (you can enter multiple memcache addresses here, this way, sessions can be stored in distributed storage)

Test one. Restart Apache. When session_start () is restarted, the session data we saved will save the above memcache read in normal session read mode.

Let's think about a problem. If the Administrator does not allow us to modify the php. ini file, how can we handle session-based memcached? We can use a function to modify the configuration of PHP. ini.

Ini_set ("session. save_handler", "memcache ");

Ini_set ("session. save_path", "TCP: // 127.0.0.1: 9999 ");

You can also use ini_set to dynamically modify PHP. other settings of INI, but it does not affect other PHP pages and does not modify PHP. the INI file takes effect only on this page, that is, it takes effect temporarily.


I hope this article will be helpful to you. The above example will help you better understand it.


This article is from "if you have any" blog, please be sure to keep this source http://xiyudongqing.blog.51cto.com/2380647/1556715

Memcache distributed cache

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.