7月 272011
 

【 可直接参看: http://phpor.net/blog/post/888 】

上篇文章解决了PHP的Memcache 模块的get方法不区分连接失败NOT FOUND, 今天说一下delete方法不区分NOT FOUND和连接失败的处理办法。

这次的修改不同上次,虽然delete也有第二个参数,但是第二个参数不是引用(当然可以修改成引用,只是稍微麻烦一点);根据delete的特点,不妨将返回值修改为整型(long型); 代码如下:
==== memcache.c =======

  1. PHP_FUNCTION(memcache_delete)
  2. {
  3.     if (!mmc_get_pool(mmc_object, &pool TSRMLS_CC) || !pool->num_servers) {
  4.         RETURN_LONG(-1); // 此处修改
  5.     }
  6.     if (mmc_prepare_key_ex(key, key_len, key_tmp, &key_tmp_len TSRMLS_CC) != MMC_OK) {
  7.         RETURN_LONG(-1);  // 此处修改
  8.     }
  9.     while (result < 0 && (mmc = mmc_pool_find(pool, key_tmp, key_tmp_len TSRMLS_CC)) != NULL) {
  10.         if ((result = mmc_delete(mmc, key_tmp, key_tmp_len, time TSRMLS_CC)) < 0) {
  11.             mmc_server_failure(mmc TSRMLS_CC);
  12.         }      
  13.     }
  14.     if (result > 0) {
  15.         RETURN_LONG(1); // delete OK
  16.     } elseif(result == 0) {
  17.         RETURN_LONG(0); // not found
  18.     }
  19.     RETURN_LONG(-1); // connect fail
  20. }

测试代码如下:

  1. <?php
  2. $m = new Memcache();
  3. $m->addServer(“127.0.0.1”, 11211); // this is available
  4. $result = $m->delete(“noexists”);
  5. var_dump($result);
  6. $m->set(“exists”,“I am here”,0,600);
  7. $result = $m->delete(“exists”);
  8. var_dump($result);
  9. $m = new Memcache();
  10. $m->addServer(“127.0.0.1”, 11214); // this is available
  11. $result = $m->delete(“noexists”);
  12. var_dump($result);

输出:
int(0)
int(1)
int(-1)

这样修改以后,确实挺好用的,但是,和原来的用法已经不兼容了,除非你原来根本就不判断返回值。

 Posted by at 上午 8:07

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

(required)

(required)

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据