Skip to content

Commit

Permalink
Merge pull request #1395 from dreamsxin/flush
Browse files Browse the repository at this point in the history
Fix #1352 Add Flush for Cache
  • Loading branch information
Phalcon committed Oct 22, 2013
2 parents 4d92fe9 + b365ef0 commit 0d39dcd
Show file tree
Hide file tree
Showing 18 changed files with 557 additions and 31 deletions.
88 changes: 88 additions & 0 deletions ext/cache/backend/apc.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,91 @@ PHP_METHOD(Phalcon_Cache_Backend_Apc, exists){

RETURN_MM_FALSE;
}

/**
* Immediately invalidates all existing items.
*
* @return boolean
*/
PHP_METHOD(Phalcon_Cache_Backend_Apc, flush){

zval *type, *prefix_pattern, *iterator;
zval *key = NULL;
zend_class_entry *apciterator_ce;
#if PHP_VERSION_ID < 50500
char *str_key;
uint str_key_len;
ulong int_key;
int key_type;
#else
zval *itkey = NULL;
#endif
zend_object_iterator *it;

PHALCON_MM_GROW();

PHALCON_INIT_VAR(prefix_pattern);
ZVAL_STRING(prefix_pattern, "/^_PHCA/", 1);

array_init(return_value);

PHALCON_INIT_VAR(type);
ZVAL_STRING(type, "user", 1);

apciterator_ce = zend_fetch_class(SL("APCIterator"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);

PHALCON_INIT_VAR(iterator);
object_init_ex(iterator, apciterator_ce);
assert(phalcon_has_constructor(iterator TSRMLS_CC));
if (!phalcon_cache_backend_is_apcu) {
phalcon_call_method_p2_noret(iterator, "__construct", type, prefix_pattern);
}
else {
phalcon_call_method_p1_noret(iterator, "__construct", prefix_pattern);
}

/* APCIterator implements Iterator */
assert(instanceof_function_ex(apciterator_ce, zend_ce_iterator, 1 TSRMLS_CC));

it = apciterator_ce->get_iterator(apciterator_ce, iterator, 0 TSRMLS_CC);

/* APCIterator is an iterator */
assert(it != NULL);

/* APCIterator has key() method */
assert(it->funcs->get_current_key != NULL);

/* APCIterator has rewind() method */
assert(it->funcs->rewind != NULL);

it->funcs->rewind(it TSRMLS_CC);
while (it->funcs->valid(it TSRMLS_CC) == SUCCESS) {
PHALCON_INIT_NVAR(key);
#if PHP_VERSION_ID < 50500
key_type = it->funcs->get_current_key(it, &str_key, &str_key_len, &int_key TSRMLS_CC);
if (likely(key_type == HASH_KEY_IS_STRING)) {
/**
* Note that str_key_len includes the trailing zero.
* Remove the _PHCA prefix.
*/
ZVAL_STRINGL(key, str_key, str_key_len - 1, 1);
efree(str_key);

phalcon_call_func_p1_noret("apc_delete", key);
}
#else
PHALCON_INIT_NVAR(itkey);
it->funcs->get_current_key(it, itkey TSRMLS_CC);
if (likely(Z_TYPE_P(itkey) == IS_STRING)) {
ZVAL_STRINGL(key, Z_STRVAL_P(itkey), Z_STRLEN_P(itkey), 1);
phalcon_call_func_p1_noret("apc_delete", key);
}
#endif

it->funcs->move_forward(it TSRMLS_CC);
}

it->funcs->dtor(it TSRMLS_CC);

RETURN_MM_TRUE;
}
2 changes: 2 additions & 0 deletions ext/cache/backend/apc.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ PHP_METHOD(Phalcon_Cache_Backend_Apc, queryKeys);
PHP_METHOD(Phalcon_Cache_Backend_Apc, exists);
PHP_METHOD(Phalcon_Cache_Backend_Apc, increment);
PHP_METHOD(Phalcon_Cache_Backend_Apc, decrement);
PHP_METHOD(Phalcon_Cache_Backend_Apc, flush);

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_cache_backend_apc_get, 0, 0, 1)
ZEND_ARG_INFO(0, keyName)
Expand Down Expand Up @@ -72,6 +73,7 @@ PHALCON_INIT_FUNCS(phalcon_cache_backend_apc_method_entry){
PHP_ME(Phalcon_Cache_Backend_Apc, exists, arginfo_phalcon_cache_backend_apc_exists, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_Apc, increment, arginfo_phalcon_cache_backend_apc_increment, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_Apc, decrement, arginfo_phalcon_cache_backend_apc_decrement, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_Apc, flush, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};

64 changes: 64 additions & 0 deletions ext/cache/backend/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -704,3 +704,67 @@ PHP_METHOD(Phalcon_Cache_Backend_File, decrement){

RETURN_MM_NULL();
}

/**
* Immediately invalidates all existing items.
*
* @return boolean
*/
PHP_METHOD(Phalcon_Cache_Backend_File, flush){

zval *options, *prefix, *cache_dir, *iterator;
zval **item, *is_file = NULL, *key = NULL, *cache_file = NULL;
zend_object_iterator *it;

PHALCON_MM_GROW();

options = phalcon_fetch_nproperty_this(this_ptr, SL("_options"), PH_NOISY_CC);
prefix = phalcon_fetch_nproperty_this(this_ptr, SL("_prefix"), PH_NOISY_CC);

if (unlikely(!phalcon_array_isset_string_fetch(&cache_dir, options, SS("cacheDir")))) {
PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Unexpected inconsistency in options");
return;
}

PHALCON_INIT_VAR(iterator);
object_init_ex(iterator, spl_ce_DirectoryIterator);
assert(phalcon_has_constructor(iterator TSRMLS_CC));
phalcon_call_method_p1_noret(iterator, "__construct", cache_dir);

/* DirectoryIterator implements Iterator */
assert(instanceof_function_ex(spl_ce_DirectoryIterator, zend_ce_iterator, 1 TSRMLS_CC));

it = spl_ce_DirectoryIterator->get_iterator(spl_ce_DirectoryIterator, iterator, 0 TSRMLS_CC);

/* DirectoryIterator is an iterator */
assert(it != NULL);

/* DirectoryIterator has rewind() method */
assert(it->funcs->rewind != NULL);

it->funcs->rewind(it TSRMLS_CC);
while (it->funcs->valid(it TSRMLS_CC) == SUCCESS && !EG(exception)) {
it->funcs->get_current_data(it, &item TSRMLS_CC);

PHALCON_OBS_NVAR(is_file);
phalcon_call_method_params(is_file, &is_file, *item, SL("isFile"), zend_inline_hash_func(SS("isFile")) TSRMLS_CC, 0);

if (!EG(exception) && PHALCON_IS_TRUE(is_file)) {
PHALCON_OBS_NVAR(key);
phalcon_call_method_params(key, &key, *item, SL("getfilename"), zend_inline_hash_func(SS("getfilename")) TSRMLS_CC, 0);

PHALCON_OBS_NVAR(cache_file);
phalcon_call_method_params(cache_file, &cache_file, *item, SL("getPathname"), zend_inline_hash_func(SS("getPathname")) TSRMLS_CC, 0);

if (!EG(exception) && (PHALCON_IS_EMPTY(prefix) || phalcon_start_with(key, prefix, NULL))) {
phalcon_unlink(return_value, cache_file TSRMLS_CC);
}
}

it->funcs->move_forward(it TSRMLS_CC);
}

it->funcs->dtor(it TSRMLS_CC);

RETURN_MM_TRUE;
}
4 changes: 3 additions & 1 deletion ext/cache/backend/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ PHP_METHOD(Phalcon_Cache_Backend_File, queryKeys);
PHP_METHOD(Phalcon_Cache_Backend_File, exists);
PHP_METHOD(Phalcon_Cache_Backend_File, increment);
PHP_METHOD(Phalcon_Cache_Backend_File, decrement);
PHP_METHOD(Phalcon_Cache_Backend_File, flush);

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_cache_backend_file___construct, 0, 0, 1)
ZEND_ARG_INFO(0, frontend)
Expand Down Expand Up @@ -78,7 +79,8 @@ PHALCON_INIT_FUNCS(phalcon_cache_backend_file_method_entry){
PHP_ME(Phalcon_Cache_Backend_File, queryKeys, arginfo_phalcon_cache_backend_file_querykeys, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_File, exists, arginfo_phalcon_cache_backend_file_exists, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_File, increment, arginfo_phalcon_cache_backend_file_increment, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_File, decrement, arginfo_phalcon_cache_backend_file_decrement, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_File, decrement, arginfo_phalcon_cache_backend_file_decrement, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_File, flush, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};

54 changes: 54 additions & 0 deletions ext/cache/backend/libmemcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,3 +605,57 @@ PHP_METHOD(Phalcon_Cache_Backend_Libmemcached, exists){

PHALCON_MM_RESTORE();
}

/**
* Immediately invalidates all existing items.
*
* @return boolean
*/
PHP_METHOD(Phalcon_Cache_Backend_Libmemcached, flush){

zval *memcache, *options, *special_key;
zval *keys, *real_key = NULL;
HashPosition pos;
zval **value;

PHALCON_MM_GROW();

memcache = phalcon_fetch_nproperty_this(this_ptr, SL("_memcache"), PH_NOISY_CC);
if (Z_TYPE_P(memcache) != IS_OBJECT) {
phalcon_call_method_noret(this_ptr, "_connect");
memcache = phalcon_fetch_nproperty_this(this_ptr, SL("_memcache"), PH_NOISY_CC);
}

options = phalcon_fetch_nproperty_this(this_ptr, SL("_options"), PH_NOISY_CC);

if (unlikely(!phalcon_array_isset_string_fetch(&special_key, options, SS("statsKey")))) {
PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Unexpected inconsistency in options");
return;
}

/**
* Get the key from memcached
*/
PHALCON_OBS_VAR(keys);
phalcon_call_method_p1_ex(keys, &keys, memcache, "get", special_key);
if (Z_TYPE_P(keys) == IS_ARRAY) {

for (
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(keys), &pos);
zend_hash_get_current_data_ex(Z_ARRVAL_P(keys), (void**)&value, &pos) == SUCCESS;
zend_hash_move_forward_ex(Z_ARRVAL_P(keys), &pos)
) {
zval key = phalcon_get_current_key_w(Z_ARRVAL_P(keys), &pos);

PHALCON_INIT_NVAR(real_key);
ZVAL_STRINGL(real_key, Z_STRVAL(key), Z_STRLEN(key), 1);

phalcon_array_unset(&keys, real_key, 0);
phalcon_call_method_p1_noret(memcache, "delete", real_key);
}

phalcon_call_method_p2_noret(memcache, "set", special_key, keys);
}

RETURN_MM_TRUE;
}
2 changes: 2 additions & 0 deletions ext/cache/backend/libmemcached.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ PHP_METHOD(Phalcon_Cache_Backend_Libmemcached, queryKeys);
PHP_METHOD(Phalcon_Cache_Backend_Libmemcached, exists);
PHP_METHOD(Phalcon_Cache_Backend_Libmemcached, increment);
PHP_METHOD(Phalcon_Cache_Backend_Libmemcached, decrement);
PHP_METHOD(Phalcon_Cache_Backend_Libmemcached, flush);

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_cache_backend_libmemcached___construct, 0, 0, 1)
ZEND_ARG_INFO(0, frontend)
Expand Down Expand Up @@ -81,6 +82,7 @@ PHALCON_INIT_FUNCS(phalcon_cache_backend_libmemcached_method_entry){
PHP_ME(Phalcon_Cache_Backend_Libmemcached, exists, arginfo_phalcon_cache_backend_libmemcached_exists, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_Libmemcached, increment, arginfo_phalcon_cache_backend_libmemcached_increment, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_Libmemcached, decrement, arginfo_phalcon_cache_backend_libmemcached_decrement, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_Libmemcached, flush, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};

53 changes: 53 additions & 0 deletions ext/cache/backend/memcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -577,3 +577,56 @@ PHP_METHOD(Phalcon_Cache_Backend_Memcache, decrement){

RETURN_CTOR(newVal);
}

/**
* Immediately invalidates all existing items.
*
* @return boolean
*/
PHP_METHOD(Phalcon_Cache_Backend_Memcache, flush){

zval *memcache, *options, *special_key;
zval *keys, *real_key = NULL;
HashPosition pos;
zval **value;

PHALCON_MM_GROW();

memcache = phalcon_fetch_nproperty_this(this_ptr, SL("_memcache"), PH_NOISY_CC);
if (Z_TYPE_P(memcache) != IS_OBJECT) {
phalcon_call_method_noret(this_ptr, "_connect");
memcache = phalcon_fetch_nproperty_this(this_ptr, SL("_memcache"), PH_NOISY_CC);
}

options = phalcon_fetch_nproperty_this(this_ptr, SL("_options"), PH_NOISY_CC);

if (unlikely(!phalcon_array_isset_string_fetch(&special_key, options, SS("statsKey")))) {
PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Unexpected inconsistency in options");
return;
}

/**
* Get the key from memcached
*/
PHALCON_OBS_VAR(keys);
phalcon_call_method_p1_ex(keys, &keys, memcache, "get", special_key);
if (Z_TYPE_P(keys) == IS_ARRAY) {
for (
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(keys), &pos);
zend_hash_get_current_data_ex(Z_ARRVAL_P(keys), (void**)&value, &pos) == SUCCESS;
zend_hash_move_forward_ex(Z_ARRVAL_P(keys), &pos)
) {
zval key = phalcon_get_current_key_w(Z_ARRVAL_P(keys), &pos);

PHALCON_INIT_NVAR(real_key);
ZVAL_STRINGL(real_key, Z_STRVAL(key), Z_STRLEN(key), 1);

phalcon_array_unset(&keys, real_key, 0);
phalcon_call_method_p1_noret(memcache, "delete", real_key);
}

phalcon_call_method_p2_noret(memcache, "set", special_key, keys);
}

RETURN_MM_TRUE;
}
6 changes: 4 additions & 2 deletions ext/cache/backend/memcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ PHP_METHOD(Phalcon_Cache_Backend_Memcache, queryKeys);
PHP_METHOD(Phalcon_Cache_Backend_Memcache, exists);
PHP_METHOD(Phalcon_Cache_Backend_Memcache, increment);
PHP_METHOD(Phalcon_Cache_Backend_Memcache, decrement);
PHP_METHOD(Phalcon_Cache_Backend_Memcache, flush);

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_cache_backend_memcache___construct, 0, 0, 1)
ZEND_ARG_INFO(0, frontend)
Expand Down Expand Up @@ -79,8 +80,9 @@ PHALCON_INIT_FUNCS(phalcon_cache_backend_memcache_method_entry){
PHP_ME(Phalcon_Cache_Backend_Memcache, delete, arginfo_phalcon_cache_backend_memcache_delete, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_Memcache, queryKeys, arginfo_phalcon_cache_backend_memcache_querykeys, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_Memcache, exists, arginfo_phalcon_cache_backend_memcache_exists, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_Memcache, increment, arginfo_phalcon_cache_backend_memcache_increment, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_Memcache, decrement, arginfo_phalcon_cache_backend_memcache_decrement, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_Memcache, increment, arginfo_phalcon_cache_backend_memcache_increment, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_Memcache, decrement, arginfo_phalcon_cache_backend_memcache_decrement, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_Memcache, flush, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};

12 changes: 12 additions & 0 deletions ext/cache/backend/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,15 @@ PHP_METHOD(Phalcon_Cache_Backend_Memory, decrement){

RETURN_MM();
}

/**
* Immediately invalidates all existing items.
*
* @return boolean
*/
PHP_METHOD(Phalcon_Cache_Backend_Memory, flush){

phalcon_update_property_null(this_ptr, SL("_data") TSRMLS_CC);

RETVAL_TRUE;
}
4 changes: 3 additions & 1 deletion ext/cache/backend/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ PHP_METHOD(Phalcon_Cache_Backend_Memory, queryKeys);
PHP_METHOD(Phalcon_Cache_Backend_Memory, exists);
PHP_METHOD(Phalcon_Cache_Backend_Memory, increment);
PHP_METHOD(Phalcon_Cache_Backend_Memory, decrement);
PHP_METHOD(Phalcon_Cache_Backend_Memory, flush);

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_cache_backend_memory_get, 0, 0, 1)
ZEND_ARG_INFO(0, keyName)
Expand Down Expand Up @@ -71,7 +72,8 @@ PHALCON_INIT_FUNCS(phalcon_cache_backend_memory_method_entry){
PHP_ME(Phalcon_Cache_Backend_Memory, queryKeys, arginfo_phalcon_cache_backend_memory_querykeys, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_Memory, exists, arginfo_phalcon_cache_backend_memory_exists, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_Memory, increment, arginfo_phalcon_cache_backend_memory_increment, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_Memory, decrement, arginfo_phalcon_cache_backend_memory_decrement, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_Memory, decrement, arginfo_phalcon_cache_backend_memory_decrement, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_Memory, flush, arginfo_phalcon_cache_backend_memory_decrement, ZEND_ACC_PUBLIC)
PHP_FE_END
};

Loading

0 comments on commit 0d39dcd

Please sign in to comment.