Skip to content

Commit

Permalink
opcache_is_script_cached: file_cache option
Browse files Browse the repository at this point in the history
  • Loading branch information
iamacarpet committed Nov 28, 2024
1 parent 8ac8ec4 commit 62fe89c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
4 changes: 2 additions & 2 deletions ext/opcache/ZendAccelerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1916,7 +1916,7 @@ static zend_op_array *file_cache_compile_file(zend_file_handle *file_handle, int

HANDLE_BLOCK_INTERRUPTIONS();
SHM_UNPROTECT();
persistent_script = zend_file_cache_script_load(file_handle);
persistent_script = zend_file_cache_script_load(file_handle, false);
SHM_PROTECT();
HANDLE_UNBLOCK_INTERRUPTIONS();
if (persistent_script) {
Expand Down Expand Up @@ -2133,7 +2133,7 @@ zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type)

/* Check the second level cache */
if (!persistent_script && ZCG(accel_directives).file_cache) {
persistent_script = zend_file_cache_script_load(file_handle);
persistent_script = zend_file_cache_script_load(file_handle, false);
}

/* If script was not found or invalidated by validate_timestamps */
Expand Down
2 changes: 1 addition & 1 deletion ext/opcache/opcache.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ function opcache_jit_blacklist(Closure $closure): void {}
*/
function opcache_get_configuration(): array|false {}

function opcache_is_script_cached(string $filename): bool {}
function opcache_is_script_cached(string $filename, bool $file_cache = false): bool {}
5 changes: 4 additions & 1 deletion ext/opcache/opcache_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 37 additions & 4 deletions ext/opcache/zend_accelerator_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "zend_closures.h"
#include "zend_shared_alloc.h"
#include "zend_accelerator_blacklist.h"
#include "zend_file_cache.h"
#include "php_ini.h"
#include "SAPI.h"
#include "zend_virtual_cwd.h"
Expand Down Expand Up @@ -363,6 +364,33 @@ static int filename_is_in_cache(zend_string *filename)
return 0;
}

static int filename_is_in_file_cache(zend_string *filename)
{
if (!ZCG(accel_directives).file_cache) {
return 0;
}

zend_file_handle *handle;

zend_stream_init_filename_ex(handle, filename);

if (!handle->opened_path) {
zend_destroy_file_handle(handle);
return 0;
}

zend_persistent_script *persistent_script = zend_file_cache_script_load(handle, true)

zend_destroy_file_handle(handle);

if (persistent_script) {
return 1;
}

return 0;
}


static int accel_file_in_cache(INTERNAL_FUNCTION_PARAMETERS)
{
if (ZEND_NUM_ARGS() == 1) {
Expand Down Expand Up @@ -983,10 +1011,11 @@ ZEND_FUNCTION(opcache_compile_file)
ZEND_FUNCTION(opcache_is_script_cached)
{
zend_string *script_name;
bool file_cache = 0;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(script_name)
ZEND_PARSE_PARAMETERS_END();
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|b", &script_name, &file_cache) == FAILURE) {
RETURN_THROWS();
}

if (!validate_api_restriction()) {
RETURN_FALSE;
Expand All @@ -996,5 +1025,9 @@ ZEND_FUNCTION(opcache_is_script_cached)
RETURN_FALSE;
}

RETURN_BOOL(filename_is_in_cache(script_name));
if (file_cache) {
RETURN_BOOL(filename_is_in_file_cache(script_name));
} else {
RETURN_BOOL(filename_is_in_cache(script_name));
}
}
3 changes: 2 additions & 1 deletion ext/opcache/zend_file_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -1813,7 +1813,7 @@ static void zend_file_cache_unserialize(zend_persistent_script *script,
zend_file_cache_unserialize_early_bindings(script, buf);
}

zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handle)
zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handle, bool force_file_cache_only)
{
zend_string *full_path = file_handle->opened_path;
int fd;
Expand Down Expand Up @@ -1928,6 +1928,7 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
}

if (!file_cache_only &&
!force_file_cache_only &&
!ZCSG(restart_in_progress) &&
!ZCSG(restart_pending) &&
!ZSMMG(memory_exhausted) &&
Expand Down
2 changes: 1 addition & 1 deletion ext/opcache/zend_file_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#define ZEND_FILE_CACHE_H

int zend_file_cache_script_store(zend_persistent_script *script, bool in_shm);
zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handle);
zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handle, bool force_file_cache_only);
void zend_file_cache_invalidate(zend_string *full_path);

#endif /* ZEND_FILE_CACHE_H */

0 comments on commit 62fe89c

Please sign in to comment.