检查Memcache中是否存在密钥 [英] Check if a key exists in Memcache
问题描述
如何在PHP中检查是否将值存储在Memcache中而不进行获取?我不喜欢获取它,因为我设置的值都为1MB,获取之后我就没有用了,所以浪费了资源.我在脚本中使用此脚本,该脚本检查某些密钥是否已缓存在内存缓存中,如果没有,它将从慢速数据源读取它们并将其设置在内存缓存中.
How do I check in PHP if a value is stored in Memcache without fetching it? I don't like fetching it because the values I have set are all 1MB in size and after I fetch it, I have no use for it, so I'm wasting resources. I'm using this in a script that checks if certain keys are cached in memcache and if not, it reads them from a slow data source and sets them in memcache.
如果我使用Memcached::append
将NULL
附加到要检查的密钥上怎么办?如果成功,则返回TRUE
;如果失败,则返回FALSE
.如果键不存在,Memcached::getResultCode
将返回Memcached::RES_NOTSTORED
.这样,我检查密钥是否存在,并且应该将密钥放在LRU列表的顶部吗?
What if I use Memcached::append
to append NULL
to the key I'm checking? Returns TRUE
on success or FALSE
on failure. The Memcached::getResultCode
will return Memcached::RES_NOTSTORED
if the key does not exist. This way I check whether the key exists and it should put the key on top of the LRU list right?
谢谢.
推荐答案
我想知道为什么Memcached没有特殊的方法.经过一些考虑之后,我得出以下结论:
I wonder why Memcached has no special method for it. Here is what I came down to after some considerations:
function has($key)
{
$m->get($key)
return \Memcached::RES_NOTFOUND !== $m->getResultCode();
}
这篇关于检查Memcache中是否存在密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!