使用apc_store vs apc_add的任何性能优势(反之亦然)?

时间:2013-10-20 11:56:34

标签: php caching store add apc

虽然我理解apc_store和apc_add之间的区别,但我想知道使用其中一个是否有任何性能优势?

有人会认为apc_store可能会更快一些,因为它不需要在执行插入之前进行check-if-exists。

我的想法是否正确?

或者在我们知道条件不存在的情况下使用apc_add会证明更快一点?

1 个答案:

答案 0 :(得分:0)

简短:apc_store()应略慢于apc_add()

更长:两者之间唯一的区别是传递给apc_store_helper()exclusive标志会导致apc_cache_insert()中的行为差异。

以下是发生的事情:

if (((*slot)->key.h == key.h) && (!memcmp((*slot)->key.str, key.str, key.len))) {
    if(exclusive) {
        if (!(*slot)->value->ttl || (time_t) ((*slot)->ctime + (*slot)->value->ttl) >= t) {
            goto nothing;
        }
    }
    // THIS IS THE MAIN DIFFERENCE
    apc_cache_remove_slot(cache, slot TSRMLS_CC);**
    break;
} else 
    if((cache->ttl && (time_t)(*slot)->atime < (t - (time_t)cache->ttl)) || 
        ((*slot)->value->ttl && (time_t) ((*slot)->ctime + (*slot)->value->ttl) < t)) {
        apc_cache_remove_slot(cache, slot TSRMLS_CC);
        continue;
    }

    slot = &(*slot)->next;      
}

if ((*slot = make_slot(cache, &key, value, *slot, t TSRMLS_CC)) != NULL) {
    value->mem_size = ctxt->pool->size;

    cache->header->mem_size += ctxt->pool->size;
    cache->header->nentries++;
    cache->header->ninserts++;
} else {
    goto nothing;
}

主要区别在于apc_add()如果值已经存在,则会保存一个插槽删除。现实世界的基准显然对确认分析很有意义。