何时以及如何更新 Memcached 中已更改的项目?
我是第一次使用 PHP 的 PECL/Memcached,我不知道何时或如何更新 Memcached 中更改的项目。
我尝试过使用 Memcached::add
和 Memcached::set
,但都没有产生我期望的结果。
Memcached::set
会自动替换该值
$memcached->set('key', 'value', time() + 300);
$memcached->set('key', 'value2', time() + 300);
var_dump($memcached->get('key')); // Outputs "value2"
,而 Memcached::add
不会替换已在 Memcached 中设置的值
$memcached->add('key', 'value');
$memcached->add('key', 'value2';
var_dump($memcached->get('key')); // Outputs "value"
那么在 Memcached 中更新值的典型做法是什么?
I'm using PHP's PECL/Memcached for the first time and I can't figure out when or how I should be updating changed items in Memcached.
I've tried using both Memcached::add
and Memcached::set
and neither yields the results I expect.
Memcached::set
replaces the value automatically
$memcached->set('key', 'value', time() + 300);
$memcached->set('key', 'value2', time() + 300);
var_dump($memcached->get('key')); // Outputs "value2"
and Memcached::add
won't replace the value if it's already set in Memcached
$memcached->add('key', 'value');
$memcached->add('key', 'value2';
var_dump($memcached->get('key')); // Outputs "value"
So what's the typical practice for updating values in Memcached?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您调用 memcached::get。
如果返回值不为 false,则您的值已在缓存中,您现在拥有它并且可以使用它。
如果返回值为 false,则它不在缓存中(从未存在过或存在的内容已过期)。计算该值,将其保存到内存缓存中,然后继续执行您的程序。
缓存的目的是让您免于执行一些资源密集型任务,方法是执行一次,然后缓存一段时间并在将来的请求中重用该值。
You call memcached::get.
If the return value is not false, your value was in the cache and you now have it and can make use of it.
If the return value is false, it's not in the cache (never was or what was there has expired). Compute the value, save it into your memcache, then continue with your program.
The purpose of the cache is to save you from doing some resource-intensive task by doing it once, then caching it for a while and reusing the value on future requests.