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 运筹学排序问题中的在线排序
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧