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'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 C#调用虚拟键盘TabTip.exe
  • ¥15 Qt4代码实现下面的界面
  • ¥15 CCS离散化传递函数与仿真不一致
  • ¥15 prism提示我reinstall prism 如何解决
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确
  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用
  • ¥15 自己用vb.net编写了一个dll文件,如何只给授权的用户使用这个dll文件进行打包编译,未授权用户不能进行打包编译操作?