Memcache中的replace功能有什么意义?
如果你只能使用 set,那么 PHP memcache 中的替换函数有什么意义呢?即使有变量,set 也会自动替换它,对吧?
你能举个例子,说明最好使用替换而不是设置吗?
谢谢。
What is the point of replace function in PHP memcache if you can just use set? Even if there is a variable, set automatically replaces it, right?
Can you give me an example where it's better to use replace instead of set?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据 PHP.net:
According to PHP.net:
如果您查看此处的评论,请按照 ZoFreX 的回答进行操作:
http://www.php.net/manual/en/memcache.set。 php
你会看到以下内容:
因此,真正的
replace()
将首先查找现有的键,然后替换它(如果存在),而set()
只会添加该键。我想最好首先使用replace()
,因为如果找不到密钥,它会返回FALSE
,在这种情况下,您可以使用add()< /code> 而不是
set()
因为您确信该密钥不存在。这可以确保您不会发生任何意外事故。所以你的代码可能是这样的:Following on from ZoFreX's answer if you look at the comments here:
http://www.php.net/manual/en/memcache.set.php
You will see the following:
So really and truly
replace()
will look for an existing key first, and then replace it (if it exists), whereasset()
will just add the key. I imagine it's always best to usereplace()
first considering it returnsFALSE
if the key is not found, in which case you'd useadd()
rather thanset()
since you know for sure the key doesn't exist. This ensures that you won't have any unintended mishaps. So you're code could be something like:一般情况下,如果密钥可能经常使用,则应使用替换。设置/添加/等创建一个全新的条目,并可能导致碎片和大量清理。 Replace 重用已经分配的内存(如果可能),并且可以更加稳定和高效。如果失败,使用 add/set 仍然有效。
Generally, replace should be used if the key is likely to be used frequently. Set/add/etc creates a brand new entry, and can lead to fragmentation and a lot of cleanup. Replace reuses the memory already allocated (if possible), and can be more stable and efficient. If it fails, using add/set will still work.
不,那是完全错误的。
如果你想检查和设置一个值,你必须使用GETS + CAS。
nope that's entirely wrong.
If you want to check and set a value you must use GETS + CAS.