【问题标题】:aws elasticache with php - unable to set key/value pair带有 php 的 aws elasticache - 无法设置键/值对
【发布时间】:2019-07-30 19:08:47
【问题描述】:

我可以像这样连接到我的 elasticache 集群:

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

当我打印$clusterResult 时,我得到了关于集群的信息,很好。

但是我如何才能真正与端点交互来设置键/值对呢?

我正在尝试但没有成功:

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

$result 没有打印任何内容。 我对使用哪个对象来设置和获取键/值对感到困惑。

【问题讨论】:

  • 你指的是 Memcached 吗?从您使用的类来看,您所指的似乎也是它的内存缓存
  • 当我尝试使用 memcached 时,你是怎么理解的?因此 Memcached()。我应该改用 Memcache 吗?
  • 是的,使用 Memcached 进行快速值存储和检索。但它永远不应该用来替代数据库。从您当前的问题来看,Memcached 似乎适合您的需求。

标签: php memcached aws-sdk amazon-elasticache


【解决方案1】:

要在 Memcached 中设置键/值对,请始终从当前时间延长到期时间。

试试这个

$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: 出于某种原因,Memcached 最适合 Carbon 时间

请参阅https://artisansweb.net/work-php-datetime-using-carbon/,了解如何在您当前的项目中设置和使用Carbon

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 2018-03-28
    • 2015-10-01
    • 2021-11-29
    • 1970-01-01
    • 2018-05-06
    • 2017-01-27
    • 2012-06-22
    相关资源
    最近更新 更多