0

After installing memcache on my EC2 Linux instance using this:

:~$ sudo apt-get install memcached php5-memcache

I can immediately do this:

$memcache = new Memcache;
$memcache->connect('localhost', 11211);

$array_result=$this->db->query("SELECT * where ...."); // some DB query
$memcache->set('my_items', $array_result, false, 60*60*24);    

and later can access this cached array like this:

$memcache = new Memcache;
$memcache->connect('localhost', 11211);
$my_items=$memcache->get('my_items');
var_dump($my_items);

My question is what is the Elasticache syntax that correlates with memcache's connect(), set(), and get() commands? I'm totally confused by the Elasticache part of the AWS PHP SDK.

tim peterson
  • 23,653
  • 59
  • 177
  • 299

2 Answers2

2

You need to create elasticache node (AWS Management Console), to which you can connect via memcache client, take a look at the Getting started guide.

If you want to control your cache nodes with your code then you should use Elasticache SDK.

$memcache->connect('myfirstcacheinstance.evdfes.0001.use1.cache.amazonaws.com', 11211);

You don't need memcache server on your EC2 Linux instance it's enough to have php5-memcache PECL extension.

Aurimas Ličkus
  • 9,886
  • 4
  • 24
  • 26
  • -@Aurimas thank you, could you add to your answer some demo code to show me how the cache node code looks in comparison to my code? – tim peterson Sep 09 '12 at 17:30
  • 1
    All the hard work is on AWS Management Console :), when you create it like it's described on getting started guide, you may connect to elasticache like you are doing now, just hostname will be different. – Aurimas Ličkus Sep 09 '12 at 17:39
  • -@Aurimus, thanks again, last question: I'd like to use Elastic Beanstalk to create my instances and I'm not sure if they have the php5-memcache PECL extension installed. Meaning, I'm not sure how to instantiate new Memcache objects (`$memcache = new Memcache;`). Can I instantiate after connecting somehow? – tim peterson Sep 09 '12 at 18:15
  • 1
    If memcached extension is missing you will need to customize your beanstalk instance, take a look at the http://docs.amazonwebservices.com/elasticbeanstalk/latest/dg/using-features.customami.html – Aurimas Ličkus Sep 09 '12 at 19:08
  • The PHP container for AWS Elastic Beanstalk has both the (older) Memcache and (newer) Memcached extensions installed by default. – Ryan Parman Oct 05 '12 at 09:11
0

Try this:

<?php

$server_endpoint = "xxx.xx.xfg.sae1.cache.amazonaws.com";
$server_port = 11211;

if (version_compare(PHP_VERSION, '5.4.0') < 0) {
    //PHP 5.3 with php-pecl-memcache
    $client = new Memcache;
    $client->connect($server_endpoint, $server_port);
    //If you need debug see $client->getExtendedStats();
    $client->set('myKey', 'My Value PHP 5.3');
} else {
    //PHP 5.4 with php54-pecl-memcached:
    $client = new Memcached;
    $client->addServer($server_endpoint, $server_port);
    $client->set('myKey', 'My Value PHP 5.4');
}

echo 'Data in the cluster: [' . $client->get('myKey') . ']';

Make sure you have allowed the OUTPUT on port 11211

gpupo
  • 942
  • 9
  • 16