Skip to content

Commit 4cf00c0

Browse files
committedApr 9, 2013
Fixed issue #26 (added opcache_invalidate(string $filename [, bool $force = false]) function)
1 parent d9c9e78 commit 4cf00c0

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
 

‎ext/opcache/ZendAccelerator.c

+49
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,55 @@ static inline char *accel_make_persistent_key(zend_file_handle *file_handle, int
10471047
return accel_make_persistent_key_ex(file_handle, strlen(file_handle->filename), key_len TSRMLS_CC);
10481048
}
10491049

1050+
int zend_accel_invalidate(const char *filename, int filename_len, zend_bool force TSRMLS_DC)
1051+
{
1052+
char *realpath;
1053+
zend_persistent_script *persistent_script;
1054+
1055+
if (!ZCG(enabled) || !accel_startup_ok || !ZCSG(accelerator_enabled) || accelerator_shm_read_lock(TSRMLS_C) != SUCCESS) {
1056+
return FAILURE;
1057+
}
1058+
1059+
#if ZEND_EXTENSION_API_NO < PHP_5_3_X_API_NO
1060+
realpath = accel_php_resolve_path(filename, filename_len, ZCG(include_path) TSRMLS_CC);
1061+
#else
1062+
realpath = accelerator_orig_zend_resolve_path(filename, filename_len TSRMLS_CC);
1063+
#endif
1064+
1065+
persistent_script = zend_accel_hash_find(&ZCSG(hash), realpath, strlen(realpath) + 1);
1066+
if (persistent_script && !persistent_script->corrupted) {
1067+
zend_file_handle file_handle;
1068+
1069+
file_handle.type = ZEND_HANDLE_FILENAME;
1070+
file_handle.filename = realpath;
1071+
file_handle.opened_path = realpath;
1072+
1073+
if (force ||
1074+
!ZCG(accel_directives).validate_timestamps ||
1075+
do_validate_timestamps(persistent_script, &file_handle TSRMLS_CC) == FAILURE) {
1076+
SHM_UNPROTECT();
1077+
zend_shared_alloc_lock(TSRMLS_C);
1078+
if (!persistent_script->corrupted) {
1079+
persistent_script->corrupted = 1;
1080+
persistent_script->timestamp = 0;
1081+
ZSMMG(wasted_shared_memory) += persistent_script->dynamic_members.memory_consumption;
1082+
if (ZSMMG(memory_exhausted)) {
1083+
zend_accel_restart_reason reason =
1084+
zend_accel_hash_is_full(&ZCSG(hash)) ? ACCEL_RESTART_HASH : ACCEL_RESTART_OOM;
1085+
zend_accel_schedule_restart_if_necessary(reason TSRMLS_CC);
1086+
}
1087+
}
1088+
zend_shared_alloc_unlock(TSRMLS_C);
1089+
SHM_PROTECT();
1090+
}
1091+
}
1092+
1093+
accelerator_shm_read_unlock(TSRMLS_C);
1094+
efree(realpath);
1095+
1096+
return SUCCESS;
1097+
}
1098+
10501099
/* Adds another key for existing cached script */
10511100
static void zend_accel_add_key(char *key, unsigned int key_length, zend_accel_hash_entry *bucket TSRMLS_DC)
10521101
{

‎ext/opcache/ZendAccelerator.h

+1
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ extern char *zps_api_failure_reason;
318318

319319
void zend_accel_schedule_restart(zend_accel_restart_reason reason TSRMLS_DC);
320320
void zend_accel_schedule_restart_if_necessary(zend_accel_restart_reason reason TSRMLS_DC);
321+
int zend_accel_invalidate(const char *filename, int filename_len, zend_bool force TSRMLS_DC);
321322
int accelerator_shm_read_lock(TSRMLS_D);
322323
void accelerator_shm_read_unlock(TSRMLS_D);
323324

‎ext/opcache/zend_accelerator_module.c

+21
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static void (*orig_is_readable)(INTERNAL_FUNCTION_PARAMETERS) = NULL;
4343

4444
/* User functions */
4545
static ZEND_FUNCTION(opcache_reset);
46+
static ZEND_FUNCTION(opcache_invalidate);
4647

4748
/* Private functions */
4849
static ZEND_FUNCTION(opcache_get_status);
@@ -51,6 +52,7 @@ static ZEND_FUNCTION(opcache_get_configuration);
5152
static zend_function_entry accel_functions[] = {
5253
/* User functions */
5354
ZEND_FE(opcache_reset, NULL)
55+
ZEND_FE(opcache_invalidate, NULL)
5456
/* Private functions */
5557
ZEND_FE(opcache_get_configuration, NULL)
5658
ZEND_FE(opcache_get_status, NULL)
@@ -643,3 +645,22 @@ static ZEND_FUNCTION(opcache_reset)
643645
zend_accel_schedule_restart(ACCEL_RESTART_USER TSRMLS_CC);
644646
RETURN_TRUE;
645647
}
648+
649+
/* {{{ proto void opcache_invalidate(string $script [, bool $force = false])
650+
Invalidates cached script (in necessary or forced) */
651+
static ZEND_FUNCTION(opcache_invalidate)
652+
{
653+
char *script_name;
654+
int script_name_len;
655+
zend_bool force = 0;
656+
657+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &script_name, &script_name_len, &force) == FAILURE) {
658+
return;
659+
}
660+
661+
if (zend_accel_invalidate(script_name, script_name_len, force) == SUCCESS) {
662+
RETURN_TRUE;
663+
} else {
664+
RETURN_FALSE;
665+
}
666+
}

0 commit comments

Comments
 (0)