【发布时间】:2016-11-29 20:15:48
【问题描述】:
我在我管理的应用程序中使用 Codeigniter 2.1.4 和 PHP 5.5。我一直在测试 Memcached 以提高应用程序的性能,昨天它运行良好。
今天我打算实时部署它,但经过一些改进后,我发现它不再工作了。在生产中,我将使用 AWS Elasticache(它也不适用于我的暂存环境 - 但这个问题的目的是让它在我的本地主机上重新运行,之后我会担心 Elasticache)。
首先,在 MAMP 的 php 信息上我看到了:
memcached support enabled
Version 2.2.0
libmemcached version 1.0.18
SASL support yes
Session support yes
igbinary support no
json support no
msgpack support no
在 codeigniter application/config/development/ 我有一个名为 memcached.php 的文件,其中包含以下内容:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
$config = array(
'default' => array(
'host' => 'localhost',
'port' => 11211,
'weight' => 1
)
);
我的测试很简单。我有一个具有此功能的控制器:
public function memcached(){
$this->load->driver('cache');
if($this->cache->memcached->is_supported()){
$data = $this->cache->memcached->get('foo');
if (!$data){
echo 'cache miss!<br />';
$data = 'bar';
$this->cache->memcached->save('foo',$data, 60);
}
echo $data;
echo '<pre>';
var_dump($this->cache->memcached->cache_info());
echo '</pre>';
}
}
输出是(总是)这个:
cache miss!
bar
array(1) {
["localhost:11211"]=>
array(24) {
["pid"]=>
int(-1)
["uptime"]=>
int(0)
["threads"]=>
int(0)
["time"]=>
int(0)
["pointer_size"]=>
int(0)
["rusage_user_seconds"]=>
int(0)
["rusage_user_microseconds"]=>
int(0)
["rusage_system_seconds"]=>
int(0)
["rusage_system_microseconds"]=>
int(0)
["curr_items"]=>
int(0)
["total_items"]=>
int(0)
["limit_maxbytes"]=>
int(0)
["curr_connections"]=>
int(0)
["total_connections"]=>
int(0)
["connection_structures"]=>
int(0)
["bytes"]=>
int(0)
["cmd_get"]=>
int(0)
["cmd_set"]=>
int(0)
["get_hits"]=>
int(0)
["get_misses"]=>
int(0)
["evictions"]=>
int(0)
["bytes_read"]=>
int(0)
["bytes_written"]=>
int(0)
["version"]=>
string(0) ""
}
}
有趣的是,如果我在那个 memcached.php 配置文件中编辑 memcached 的主机地址,它仍然会输出 ["localhost:11211"]。
更新:
我刚刚使用新下载的 Codeigniter 2.1.4 文件创建了一个干净的应用程序,一切都一样。
干杯!
【问题讨论】:
-
memcached 服务是否正在运行?它说pid是-1和0正常运行时间。有一个实际的 memcached 服务守护进程需要启动。尝试 telnet 到 localhost 上的端口 11211 以确定服务是否正常运行/侦听。
-
天哪。就是这样。 @skrilled,请提交答案,我将标记为正确。谢谢!
-
强烈建议您升级到 Codeigniter 3。需要处理的重大更改很少,您的性能和安全性将大大提高。
-
@cartalot 我知道我必须在不久的将来这样做。我只是担心,因为它是一个相对较大的应用程序,它可能需要一些工作。谢谢!
标签: php codeigniter memcached