尝试连接到memcached的步骤可以从PHP5.6开始
由于我从来没有做过这件事,所以我记下来了。
-
- memcachedのインストール
-
- PHP5.6及びPHPからmemcachedに接続する際に利用するライブラリのインストール
- コードを書いてみる
请看,以上只需一种中文的选项。
- CentOS 7にMemcached最新版をインストールしてPHPと連携する
安装memcached
$sudo yum search memcached
一部抜粋
memcached.x86_64 : High Performance, Distributed Memory Object Cache
$sudo yum install memcached
$sudo service memcached start
Starting memcached: [ OK ]
$nc -w1 localhost 11211
$echo $?
0
安装 PHP 和 memcached 库。
首先安装PHP 5.6。
$sudo yum install php56
$php -v
PHP 5.6.25 (cli) (built: Aug 27 2016 05:06:49)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
下一步是安装memcached库,用于从PHP访问memcache。
PECL :: 软件包 :: Memcached – PHP
$sudo yum search php memcached
読み込んだプラグイン:priorities, update-motd, upgrade-helper
========================= N/S matched: php, memcached ==========================
php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon
php-pecl-memcached.x86_64 : Extension to work with the Memcached caching daemon
php54-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon
php54-pecl-memcached.x86_64 : Extension to work with the Memcached caching
: daemon
php55-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon
php55-pecl-memcached.x86_64 : Extension to work with the Memcached caching
: daemon
php56-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon
php56-pecl-memcached.x86_64 : Extension to work with the Memcached caching
: daemon
php70-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon
php70-pecl-memcached.x86_64 : Extension to work with the Memcached caching
: daemon
php-ZendFramework-Cache-Backend-Libmemcached.noarch : Zend Framework libmemcache
: cache backend
php-ZendFramework-Cache-Backend-Memcached.noarch : Zend Framework memcache cache
: backend
Full name and summary matches only, use "search all" for everything.
$sudo yum install php56-pecl-memcached
# phpinfo()で取得できる内容を確認。有効化されている
$php -i |grep memcached
/etc/php-5.6.d/50-memcached.ini,
PHP Warning: Unknown: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in Unknown on line 0
memcached
memcached support => enabled
libmemcached version => 1.0.16
memcached.compression_factor => 1.3 => 1.3
memcached.compression_threshold => 2000 => 2000
memcached.compression_type => fastlz => fastlz
memcached.serializer => igbinary => igbinary
memcached.sess_binary => no value => no value
memcached.sess_connect_timeout => 1000 => 1000
memcached.sess_consistent_hash => no value => no value
memcached.sess_lock_expire => 0 => 0
memcached.sess_lock_max_wait => 0 => 0
memcached.sess_lock_wait => 150000 => 150000
memcached.sess_locking => 1 => 1
memcached.sess_number_of_replicas => 0 => 0
memcached.sess_prefix => memc.sess.key. => memc.sess.key.
memcached.sess_randomize_replica_read => no value => no value
memcached.sess_remove_failed => 1 => 1
memcached.sess_sasl_password => no value => no value
memcached.sess_sasl_username => no value => no value
memcached.store_retry_count => 2 => 2
memcached.use_sasl => no value => no value
Registered save handlers => files user memcached
# php.ini の確認。memcachedのファイルが読み込まれている
$php --ini
Configuration File (php.ini) Path: /etc
Loaded Configuration File: /etc/php.ini
Scan for additional .ini files in: /etc/php-5.6.d
Additional .ini files parsed: /etc/php-5.6.d/20-bz2.ini,
/etc/php-5.6.d/20-calendar.ini,
/etc/php-5.6.d/20-ctype.ini,
/etc/php-5.6.d/20-curl.ini,
/etc/php-5.6.d/20-dom.ini,
/etc/php-5.6.d/20-exif.ini,
/etc/php-5.6.d/20-fileinfo.ini,
/etc/php-5.6.d/20-ftp.ini,
/etc/php-5.6.d/20-gettext.ini,
/etc/php-5.6.d/20-iconv.ini,
/etc/php-5.6.d/20-phar.ini,
/etc/php-5.6.d/20-posix.ini,
/etc/php-5.6.d/20-shmop.ini,
/etc/php-5.6.d/20-simplexml.ini,
/etc/php-5.6.d/20-sockets.ini,
/etc/php-5.6.d/20-sysvmsg.ini,
/etc/php-5.6.d/20-sysvsem.ini,
/etc/php-5.6.d/20-sysvshm.ini,
/etc/php-5.6.d/20-tokenizer.ini,
/etc/php-5.6.d/20-xml.ini,
/etc/php-5.6.d/20-xmlwriter.ini,
/etc/php-5.6.d/20-xsl.ini,
/etc/php-5.6.d/20-zip.ini,
/etc/php-5.6.d/30-wddx.ini,
/etc/php-5.6.d/30-xmlreader.ini,
/etc/php-5.6.d/40-igbinary.ini,
/etc/php-5.6.d/40-json.ini,
/etc/php-5.6.d/50-memcached.ini,
/etc/php-5.6.d/php.ini
试试看
我试着按照参考博客的内容来写。
据说API的文档在下面的地方。
PHP.net Memcached 是 PHP 语言中的一个功能强大的缓存系统。
<?php
$memcache = new Memcached();
$memcache->addServer('localhost', 11211);
$data = 'this is test';
$memcache->set('key', $data, 1000);
echo $get_data = $memcache->get('key');
?>
执行
$php test.php
this is test
好的,一切都顺利完成了。