duai8153 2016-11-30 20:32
浏览 78
已采纳

Codeigniter Memcached未连接到AWS Elasticache实例

TL;DR: I've set up AWS ElastiCache and connected via SSH through EC2. But when I try to connect with my Codeigniter application (in the same EC2 instance) it fails and shows that it's attempting to connect to:

["localhost:11211"]

Why? Shouldn't it be:

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

So here's the background:

I'm using Codeigniter 2.1.4 with PHP 5.5 in an application I manage, and I want to save some data on an Elasticache instance using Memcached.

My application is running on an Elastic Beanstalk environment, the Elasticache instance is in the same security group as the EB's EC2 instance, and I've set a rule to open all TCP connections.

Memcached is activated as we can see running 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

When I SSH into this instance I can connect to the Elasticache instance using telnet:

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

On my application side, I have configured application/config/staging/memcached.php like this:

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

$config = array(
    'default' => array(
        'host'      => '****.****.sae1.cache.amazonaws.com',
        'port'      => 11211,
        'weight'    => 1
    )
);

And on the controller:

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>';
    }
}

And the output always say that I'm trying to connect to localhost and not to Elasticache, why?? Here's the output:

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

Sorry about the loong question guys. But I'd appreciate any thoughts on this.

Cheers.

  • 写回答

1条回答 默认 最新

  • dqdl6469 2016-11-30 21:07
    关注

    Try modifying your memcached.php file, like:

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

    And on your memcached function:

    $this->load->driver('cache', array('adapter' => 'memcached', 'backup' => 'dummy'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算