我已经想到了两种我不太喜欢的方法:
- 调用触摸(键,null)内部try。。接住并从返回false捕获部分。但是我要换ttl,这不好对我来说
- 调用在try内添加(键,值)。。接住和返回捕获部分为false-这会降低效率,因为我必须删除我刚才不必要地添加的密钥
顺便说一句,我的环境是PHP。
有什么建议吗?
谢谢!
Couchbase目前还没有提供exists
方法,但你可以使用add
和delete
来做这些事情,这对Memcached/Memcached 也很有用
public function exists($key)
{
if ($this->object->add($key, true)) {
$this->object->delete($key);
return false;
}
return true;
}
https://github.com/twinh/widget/blob/master/lib/Widget/Couchbase.php#L118
一个简单的方法是执行get(键);如果键存在,则返回值;如果不存在,则操作返回null。
你的申请可以吗?
请注意,由于所有密钥都在内存中,因此无论密钥是否存在,都可以快速获取。
查看此示例,取自couchbase
#retrieve the last access date/time of the script.
#the key name is is the script name prefixed with DATE::
$last_access_date=$cb_obj->get("DATE::" . $script_name);
#handle the case where this is the first access to the script
#and that key doesn't yet exist
if($last_access_date == NULL){
$last_access_date = "never";
}
文章链接
我也缺少一个简单的Exists
成员。
在中。Net客户端有client.TryGet
,但它仍然会拉取该项,当它返回false
时,这并不意味着它不存在,只是它无法拉取它(只是试图在我的节点关闭的情况下执行它)。
再次为。Net客户端,但ExecuteGet
会给您一个IGetOperationResult
,它将公开例如HasValue
,但再次获取实际值。
使用视图?也许有点脏,但您可以有一个只返回Id的视图,这也将消除检索文档的需要。但不确定它是否真的会表现得更好。