grpaiva grpaiva - 9 days ago 6
PHP Question

Codeigniter Memcached not connecting to AWS Elasticache Instance

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.

Answer

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