user1098063
user1098063

Reputation:

aws elasticache with php - unable to set key/value pair

I am able to connect to my elasticache cluster like so:

$awsElasticache = new ElastiCacheClient(CredentialProvider::atsDefaultConfigConstructor(false, false));
$clusterResult = $awsElasticache->describeCacheClusters(array('CacheClusterId'=>'my_cluster'));

When I print $clusterResult, I get info about the cluster, good.

But how can I actually interact with the endpoint to set key/value pairs?

I am trying this without success:

$this->mem = new Memcached();
$this->mem->addServer($this->endPoint,11211);
$this->mem->set('myKey','myValue',3600);
$result = $this->mem->get('myKey');
echo $result;

I get nothing printed from $result. I am confused about which object to use to set and get key/value pairs.

Upvotes: 0

Views: 395

Answers (1)

Jonathan K
Jonathan K

Reputation: 550

To set key/value pair in Memcached, always extend the time of expiry from current time.

Try this

$this->mem = new Memcached();
$this->mem->addServer($this->endPoint,11211);

$expires = Carbon::now()->addMinutes(10);
$this->mem->set('myKey','myValue', $expires);
$result = $this->mem->get('myKey');
echo $result;

NOTE: For some reason, Memcached works best with Carbon time

See https://artisansweb.net/work-php-datetime-using-carbon/ on how to setup and use Carbon on your current project

Upvotes: 1

Related Questions