Codeigniter Memcached未连接到AWS Elasticache实例

时间:2016-11-30 20:32:31

标签: php codeigniter amazon-web-services memcached amazon-elasticache

TL; DR:我已经设置了AWS ElastiCache并通过EC2通过SSH连接。但是当我尝试连接我的Codeigniter应用程序(在相同的EC2实例中)时,它会失败并显示它正在尝试连接到:

["localhost:11211"]

为什么呢?不应该是:

["****.****.sae1.cache.amazonaws.com:11211"]

所以这里是背景:

我在我管理的应用程序中使用 Codeigniter 2.1.4 PHP 5.5 ,我想使用Memcached在Elasticache实例上保存一些数据。

我的应用程序在Elastic Beanstalk环境中运行,Elasticache实例与EB的EC2实例位于同一安全组中,并且我已设置规则以打开所有TCP连接。

Memcached已激活,因为我们可以看到正在运行的phpinfo:

memcached support       enabled
Version                 2.2.0
libmemcached version    1.0.8
SASL support            yes
Session support         yes
igbinary support        yes
json support            yes
msgpack support         no

当我通过SSH连接到此实例时,我可以使用telnet连接到Elasticache实例:

#telnet ****.****.sae1.cache.amazonaws.com 11211
Trying xx.xx.xx.xx...
Connected to ****.****.sae1.cache.amazonaws.com.

在我的应用程序方面,我已经像这样配置了 application / config / staging / memcached.php

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

$config = array(
    'default' => array(
        'host'      => '****.****.sae1.cache.amazonaws.com',
        '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>';
    }
}

输出总是说我试图连接到localhost而不是Elasticache,为什么?这是输出:

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) ""
  }
}

对不起那些问题的人抱歉。但我对这方面的任何想法表示感谢。

干杯。

1 个答案:

答案 0 :(得分:1)

尝试修改memcached.php文件,例如:

$config['memcached'] = array(
  'hostname' => '****.****.sae1.cache.amazonaws.com',
  'port' => 11211,
  'weight' => 1
);

memcached函数上:

$this->load->driver('cache', array('adapter' => 'memcached', 'backup' => 'dummy'));