Skip to content

Instantly share code, notes, and snippets.

@dervn
Created August 15, 2012 12:43
Show Gist options
  • Save dervn/3359828 to your computer and use it in GitHub Desktop.
Save dervn/3359828 to your computer and use it in GitHub Desktop.
php memcached 扩展安装以及测试代码
// 安装 php memcached 扩展 http://www.php.net/manual/zh/book.memcached.php
// 注意:不是http://www.php.net/manual/zh/book.memcache.php
// 确保已经安装了libmemcached
wget http://pecl.php.net/get/memcached-2.1.0.tgz
tar zxf memcached-2.1.0
cd memcached-2.1.0
phpize
./configure
make
make install
// 修改 php.ini
// vim .../php.ini
// 添加扩展
// extension=memcached.so
// 重启php-fpm[查看phpinfo 中 memcached 是否已经加载了]
<?php
//连接
$mem = new Memcached();
$mem->addServer("127.0.0.1", 11211);
//$mem->connect("127.0.0.1", 11211);
//保存数据
$mem->set('key1', 'This is first value', 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val ."<br />";
//替换数据
$mem->replace('key1', 'This is replace value', 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "<br />";
//保存数组
$arr = array('aaa', 'bbb', 'ccc', 'ddd');
$mem->set('key2', $arr, 60);
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "<br />";
//删除数据
$mem->delete('key1');
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "<br />";
//清除所有数据
$mem->flush();
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "<br />";
//关闭连接
//$mem->close();
@dervn
Copy link
Author

dervn commented Aug 15, 2012

ruby有gem,python有easy_install 以及进阶的pip,新兴的nodejs也有npm。。。

php啊!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment