【问题标题】:Issue with php memcache fetching the value with 'true'php memcache 使用“true”获取值的问题
【发布时间】:2014-04-28 06:49:54
【问题描述】:

当我将 memcache 键“show-errors”的值存储为 true 时 使用以下方法调用它会返回成功(即 true),说明它已设置。

$memcacheObj->set("show-errors", true);

但是当我使用以下方法调用获取密钥时,我得到 1 而不是 true

$memcacheObj->get($key);

谁能帮我解决这个问题。我需要得到与 memcache 中存储的值完全相同的值。

我已经使用以下方法交叉验证了我的 memcached 服务器正在我的本地系统上运行,它返回 true。

$this->cacheObj->connect('127.0.0.1', '11211');

【问题讨论】:

  • Afaik,memcached保存序列化后的值,TRUE的序列化值为1
  • @Sal00m,有没有办法在得到结果后得到反序列化的值
  • 你可以这样使用filter_var:filter_var($memcacheObj->get($key), FILTER_VALIDATE_BOOLEAN);它应该返回true
  • @Sal00m,谢谢你的回答,它帮助了我

标签: php memcached


【解决方案1】:

与您在 cmets 中被告知的不同,序列化布尔值 TRUE 的未序列化值仍然为真。 看来问题不在于您的内存缓存,而在于您的支票。

试着检查天气你的变量是=== true,而不是打印出来,你会看到的。

这里是some example code,向您展示这是如何工作的:

<?php
$peter = true;
$serPeter = serialize($peter);

$unserPeter = unserialize($serPeter);
if($unserPeter === TRUE) {
  echo 'TRUE';
}
elseif($unserPeter === 1) {
  echo '1';
}
else {
  echo '$unserPeter is : ('.$unserPeter.')';
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    • 2015-11-01
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多