How do I check if a key exists in Couchbase?
I've already thought of two methods that I don't really like:
- Call touch (key, null) inside try..catch .. and return false from the catch section. But then I change the ttl which is not good for me.
- The call to add (key, value) inside try..catch .. and return false from the catch section is this defect efficiency because I need to remove their key which I just unnecessarily added.
By the way, my environment is PHP.
Any suggestions?
Thank!
source to share
Couchbase is not currently provided method exists
, but you can use add
, and delete
for this, it is also useful for the Memcache / 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
source to share
check this example taken from 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";
}
source to share
I am missing a simple element Exists
.
In a .Net client, you have client.TryGet
that, however, will pull the item, and when it returns false
it doesn't really mean it doesn't exist, just so that it can't pull it out (just tried to execute it with my nodes disabled).
Again for a .Net client, but ExecuteGet
will provide you IGetOperationResult
with which to display, for example HasValue
, but pull out the actual value again.
Using a view? Maybe a little messy, but you can have a view that only returns IDs, this saves you the trouble of fetching the document. Not sure if it really will be better though.
source to share