Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add memcache increment and decrement #1135

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
be45696
Basic memcache incr & decr implementation
iwalz Aug 21, 2013
ada4243
Unit tests
iwalz Aug 22, 2013
a6cecd5
Format
iwalz Aug 22, 2013
50ef4ba
Fixed Format
iwalz Aug 22, 2013
fd888be
Fixed Format
iwalz Aug 22, 2013
7a5495c
Added phalcon.c
iwalz Aug 22, 2013
b6b72ec
Xcache incr and decr
iwalz Aug 23, 2013
7e4ba36
Add xcache trunk
iwalz Aug 23, 2013
cc197cb
Add php -v output
iwalz Aug 23, 2013
0ce63d3
Add php -v output after xcache installation
iwalz Aug 23, 2013
98e40c2
Temporary remove apc
iwalz Aug 23, 2013
db7c949
Finished incr and decr for xcache
iwalz Aug 23, 2013
f1e9061
Added missing xcache.c
iwalz Aug 23, 2013
76cec9d
Fixed travis failures
iwalz Aug 23, 2013
4c97eba
Changed variable export
iwalz Aug 23, 2013
ad0ad39
Added fallback if xcache_inc doesn't exist
iwalz Aug 23, 2013
1585c8f
Flush memcache after first test
iwalz Aug 23, 2013
8c266af
Disable apc via cli setting
iwalz Aug 23, 2013
e546f47
Seperated xcache check for travis
iwalz Aug 23, 2013
25e6f11
Finished xcache fallback to non atomic incr/decr
iwalz Aug 24, 2013
5522dd8
Use ZVAL_LONG instead of direct assignment
iwalz Aug 25, 2013
da05106
Add memory cache incr/decr
iwalz Aug 26, 2013
bfc6757
Add filecache incr/decr
iwalz Sep 13, 2013
d36631d
Mongo incr/decr implementation
iwalz Sep 15, 2013
9876237
Removed comment
iwalz Sep 15, 2013
354dad3
Fixed seg fault
iwalz Sep 24, 2013
b647337
Some code optimization
iwalz Sep 24, 2013
cb63e2b
Some code optimization
iwalz Sep 24, 2013
3e2fc60
Fixed segfault
iwalz Sep 25, 2013
1aaf168
Reverted build changes, impossible to merge
iwalz Oct 12, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ before_script:

script:
- $(phpenv which php) ./unit-tests/ci/phpunit.php --debug -c unit-tests/phpunit.xml
- $(which bash) ./unit-tests/ci/check_for_xcache.sh

after_failure:
- sudo apt-get -qq install gdb
Expand Down
253 changes: 244 additions & 9 deletions ext/cache/backend/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,16 @@ PHP_METHOD(Phalcon_Cache_Backend_File, get){
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_cache_exception_ce, exception_message);
return;
}

/**
* Use the frontend to process the content of the cache
*/
phalcon_call_method_p1(return_value, frontend, "afterretrieve", cached_content);

if (phalcon_is_numeric(cached_content)) {
RETURN_CCTOR(cached_content);
} else {
/**
* Use the frontend to process the content of the cache
*/
phalcon_call_method_p1(return_value, frontend, "afterretrieve", cached_content);
}

RETURN_MM();
}
}
Expand Down Expand Up @@ -290,14 +295,21 @@ PHP_METHOD(Phalcon_Cache_Backend_File, save){
PHALCON_CPY_WRT(cached_content, content);
}

PHALCON_INIT_VAR(prepared_content);
phalcon_call_method_p1(prepared_content, frontend, "beforestore", cached_content);

if (!phalcon_is_numeric(cached_content)) {
PHALCON_INIT_VAR(prepared_content);
phalcon_call_method_p1(prepared_content, frontend, "beforestore", cached_content);
}

/**
* We use file_put_contents to respect open-base-dir directive
*/
PHALCON_INIT_VAR(status);
phalcon_file_put_contents(status, cache_file, prepared_content TSRMLS_CC);
if (!phalcon_is_numeric(cached_content)) {
phalcon_file_put_contents(status, cache_file, prepared_content TSRMLS_CC);
} else {
phalcon_file_put_contents(status, cache_file, cached_content TSRMLS_CC);
}

if (PHALCON_IS_FALSE(status)) {
PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Cache directory can't be written");
return;
Expand Down Expand Up @@ -514,3 +526,226 @@ PHP_METHOD(Phalcon_Cache_Backend_File, exists){
RETURN_MM_FALSE;
}

PHP_METHOD(Phalcon_Cache_Backend_File, increment){

zval *key_name, *value = NULL, *lifetime = NULL, *options, *prefix, *prefixed_key, *result, *status;
zval *cache_dir, *cache_file, *frontend, *timestamp;
zval *ttl = NULL, *modified_time, *difference, *not_expired;
zval *cached_content, *exception_message;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 1, &key_name, &value);

if (!value) {
PHALCON_INIT_VAR(value);
} else {
PHALCON_SEPARATE_PARAM(value);
}

if (Z_TYPE_P(value) == IS_NULL) {
ZVAL_LONG(value, 1);
}

PHALCON_OBS_VAR(options);
phalcon_read_property_this(&options, this_ptr, SL("_options"), PH_NOISY_CC);

PHALCON_OBS_VAR(prefix);
phalcon_read_property_this(&prefix, this_ptr, SL("_prefix"), PH_NOISY_CC);

PHALCON_INIT_VAR(prefixed_key);
PHALCON_CONCAT_VV(prefixed_key, prefix, key_name);
phalcon_update_property_this(this_ptr, SL("_lastKey"), prefixed_key TSRMLS_CC);

PHALCON_OBS_VAR(cache_dir);
phalcon_array_fetch_string(&cache_dir, options, SL("cacheDir"), PH_NOISY);

PHALCON_INIT_VAR(cache_file);
PHALCON_CONCAT_VV(cache_file, cache_dir, prefixed_key);

if (phalcon_file_exists(cache_file TSRMLS_CC) == SUCCESS) {

PHALCON_OBS_VAR(frontend);
phalcon_read_property_this(&frontend, this_ptr, SL("_frontend"), PH_NOISY_CC);

/**
* Check if the file has expired
*/
PHALCON_INIT_VAR(timestamp);
ZVAL_LONG(timestamp, (long) time(NULL));

/**
* Take the lifetime from the frontend or read it from the set in start()
*/
PHALCON_INIT_VAR(lifetime);
if (Z_TYPE_P(lifetime) == IS_NULL) {

PHALCON_OBS_NVAR(lifetime);
phalcon_read_property_this(&lifetime, this_ptr, SL("_lastLifetime"), PH_NOISY_CC);
if (Z_TYPE_P(lifetime) == IS_NULL) {
PHALCON_INIT_VAR(ttl);
phalcon_call_method(ttl, frontend, "getlifetime");
} else {
PHALCON_CPY_WRT(ttl, lifetime);
}
} else {
PHALCON_CPY_WRT(ttl, lifetime);
}

PHALCON_INIT_VAR(modified_time);
phalcon_call_func_p1(modified_time, "filemtime", cache_file);

PHALCON_INIT_VAR(difference);
sub_function(difference, timestamp, ttl TSRMLS_CC);

PHALCON_INIT_VAR(not_expired);
is_smaller_function(not_expired, difference, modified_time TSRMLS_CC);

/**
* The content is only retrieved if the content has not expired
*/
if (PHALCON_IS_TRUE(not_expired)) {

/**
* Use file-get-contents to control that the openbase_dir can't be skipped
*/
PHALCON_INIT_VAR(cached_content);
phalcon_file_get_contents(cached_content, cache_file TSRMLS_CC);
if (PHALCON_IS_FALSE(cached_content)) {
PHALCON_INIT_VAR(exception_message);
PHALCON_CONCAT_SVS(exception_message, "Cache file ", cache_file, " could not be opened");
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_cache_exception_ce, exception_message);
return;
}

if (phalcon_is_numeric(cached_content)) {
PHALCON_INIT_VAR(result);
add_function(result, value, cached_content TSRMLS_CC);

PHALCON_INIT_VAR(status);
phalcon_file_put_contents(status, cache_file, result TSRMLS_CC);

if (PHALCON_IS_FALSE(status)) {
PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Cache directory can't be written");
return;
}

RETURN_ZVAL(result, 1, 0);
}
}
}

RETURN_MM_NULL();
}

PHP_METHOD(Phalcon_Cache_Backend_File, decrement){

zval *key_name, *value = NULL, *lifetime = NULL, *options, *prefix, *prefixed_key, *result, *status;
zval *cache_dir, *cache_file, *frontend, *timestamp;
zval *ttl = NULL, *modified_time, *difference, *not_expired;
zval *cached_content, *exception_message;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 1, &key_name, &value);

if (!value) {
PHALCON_INIT_VAR(value);
} else {
PHALCON_SEPARATE_PARAM(value);
}

if (Z_TYPE_P(value) == IS_NULL) {
ZVAL_LONG(value, 1);
}

PHALCON_OBS_VAR(options);
phalcon_read_property_this(&options, this_ptr, SL("_options"), PH_NOISY_CC);

PHALCON_OBS_VAR(prefix);
phalcon_read_property_this(&prefix, this_ptr, SL("_prefix"), PH_NOISY_CC);

PHALCON_INIT_VAR(prefixed_key);
PHALCON_CONCAT_VV(prefixed_key, prefix, key_name);
phalcon_update_property_this(this_ptr, SL("_lastKey"), prefixed_key TSRMLS_CC);

PHALCON_OBS_VAR(cache_dir);
phalcon_array_fetch_string(&cache_dir, options, SL("cacheDir"), PH_NOISY);

PHALCON_INIT_VAR(cache_file);
PHALCON_CONCAT_VV(cache_file, cache_dir, prefixed_key);

if (phalcon_file_exists(cache_file TSRMLS_CC) == SUCCESS) {

PHALCON_OBS_VAR(frontend);
phalcon_read_property_this(&frontend, this_ptr, SL("_frontend"), PH_NOISY_CC);

/**
* Check if the file has expired
*/
PHALCON_INIT_VAR(timestamp);
ZVAL_LONG(timestamp, (long) time(NULL));

/**
* Take the lifetime from the frontend or read it from the set in start()
*/
PHALCON_INIT_VAR(lifetime);
if (Z_TYPE_P(lifetime) == IS_NULL) {

PHALCON_OBS_NVAR(lifetime);
phalcon_read_property_this(&lifetime, this_ptr, SL("_lastLifetime"), PH_NOISY_CC);
if (Z_TYPE_P(lifetime) == IS_NULL) {
PHALCON_INIT_VAR(ttl);
phalcon_call_method(ttl, frontend, "getlifetime");
} else {
PHALCON_CPY_WRT(ttl, lifetime);
}
} else {
PHALCON_CPY_WRT(ttl, lifetime);
}

PHALCON_INIT_VAR(modified_time);
phalcon_call_func_p1(modified_time, "filemtime", cache_file);

PHALCON_INIT_VAR(difference);
sub_function(difference, timestamp, ttl TSRMLS_CC);

PHALCON_INIT_VAR(not_expired);
is_smaller_function(not_expired, difference, modified_time TSRMLS_CC);

/**
* The content is only retrieved if the content has not expired
*/
if (PHALCON_IS_TRUE(not_expired)) {

/**
* Use file-get-contents to control that the openbase_dir can't be skipped
*/
PHALCON_INIT_VAR(cached_content);
phalcon_file_get_contents(cached_content, cache_file TSRMLS_CC);
if (PHALCON_IS_FALSE(cached_content)) {
PHALCON_INIT_VAR(exception_message);
PHALCON_CONCAT_SVS(exception_message, "Cache file ", cache_file, " could not be opened");
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_cache_exception_ce, exception_message);
return;
}

if (phalcon_is_numeric(cached_content)) {
PHALCON_INIT_VAR(result);
sub_function(result, cached_content, value TSRMLS_CC);

PHALCON_INIT_VAR(status);
phalcon_file_put_contents(status, cache_file, result TSRMLS_CC);

if (PHALCON_IS_FALSE(status)) {
PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Cache directory can't be written");
return;
}

RETURN_ZVAL(result, 1, 0);
}
}
}

RETURN_MM_NULL();
}
16 changes: 15 additions & 1 deletion ext/cache/backend/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ PHP_METHOD(Phalcon_Cache_Backend_File, save);
PHP_METHOD(Phalcon_Cache_Backend_File, delete);
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);

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_cache_backend_file___construct, 0, 0, 1)
ZEND_ARG_INFO(0, frontend)
Expand Down Expand Up @@ -58,13 +60,25 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_cache_backend_file_exists, 0, 0, 0)
ZEND_ARG_INFO(0, lifetime)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_cache_backend_file_increment, 0, 0, 0)
ZEND_ARG_INFO(0, keyName)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_cache_backend_file_decrement, 0, 0, 0)
ZEND_ARG_INFO(0, keyName)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()

PHALCON_INIT_FUNCS(phalcon_cache_backend_file_method_entry){
PHP_ME(Phalcon_Cache_Backend_File, __construct, arginfo_phalcon_cache_backend_file___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(Phalcon_Cache_Backend_File, get, arginfo_phalcon_cache_backend_file_get, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_File, save, arginfo_phalcon_cache_backend_file_save, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Cache_Backend_File, delete, arginfo_phalcon_cache_backend_file_delete, ZEND_ACC_PUBLIC)
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, 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_FE_END
};

Loading