memcachedを使用する(memcacheライブラリ)

[参考記事] memcachedとは
[参考記事] Windowsにmemcachedをインストールする方法
[参考記事] Linuxにmemcachedをインストールする方法
[参考記事] Rubyでmemcachedを使用する(memcache-client)
[参考記事] Perlでmemcachedを使用する(Cache::Memcachedモジュール)

PHPでmemcacheを使うには、pearのmemcacheライブラリをインストールします。

memcacheライブラリを使用するには、PHPにzlibが組み込まれている必要があります。
『 --with-zlib 』オプションをつけずにビルドしている場合は、ビルドしなおします。

[参考記事] zlibのインストール

memcacheライブラリのインストール

pear install http://pecl.php.net/get/memcache-2.2.5.tgz

Build process completed successfully
Installing '/usr/local/lib/php/extensions/no-debug-non-zts-20060613/memcache.so'
install ok: channel://pecl.php.net/memcache-2.2.5
configuration option "php_ini" is not set to php.ini location
You should add "extension=memcache.so" to php.ini

インストールが完了したら、php.iniにextension=memcache.soを追記します。

インストールのトラブルシューティング

pearチャンネルのアップデート

WARNING: configuration download directory "/tmp/pear/download" is not writeable.  Change download_dir config variable to a writeable dir to avoid this warning
downloading memcache-2.2.5.tgz ...
Starting to download memcache-2.2.5.tgz (35,981 bytes)
..........done: 35,981 bytes
WARNING: channel "pecl.php.net" has updated its protocols, use "pear channel-update pecl.php.net" to update
Cannot install, php_dir for channel "pecl.php.net" is not writeable by the current user

このエラーが出たときにはpearチャンネルをアップデートします。

pear channel-update pecl.php.net

zlib

downloading memcache-2.2.5.tgz ...
Starting to download memcache-2.2.5.tgz (35,981 bytes)
..........done: 35,981 bytes
The extension 'zlib' couldn't be found.
Please make sure your version of PHP was built with 'zlib' support.

このエラーはPHPにzlibが組み込まれていないときに発生します。
--with-zlibオプションをつけてPHPをインストール(ビルド)しなおします。

[参考記事] zlibのインストール

memcacheライブラリの使い方

memcachedの接続

$memcache = new Memcache();
$memcache->connect('localhost', 11211) or die('connect failed.');

値の格納

$memcache->set(【キーの名前】, 【キーの値】);

値の受け取り

$get = $memcache->get(【キーの名前】);

接続の解除

$memcache->close();

memcacheライブラリの使用例

<?php
$memcache = new Memcache();
$memcache->connect('localhost', 11211) or die('connect failed.');

$memcache->set('key', 'hoge');

$get = $memcache->get('key');
echo $get;

$memcache->close();

関連記事

スポンサーリンク

checkout

ホームページ製作・web系アプリ系の製作案件募集中です。

上に戻る