Skip to content

Commit 0649eb1

Browse files
madmajestronikic
authored andcommitted
Cleanup of entries should not be skipped when using default settings
By default (apc.smart = 0), apc_cache_default_expunge() should not skip cleanup of old entries. This may cause insertion of new entries to fail unnecessarily, which is unexpected behavior.
1 parent fbeaaf6 commit 0649eb1

File tree

1 file changed

+23
-29
lines changed

1 file changed

+23
-29
lines changed

apc_cache.c

+23-29
Original file line numberDiff line numberDiff line change
@@ -735,8 +735,7 @@ PHP_APCU_API void apc_cache_clear(apc_cache_t* cache)
735735
PHP_APCU_API void apc_cache_default_expunge(apc_cache_t* cache, size_t size)
736736
{
737737
time_t t;
738-
size_t suitable = 0L;
739-
size_t available = 0L;
738+
size_t i;
740739

741740
if (!cache) {
742741
return;
@@ -751,43 +750,38 @@ PHP_APCU_API void apc_cache_default_expunge(apc_cache_t* cache, size_t size)
751750
return;
752751
}
753752

754-
/* make suitable selection */
755-
suitable = (cache->smart > 0L) ? (size_t) (cache->smart * size) : (size_t) (cache->sma->size/2);
756-
757753
/* gc */
758754
apc_cache_wlocked_gc(cache);
759755

760-
/* get available */
761-
available = apc_sma_get_avail_mem(cache->sma);
762-
763756
/* check that expunge is necessary */
764-
if (available < suitable) {
765-
size_t i;
766-
767-
/* look for junk */
768-
for (i = 0; i < cache->nslots; i++) {
769-
apc_cache_entry_t **entry = &cache->slots[i];
770-
while (*entry) {
771-
if (apc_cache_entry_expired(cache, *entry, t)) {
772-
apc_cache_wlocked_remove_entry(cache, entry);
773-
continue;
774-
}
757+
if (cache->smart > 0L && apc_sma_get_avail_mem(cache->sma) >= (size_t) (cache->smart * size)) {
758+
apc_cache_wunlock(cache);
759+
return;
760+
}
775761

776-
/* grab next entry */
777-
entry = &(*entry)->next;
762+
/* look for junk */
763+
for (i = 0; i < cache->nslots; i++) {
764+
apc_cache_entry_t **entry = &cache->slots[i];
765+
while (*entry) {
766+
if (apc_cache_entry_expired(cache, *entry, t)) {
767+
apc_cache_wlocked_remove_entry(cache, entry);
768+
continue;
778769
}
779-
}
780770

781-
/* if the cache now has space, then reset last key */
782-
if (apc_sma_get_avail_size(cache->sma, size)) {
783-
/* wipe lastkey */
784-
memset(&cache->header->lastkey, 0, sizeof(apc_cache_slam_key_t));
785-
} else {
786-
/* with not enough space left in cache, we are forced to expunge */
787-
apc_cache_wlocked_real_expunge(cache);
771+
/* grab next entry */
772+
entry = &(*entry)->next;
788773
}
789774
}
790775

776+
/* if the cache now has space, then reset last key */
777+
if (apc_sma_get_avail_size(cache->sma, size)) {
778+
/* wipe lastkey */
779+
memset(&cache->header->lastkey, 0, sizeof(apc_cache_slam_key_t));
780+
} else {
781+
/* with not enough space left in cache, we are forced to expunge */
782+
apc_cache_wlocked_real_expunge(cache);
783+
}
784+
791785
apc_cache_wunlock(cache);
792786
}
793787
/* }}} */

0 commit comments

Comments
 (0)