发布于 2016-02-11 23:17:13 | 182 次阅读 | 评论: 0 | 来源: 网友投递
memcached 集中式缓存系统
memcached是一套分布式的快取系统,当初是Danga Interactive为了LiveJournal所发展的,但目前被许多软件(如MediaWiki)所使用。这是一套开放源代码软件,以BSD license授权释出。
服务器端主要是安装memcache服务器端,目前的最新版本是 memcached-v1.4.4 。
下载:http://memcached.googlecode.com/files/memcached-1.4.4.tar.gz
另外,Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libevent,
libevent的最新版本是libevent-1.4.13-stable。(如果你的系统已经安装了libevent,可以不用安装)
官网:http://www.monkey.org/~provos/libevent/
下载:http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz
准备Memcached的PHP扩展的源码安装包:
官网:http://pecl.php.net/get/memcache-2.2.5.tgz
Linux指令下载:
wget http://memcached.googlecode.com/files/memcached-1.4.4.tar.gz
wget http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz
wget http://pecl.php.net/get/memcache-2.2.5.tgz
tar zxvf libevent-1.4.13-stable.tar.gz
cd libevent-1.4.13-stable
./configure --prefix=/usr
make
make install
ls -al /usr/lib | grep libevent
libevent-1.1a.so.1
libevent-1.1a.so.1.0.2
libevent-1.4.so.2
libevent-1.4.so.2.1.3
libevent.a
libevent_core-1.4.so.2
libevent_core-1.4.so.2.1.3
libevent_core.a
libevent_core.la
libevent_core.so
libevent_extra-1.4.so.2
libevent_extra-1.4.so.2.1.3
libevent_extra.a
libevent_extra.la
libevent_extra.so
libevent.la
libevent.so
tar zxvf memcached-1.4.4.tar.gz
cd memcached-1.4.4
./configure –with-libevent=/usr
make && make install
ls -al /usr/local/bin/mem*
-rwxr-xr-x 1 root root 201869 12-14 21:44 /usr/local/bin/memcached
tar vxzf memcache-2.2.5.tgz
cd memcache-2.2.5
/usr/local/webserver/php/bin/phpize
./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
make
make install
extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/”
memcached -d -m 10 -u root -l 202.207.177.177 -p 11211 -c 256 -P /tmp/memcached.pid
kill `cat /tmp/memcached.pid`
netstat -ant
tcp 0 0 202.207.177.177:11211 0.0.0.0:* LIST
reboot
<?php
$mem = new Memcache;
$mem->connect("202.207.177.177", 11211);
$mem->set('key', 'This is a test!', 0, 60);
$val = $mem->get('key');
echo $val;
?>
<?php
//MemCache服务器配置
//define('MEMCACHE_HOST', 'localhost'); //MemCache服务器主机
//define('MEMCACHE_PORT', 11211); //MemCache服务器端口
//define('MEMCACHE_TIMEOUT', 1); //S,MemCache服务器连接超时
class cache
{
var $memcache;
function __construct()
{
$this->memcache = &new Memcache;
$this->memcache->pconnect(MEMCACHE_HOST, MEMCACHE_PORT, MEMCACHE_TIMEOUT);
}
function cache()
{
$this->__construct();
}
function get($name)
{
return $this->memcache->get($name);
}
function set($name, $value, $ttl = 0)
{
return $this->memcache->set($name, $value, 0, $ttl);
}
function rm($name)
{
return $this->memcache->delete($name);
}
function clear()
{
return $this->memcache->flush();
}
}
?>