LoginSignup
5
5

More than 5 years have passed since last update.

PHP5.6からmemcachedに接続してみる

Posted at

やったことなかったのでメモ

  • memcachedのインストール
  • PHP5.6及びPHPからmemcachedに接続する際に利用するライブラリのインストール
  • コードを書いてみる

参考

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ライブラリのインストール

まずはPHP5.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

次にPHPからmemcacheにアクセスするためにmemcachedというライブラリをインストールします。

PECL :: Package :: 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

test.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

OK。無事に出来ましたー。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5