Skip to content

Commit 56f1c82

Browse files
committed
opcache_is_script_cached: file_cache option
1 parent 8ac8ec4 commit 56f1c82

6 files changed

+52
-10
lines changed

ext/opcache/ZendAccelerator.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1916,7 +1916,7 @@ static zend_op_array *file_cache_compile_file(zend_file_handle *file_handle, int
19161916

19171917
HANDLE_BLOCK_INTERRUPTIONS();
19181918
SHM_UNPROTECT();
1919-
persistent_script = zend_file_cache_script_load(file_handle);
1919+
persistent_script = zend_file_cache_script_load(file_handle, false);
19201920
SHM_PROTECT();
19211921
HANDLE_UNBLOCK_INTERRUPTIONS();
19221922
if (persistent_script) {
@@ -2133,7 +2133,7 @@ zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type)
21332133

21342134
/* Check the second level cache */
21352135
if (!persistent_script && ZCG(accel_directives).file_cache) {
2136-
persistent_script = zend_file_cache_script_load(file_handle);
2136+
persistent_script = zend_file_cache_script_load(file_handle, false);
21372137
}
21382138

21392139
/* If script was not found or invalidated by validate_timestamps */

ext/opcache/opcache.stub.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ function opcache_jit_blacklist(Closure $closure): void {}
2222
*/
2323
function opcache_get_configuration(): array|false {}
2424

25-
function opcache_is_script_cached(string $filename): bool {}
25+
function opcache_is_script_cached(string $filename, bool $file_cache = false): bool {}

ext/opcache/opcache_arginfo.h

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/opcache/zend_accelerator_module.c

+42-4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
#include <time.h>
2323

2424
#include "php.h"
25+
#include "zend.h"
2526
#include "ZendAccelerator.h"
2627
#include "zend_API.h"
2728
#include "zend_closures.h"
2829
#include "zend_shared_alloc.h"
2930
#include "zend_accelerator_blacklist.h"
31+
#include "zend_file_cache.h"
3032
#include "php_ini.h"
3133
#include "SAPI.h"
3234
#include "zend_virtual_cwd.h"
@@ -363,6 +365,37 @@ static int filename_is_in_cache(zend_string *filename)
363365
return 0;
364366
}
365367

368+
static int filename_is_in_file_cache(zend_string *filename)
369+
{
370+
if (!ZCG(accel_directives).file_cache) {
371+
return 0;
372+
}
373+
374+
zend_string *realpath;
375+
376+
realpath = zend_resolve_path(filename);
377+
378+
if (!realpath) {
379+
return 0;
380+
}
381+
382+
zend_file_handle handle;
383+
384+
zend_stream_init_filename_ex(&handle, filename);
385+
handle.opened_path = realpath;
386+
387+
zend_persistent_script *persistent_script = zend_file_cache_script_load(&handle, true);
388+
389+
zend_destroy_file_handle(&handle);
390+
391+
if (persistent_script) {
392+
return 1;
393+
}
394+
395+
return 0;
396+
}
397+
398+
366399
static int accel_file_in_cache(INTERNAL_FUNCTION_PARAMETERS)
367400
{
368401
if (ZEND_NUM_ARGS() == 1) {
@@ -983,10 +1016,11 @@ ZEND_FUNCTION(opcache_compile_file)
9831016
ZEND_FUNCTION(opcache_is_script_cached)
9841017
{
9851018
zend_string *script_name;
1019+
bool file_cache = 0;
9861020

987-
ZEND_PARSE_PARAMETERS_START(1, 1)
988-
Z_PARAM_STR(script_name)
989-
ZEND_PARSE_PARAMETERS_END();
1021+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|b", &script_name, &file_cache) == FAILURE) {
1022+
RETURN_THROWS();
1023+
}
9901024

9911025
if (!validate_api_restriction()) {
9921026
RETURN_FALSE;
@@ -996,5 +1030,9 @@ ZEND_FUNCTION(opcache_is_script_cached)
9961030
RETURN_FALSE;
9971031
}
9981032

999-
RETURN_BOOL(filename_is_in_cache(script_name));
1033+
if (file_cache) {
1034+
RETURN_BOOL(filename_is_in_file_cache(script_name));
1035+
} else {
1036+
RETURN_BOOL(filename_is_in_cache(script_name));
1037+
}
10001038
}

ext/opcache/zend_file_cache.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1813,7 +1813,7 @@ static void zend_file_cache_unserialize(zend_persistent_script *script,
18131813
zend_file_cache_unserialize_early_bindings(script, buf);
18141814
}
18151815

1816-
zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handle)
1816+
zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handle, bool force_file_cache_only)
18171817
{
18181818
zend_string *full_path = file_handle->opened_path;
18191819
int fd;
@@ -1928,6 +1928,7 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
19281928
}
19291929

19301930
if (!file_cache_only &&
1931+
!force_file_cache_only &&
19311932
!ZCSG(restart_in_progress) &&
19321933
!ZCSG(restart_pending) &&
19331934
!ZSMMG(memory_exhausted) &&

ext/opcache/zend_file_cache.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#define ZEND_FILE_CACHE_H
2121

2222
int zend_file_cache_script_store(zend_persistent_script *script, bool in_shm);
23-
zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handle);
23+
zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handle, bool force_file_cache_only);
2424
void zend_file_cache_invalidate(zend_string *full_path);
2525

2626
#endif /* ZEND_FILE_CACHE_H */

0 commit comments

Comments
 (0)