Install and use php-Memcached

Source: Internet
Author: User
PHP high-performance distributed memory object cache system extended Memcached installation and use

I. INTRODUCTION and installation

Memcached is a high-performance distributed memory object cache system. it is usually used to reduce the load pressure on databases to improve the response speed of dynamic web applications.

This extension uses the api provided by the libmemcached library to interact with the memcached server. It also provides a session processor (memcached ).

For how to install memcached, refer to this article: install and configure memcached in Ubuntu.

Before installing php extension memcached, you must first install libmemcached. libmemcached is the C/C ++ local client library of memcached.

Before installing libmemcached, install the libcloog-ppl0 first, otherwise errors will occur during compilation and installation:

sudo apt-get install libcloog-ppl0

Then refer from here:

./configure --prefix=/usr/local/libmemcachedmakesudo make install

Then you can install the memcached extension in php. download the required source code installation package: http://pecl.php.net/package/memcached, decompress it to the specified directory, enter the directory, and then execute the following command:

phpize./configure --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcachedmakesudo make install

An error may be reported during installation:

Unknown type name: memcached_server_instance_st

As a result, make fails. the solution is as follows:

Find the file php_libmemcached_compat.h in the decompressed memcached extension directory, and add the following line in it:

typedef const struct memcached_server_st *memcached_server_instance_st;

Make again and then OK.

After the installation is successful, add extension = memcacached to php. ini, restart apache, and view phpinfo.

Some storage commands include an invalid value (related to an element or a client operation request) when sent to the server. For all such usage, the actual sent value can be a Unix timestamp (the integer number of seconds from January 1, January 1, 1970 to the Expiration Time), or a number in seconds from now on. In the latter case, the number of seconds cannot exceed 60 × 60 × 24 × 30 (the number of seconds in the last 30 days). If the invalid value is greater than this value, the server uses it as a real Unix timestamp instead of an offset from the current time.

If the invalidation value is set to 0 (default), this element never expires (but it may be deleted by the server to allocate space for other new elements ).

III. callback

1. result callback

The Result callbacks method calls each element in the Result set once after obtaining the element through the Memcached: getDelayed () or Memcached: getDelayedBykey () method. The callback function can receive the element information described in an array of a Memcached object. This callback function does not need to return any information.

Example #1 Example of result callback

 addServer('localhost', 11211);    $items = array(        'key1' => 'value1',        'key2' => 'value2',        'key3' => 'value3'    );    $m->setMulti($items);    $m->getDelayed(array('key1', 'key3'), true, 'result_cb');    function result_cb($memc, $item)    {        var_dump($item);    }?>

The output of the preceding routine is similar:

array(3) {  ["key"]=> string(4) "key1"  ["value"]=> string(6) "value1"  ["cas"]=> float(49)}array(3) {  ["key"]=> string(4) "key3"  ["value"]=> string(6) "value3"  ["cas"]=> float(50)}

2. read-through cache callback

The read-through cache callback is called when an element is not retrieved from the server. This callback function receives three parameters, including the Memcached object, the request key, and the value variable passed by a reference method. This callback function determines to set a default value when the key has no value by returning true or false. If the callback returns true, Memcached stores the stored value of "outgoing parameter" (reference passed value variable) to the memcached server and returns it to the original call function. Only Memcached: get () and Memcached: getByKey () support this type of callback, because Memcache does not support providing information about keys not Retrieved when multiple keys are requested.

Example #2 Example of read-through callback

 AddServer ('localhost', 11211); $ profile_info = $ m-> get ('User :'. $ user_id, 'User _ info_cb '); function user_info_cb ($ memc, $ key, & $ value) {$ user_id = substr ($ key, 5 ); /* read personal information from the database *//*... */$ value = $ profile_info; return true ;}?>

IV. Sessions support

Memcached provides a custom session processor that can be used to store user session data to the memcached server. A completely independent memcached instance will be used internally, so you can set a different server pool if needed. The session key is stored under the prefix memc. sess. key.. Therefore, if you use the same server pool for the session and the general cache, pay attention to this. Another reason for session separation is that when the cache is full on the memcached server, your session may be kicked out of the cache, this may cause users to be inexplicably disconnected.

Session. save_handler is set to enable the memcached session processor for memcached. Session. save_path defines a comma-separated hostname: port-style session cache server pool, for example, "sess1: 11211, sess2: 11211 ".

V. Memcached class

Represents the connection to the memcached service cluster.

Memcached: add-add an element to a new key

Memcached: addByKey-add an element to a new key on the specified server

Memcached: addServer-add a server to the server pool

Memcached: addServers-add multiple servers to the server pool

Memcached: append-append data to an existing element

Memcached: appendByKey-append data to an existing element on the specified server

Memcached: cas-compare and exchange values

Memcached: casByKey-compare and exchange values on the specified server

Memcached ::__ construct-create a Memcached instance

Memcached: decrement-reduce the value of a numerical element

Memcached: decrementByKey-Decrement numeric item's value, stored on a specific server

Memcached: delete-delete an element

Memcached: deleteByKey-delete an element from the specified server

Memcached: deleteMulti-Delete multiple items

Memcached: deleteMultiByKey-Delete multiple items from a specific server

Memcached: fetch-capture the next result

Memcached: fetchAll-capture all remaining results

Memcached: flush-invalidate all elements in the cache

Memcached: get-retrieve an element

Memcached: getAllKeys-Gets the keys stored on all the servers

Memcached: getByKey-retrieve elements from a specific server

Memcached: getDelayed-request multiple elements

Memcached: getDelayedByKey-requests multiple elements from the specified server

Memcached: getMulti-retrieve multiple elements

Memcached: getMultiByKey-retrieve multiple elements from a specific server

Memcached: getOption-get the option value of Memcached

Memcached: getResultCode-return the result code of the last operation

Memcached: getResultMessage-return the result description message of the last operation

Memcached: getServerByKey-obtains the server information mapped to a key.

Memcached: getServerList-get the server list in the server pool

Memcached: getStats-get server pool statistics

Memcached: getVersion-obtains the version information of all servers in the server pool.

Memcached: increment-add the value of a numeric element

Memcached: incrementByKey-Increment numeric item's value, stored on a specific server

Memcached: isPersistent-Check if a persitent connection to memcache is being used

Memcached: isPristine-Check if the instance was recently created

Memcached: prepend-append data to an existing element

Memcached: prependByKey-Prepend data to an existing item on a specific server

Memcached: quit-Close any open connections

Memcached: replace-replace an element with an existing key

Memcached: replaceByKey-Replace the item under an existing key on a specific server

Memcached: resetServerList-Clears all servers from the server list

Memcached: set-store an element

Memcached: setByKey-Store an item on a specific server

Memcached: setMulti-store multiple elements

Memcached: setMultiByKey-Store multiple items on a specific server

Memcached: setOption-set a memcached option

Memcached: setOptions-Set Memcached options

Memcached: setSaslAuthData-Set the credentials to use for authentication

Memcached: touch-Set a new expiration on an item

Memcached: touchByKey-Set a new expiration on an item on a specific server

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.