1. Introduction Memcached is a high-performance distributed memory cache server developed by the LiveJournal development team of a foreign community website. 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. Official Website: Workshop
I. Introduction
Memcached is a high-performance distributed memory cache server developed by the development team of LiveJournal, a foreign community website. 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.
Http://www.danga.com/memcached/
Ii. Principles
Libevent is a set of cross-platform event processing interfaces. It is compatible with event processing of operating systems such as Windows, Linux, BSD, and Solaris. Memcached uses libevent to process concurrent network connections. It can maintain fast response while maintaining high concurrency.
1. Data Storage Method: Slab Allocation
The basic principle of Slab Allocator is to divide the allocated memory into blocks of a specific length according to the predefined size to completely solve the memory fragmentation problem.
The principle of Slab Allocation is quite simple. Divide the allocated memory into chunk blocks of various sizes and divide the chunk blocks into groups)
Disadvantage: The allocated memory cannot be used effectively because it is allocated with a specific length.
2. Data Expiration method: Lazy Expiration
Memcached does not monitor whether the record expires, but checks the timestamp of the record during get to check whether the record expires.
LRU -- Least Recently Used. When memcached has insufficient memory space (when the new space cannot be obtained from slab class), you can search from the records that have not been Used Recently, and allocate the space to the new record.
Iii. Installation
Memcache includes the installation of servers and clients. It can be installed on various linux platforms or on windows.
Server
Libevent
Official Website: http://monkey.org /~ Provos/libevent/
Download: http://monkey.org /~ Provos/libevent-1.4.9-stable.tar.gz
Memcached
Official Website: http://www.danga.com/memcached
Download: http://www.danga.com/memcached/dist/
Libevent Installation
> Tar-zxvf libevent-1.4.9-stable.tar.gz
> Cd libevent-1.4.9-stable
>./Configure // installed under/usr/lib/by default
> Make
> Make install
But by default, libevent is installed in the/usr/local/lib directory, and memcached find the path of the ibevent-2.0.so.5 Module
However, the/usr/lib directory cannot be found. Therefore, you must use a soft link or specify the installation directory.
Ln-s/usr/local/lib/libevent-2.0.so.5/usr/lib/libevent-2.0.so.5
Okay. So far, only the libevent has been installed.
Install Memcached
> Tar-zxvf memcached-1.4.1.tar.gz
> Cd memcached-1.4.1
>./Configure -- prefix =/usr/local/memcached
> Make
> Make install
Start Memcached
>./Memcached-d-m 10-u root-l 10.20.0000194-p 11210-c 256-P/tmp/memcached. pid
Parameter description:
-D runs memcached in daemon mode.
-U specifies the user. If the current user is root, you must use this parameter to specify the user.
-M: Set the memory size available for memcached. The unit is Mb. The default value is 64 MB;
-L set the IP address of the listener. If it is a local machine, this parameter is usually not set;
-P: Set the listening port. The default value is 11211. Therefore, you can leave this parameter Unspecified. (If this parameter is not set, the connection fails after the test)
-C: sets the maximum number of concurrent connections. The default value is 1024.
-P: Set the pid file for saving Memcache.
Memcached stopped
> Kill 'cat/tmp/memcached. Pi'
Note:
Cat/tmp/memcached. pid -- view the memcached process number
View Memcached help information
>./Memcached-h
Check whether Memcached is running normally
> Ps auxxww | grep memcached
Client
A. linux Installation
Memcached has two installation methods: one is to directly install memcache and the other is to install memcached.
(1) install memcache
PHP Memcache Extension
Official Website: http://pecl.php.net/package/memcache
Download: http://pecl.php.net/get/memcache-3.0.4.tgz
Install
> Tar-zxvf memcache-3.0.4.tgz
> Cd memcache-3.0.4
>/Usr/local/php/bin/phpize
>./Configure -- with-php-config =/usr/local/php/bin/php-config
> Make
> Make install
Configuration
Add configuration content in php. ini:
Extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613 /"
Extension = memcache. so
(2) install memcached
The Memcached client is based on libmemcached. Therefore, you must first install libmemcached.
Libmemcached
Official Website: http://download.tangent.org
Download: http://download.tangent.org/libmemcached-0.33.tar.gz
Install
> Tar-zxvf libmemcached-0.33.tar.gz
> Cd libmemcached-0.33
>./Configure -- prefix =/usr/local/libmemcached/-- with-libmemcached-dir =/usr/local/libmemcached/-- here -- with-libmemcached-dir needs to be verified
> Make
> Make install
PHP Memcached Extension
Official Website: http://pecl.php.net/package/memcached
Download: http://pecl.php.net/get/memcached-1.0.0.tgz
Install
> Tar-zxvf memcached-1.0.0.tgz
> Cd memcached-1.0.0
>./Configure -- with-php-config =/usr/local/php/bin/php-config -- with-libmemcached-dir =/usr/local/libmemcached/
> Make
> Make install
Configuration
Add configuration content in php. ini:
Extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613 /"
Extension = memcache. so
B. Windows Installation
1. Download php_memcache.dll
2. Copy php_memcache.dll to the php/ext/directory.
3. Modify php. ini and add extension = php_memcache.dll.
4. Restart the server
5. Check phpinfo () and check the memcache part.
Iv. Installation FAQs and Solutions
1. symptom:./memcached-h displays the following prompt:
Checking for libevent directory... configure: error: libevent is required. You can get it from http://www.monkey.org /~ Provos/libevent/
Solution: Run
> Whereis libevent
> LD_DEBUG = libs./memcached-v 2> & 1>/dev/null | less
& Gt; ln-s/usr/src/libevent-1.4.9-stable/lib/libevent-1.4.so.2/lib/
2. symptom: when running the program, the page prompts: Warning: Memcache: connect () [memcache. connect]: Can't connect to 10.20.194: 11211. The connection attempt fails because the connection fails to respond correctly after a while or the host does not respond. (10060) in E: \ AppServ \ www \ test \ memcache \ test1.php on line 4
Solution: Set the port and change it to-p 11210.
>./Memcached-d-m 10-u root-l 10.20.0000194-p 11210-c 256-P/tmp/memcached. pid
3. symptom: The runtime prompt is: checking for libmemcached location... configure: error: memcached support requires libmemcached. Use -- with-libmemcached-dir =
To specify the prefix where libmemcached headers and library are located
Solution: in the current directory, run whereis libmemcached to find the libmemcached path, and then add the option -- with-libmemcached-dir = libmemcached path.
V. Application
// Connection
$ Mem = new Memcache;
$ Mem-> connect ("10.20.194", 11210 );
// Save data
$ Mem-> set ('key1', 'this is first value', 0, 60 );
$ Val = $ mem-> get ('key1 ');
Echo "Get key1 value:". $ val
."
";
// Replace Data
$ Mem-> replace ('key1', 'this is replace value', 0, 60 );
$ Val = $ mem-> get ('key1 ');
Echo "Get key1 value:". $ val ."
";
// Save the Array
$ Arr = array ('aaa', 'bbb ', 'ccc', 'ddd ');
$ Mem-> set ('key2', $ arr, 0, 60 );
$ Val2 = $ mem-> get ('key2 ');
Echo "Get key2 value :";
Print_r ($ val2 );
Echo
"
";
// Delete data
$ Mem-> delete ('key1 ');
$ Val = $ mem-> get ('key1 ');
Echo "Get key1 value:". $ val ."
";
// Clear all data
$ Mem-> flush ();
$ Val2 = $ mem-> get ('key2 ');
Echo "Get key2 value :";
Print_r ($ val2 );
Echo"
";
// Close the connection
$ Mem-> close ();
?>
Vi. Memcache method description
1. Memcache: add-add a value. if it already exists, false is returned.
2. Memcache: addServer-add an available server address
Parameters:
The first parameter indicates the server address,
The second parameter indicates the port,
The third parameter indicates whether it is a persistent connection,
The fourth parameter indicates the weight of the server on all servers,
The fifth parameter indicates the connection duration,
The sixth parameter indicates the connection Retry Interval. The default value is 15. The value-1 indicates no retry,
The seventh parameter is used to control the online status of the server,
The eighth parameter allows you to set a callback function to handle error messages.
3. Memcache: close-close a Memcache object
4. Memcache: connect-create a Memcache object
5. memcache_debug-control debugging, provided that php uses the-enable-debug option during compilation.
6. Memcache: decrement-deletes the value of a saved key.
7. Memcache: delete-delete a key value
Parameters:
The first parameter indicates the key,
The second parameter is optional, indicating the deletion delay time.
8. Memcache: flush-Clear All cached data, but the memory space used is not removed.
9. Memcache: get-get a key value
Parameters:
A parameter, indicating the key
10. Memcache: getExtendedStats-obtains the running system statistics of all processes in the process pool.
11. Memcache: getServerStatus-get the parameters of the running server
Parameters:
The first parameter indicates the server address,
The second parameter indicates the port.
12. Memcache: getStats-return some running statistics of the server
Parameters:
The first parameter indicates the type to be returned: reset, malloc, maps, cachedump, slabs, items, sizes;
The second and third parameters are used when the first parameter is set to "cachedump.
13. Memcache: getVersion-the version information of the running Memcache is returned.
14. Memcache: increment-adds the value of a saved key.
15. Memcache: pconnect-creates a persistent connection object for Memcache.
16. Memcache: replace-R overwrites an existing key.
Parameters:
The first parameter is key,
The second parameter is value,
The third parameter is optional, indicating whether to compress and save,
The fourth parameter is optional. It is used to set an expiration time for automatic destruction.
17. Memcache: set-Add a value. if it already exists, overwrite it.
Parameters:
The first parameter is key,
The second parameter is value,
The third parameter is optional, indicating whether to compress and save,
The fourth parameter is optional. It is used to set an expiration time for automatic destruction.
18. Memcache: setCompressThreshold-compresses data larger than a certain size.
Parameters:
The first parameter indicates the critical point of processing data size,
The second parameter indicates the compression ratio. The default value is 0.2.
19. Memcache: setServerParams-Modify server parameters at runtime
Parameters:
The first parameter indicates the server address,
The second parameter indicates the port,
The third parameter indicates the connection duration,
The fourth parameter indicates the connection Retry Interval. The default value is 15. The value-1 indicates no retry,
The fifth parameter is used to control the online status of the server,
The sixth parameter allows you to set a callback function to handle error messages.