Use memcached in PHP

Source: Internet
Author: User

There are two ways to use memcached in PHP: one is to install {
Undefined
} "> Memcache extension (in fact there is another {
Undefined
} "> The memcached extension is encapsulated based on the popular libmemcached library. It is written in C and is highly efficient and needs to be installed on the server. The other method is to directly use the PHP-memcached-client class library of the client, but I haven't found an official website after searching for it online for a long time. So, install an extension. Suppose PHP is installed in the/home/admin/PHP Directory:

wget http://pecl.php.net/get/memcache-2.2.5.tgzgzip -d memcache-2.2.5.tgztar xvf memcache-2.2.5.tarcd memcache-2.2.5/home/admin/php/bin/phpizeConfiguring for:PHP Api Version:         20041225Zend Module Api No:      20060613Zend Extension Api No:   220060519./configure --enable-memcache --with-php-config=/home/admin/php/bin/php-config --with-zlib-dirInstalling shared extensions:     /home/admin/php/lib/php/extensions/no-debug-non-zts-20060613/

Note the information returned by the last line, and add the following two lines to/home/admin/PHP/lib/PHP. ini.

extension_dir = "/home/admin/php/lib/php/extensions/no-debug-non-zts-20060613/"extension=memcache.so

Restart the web server. If the installation is successful, you can use phpinfo () to obtain information about the extension:

Memcache support Enabled
Active persistent connections 0
Version 2.2.5
Revision $ Revision: 1.111 $
Directive Local Value Master Value
Memcache. allow_failover 1 1
Memcache. chunk_size 8192 8192
Memcache. default_port 11211 11211
Memcache. default_timeout_ms 1000 1000
Memcache. hash_function CRC32 CRC32
Memcache. hash_strategy Standard Standard
Memcache. max_failover_attempts 20 20

You can set the above parameters in PHP. ini. The following is a PHP test code on the official website:

<?php$memcache = new Memcache;$memcache->connect('127.0.0.1', 11211) or die ("Could not connect");$version = $memcache->getVersion();echo "Server's version: ".$version."/n";$tmp_object = new stdClass;$tmp_object->str_attr = 'test';$tmp_object->int_attr = 123;$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");echo "Store data in the cache (data will expire in 10 seconds)/n";$get_result = $memcache->get('key');echo "Data from the cache:/n";var_dump($get_result);?>

The output after running is as follows:

Server's version: 1.2.6Store data in the cache (data will expire in 10 seconds)Data from the cache: object(stdClass)#3 (2){ ["str_attr"]=>  string(4) "test" ["int_attr"]=>  int(123) }

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.