《PHP应用:thinkPHP实现MemCache分布式缓存功能》要点:
本文介绍了PHP应用:thinkPHP实现MemCache分布式缓存功能,希望对您有用。如果有疑问,可以联系我们。
相关主题:memcache扩展 / 键值KeyValue存储数据库
PHP实例本文实例讲述了thinkPHP实现MemCache分布式缓存功能.分享给大家供大家参考,具体如下:
PHP实例两天在研究MemCache分布式缓存的问题时,发现ThinkPHP其实并不支持分布式缓存功能,这可以从官方提供的CacheMemcache.class.php文件中看到:
PHP实例
if(empty($options)) {
$options = array
(
'host' => '127.0.0.1',
'port' => 11211,
'timeout' => false,
'persistent' => false
);
}
$func = $options['persistent'] ? 'pconnect' : 'connect';
$this->expire = isset($options['expire'])?$options['expire']:C('DATA_CACHE_TIME');
$this->handler = new Memcache;
$this->connected = $options['timeout'] === false ?
$this->handler->$func($options['host'], $options['port']) :
$this->handler->$func($options['host'], $options['port'], $options['timeout']);
PHP实例不过不要紧,稍微修改下就行了,即
PHP实例
if(empty($options)) {
$options = array
(
'timeout' => false,
'persistent' => false,
'servers'=>array(
array('ip'=>'127.0.0.1','port'=>11211),
array('ip'=>'127.0.0.1','port'=>11212),
array('ip'=>'202.116.32.4','port'=>11211),
),
);
}
//分布式处理函数
$func="addServer";
$this->expire = isset($options['expire'])?$options['expire']:C('DATA_CACHE_TIME');
$this->handler = new Memcache;
if($options['timeout']===false)
{
foreach($options['servers'] as $server)
{
$this->handler->$func($server['ip'],$server['port']);
}
}
PHP实例闲来无事,于是就在本机上启动了两个MemCache服务器,顺手编写了一段简单的监控代码(隔一段时间自动刷新一次),进行测试.如果发现服务器运行不正常,则使用PhpMailer自动发送一封Email到管理员邮箱.测试结果表明,两台Memcache服务器均工作正常,而另外一台虚假的服务器当然是无法连接到的.哈哈,够简单的吧
PHP实例更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《ThinkPHP常用办法总结》、《smarty模板入门基础教程》及《PHP模板技术总结》.
PHP实例希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所赞助.
《PHP应用:thinkPHP实现MemCache分布式缓存功能》是否对您有启发,欢迎查看更多与《PHP应用:thinkPHP实现MemCache分布式缓存功能》相关教程,学精学透。维易PHP学院为您提供精彩教程。
转载请注明本页网址:
http://www.vephp.com/jiaocheng/7249.html