Memcache - 如何延长值的过期时间并保持值的一致性?

发布于 2024-09-04 03:30:15 字数 1572 浏览 5 评论 0原文

请看这段简单的 PHP 代码:

//Documentation:
//memcache_set ( string $key , mixed $var [, int $flag [, int $expire ]] )
//memcache_increment ( string $key [, int $value = 1 ] )

//part 1
memcache_set ( 'id' , 1 , 0 , 60 );

//part 2
$id = memcache_increment ( 'id' , 1 );

现在想象一下,递增的“第 2 部分”是从许多独立的客户端调用的,并且每个客户端都获得其唯一的 ID。

问题是:如何延长值“id”的过期时间并保持值的一致性?请记住,每次某个客户端都会增加该值。

如何解决此问题的一些想法:

尝试快速

memcache_set( 'id' , memcache_get( 'id'  ) , 0 , 60 );

但是 get 和 set 之间存在时间漏洞,其他客户端有时可以更改该值。(? )

使用信号量,例如:

效果不太好...

memcache_set ( 'lock' , 1 , 0 , 60 );
memcache_set( 'id' , memcache_get( 'id'  ) , 0 , 60 );
memcache_delete( 'lock' ); 
//client will not increment if the lock is present and wait for lock will get out

使用memcache_delete:

memcache::delete  ( 'id' , 60 );

文档缺少调用两次会发生什么情况。是否应该延长首次通话的有效期?

最后一个令人惊讶的例子(你自己尝试一下):

$memcache_obj = new Memcache;
$memcache_obj->connect('127.0.0.1', 11211);
$memcache_obj->set('id', '1', 0 , 30);

echo "*" . $memcache_obj->get('id') . "\n" ;

$memcache_obj->increment('id');
echo "*" . $memcache_obj->get('id') . "\n" ;

echo " now delete with timeout..." . "\n";
$memcache_obj->delete('id' , 10 )  . "\n" ;
echo "*" . $memcache_obj->get('id')  . "\n" ;

sleep(11);
echo "*" . $memcache_obj->get('id')  . "\n" ;

返回我:

*1
*2
 now delete with timeout...
*
*

See this simple piece of code in PHP:

//Documentation:
//memcache_set ( string $key , mixed $var [, int $flag [, int $expire ]] )
//memcache_increment ( string $key [, int $value = 1 ] )

//part 1
memcache_set ( 'id' , 1 , 0 , 60 );

//part 2
$id = memcache_increment ( 'id' , 1 );

And now imagine that the incrementing "part 2" is called from many independent clients and every is getting its unique ID.

Qestion is: How extend expiration of value 'id' with keeping consistency of value? Keep in mind that everytime some client can come to increment the value.

Some ideas how solve this problem:

Try to be fast

memcache_set( 'id' , memcache_get( 'id'  ) , 0 , 60 );

But here is time hole between get and set and other client at time can change the value.(?)

Use semaphore e.g.:

not much effective...

memcache_set ( 'lock' , 1 , 0 , 60 );
memcache_set( 'id' , memcache_get( 'id'  ) , 0 , 60 );
memcache_delete( 'lock' ); 
//client will not increment if the lock is present and wait for lock will get out

use memcache_delete:

memcache::delete  ( 'id' , 60 );

documentation is lack of what happened if you call it twice. Should it extend Expiration of first call or not?

Last surprising example (try it your self):

$memcache_obj = new Memcache;
$memcache_obj->connect('127.0.0.1', 11211);
$memcache_obj->set('id', '1', 0 , 30);

echo "*" . $memcache_obj->get('id') . "\n" ;

$memcache_obj->increment('id');
echo "*" . $memcache_obj->get('id') . "\n" ;

echo " now delete with timeout..." . "\n";
$memcache_obj->delete('id' , 10 )  . "\n" ;
echo "*" . $memcache_obj->get('id')  . "\n" ;

sleep(11);
echo "*" . $memcache_obj->get('id')  . "\n" ;

Returns me:

*1
*2
 now delete with timeout...
*
*

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗳卜坏 2024-09-11 03:30:15

使用 memcached 扩展代替 memcache,它支持 cas 令牌:http://php.net/manual/en/memcached.cas.php 这个例子几乎是不言自明的。

Use the memcached extension instead of memcache, which supports cas tokens: http://php.net/manual/en/memcached.cas.php The example there is pretty much self-explanatory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文