将 Codeigniter 与 Memcached 结合使用时出现问题

Problems using Codeigniter with Memcached

我在我管理的应用程序中使用 Codeigniter 2.1.4PHP 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(进程 ID)为 -1,正常运行时间为 0 秒。

有一个实际的 memcached 服务需要 运行 在服务器上,您可以通过尝试 telnet 到本地主机上的端口 11211 来检查它是否正在正确监听。