2

I am using php-fpm 7.4.3 on ubuntu 20.04. My blacklist of opcache doesn't work at all. Scripts I visited are cached when file_cache is enabled. Here's my configures.

php.ini

opcache.save_comments=0
opcache.blacklist_filename="/etc/php/opcache_blacklist.txt"
opcache.file_cache="/tmp/php"
opcache.file_cache_fallback=0

opcache_blacklist.txt

/*
/**
/**.php
/*.php
*.php
**
**.php

test.php

<pre>
<?php
print_r(opcache_get_status(true));
?>
</pre>

Some information from test.php

[used_memory] => 9821016
[num_cached_scripts] => 44
[num_cached_keys] => 45
[max_cached_keys] => 16229
[blacklist_misses] => 2
[scripts] => Array
(
    ....
)

There is a strange thing: everytime I refresh the test.php, blacklist_misses is increased by 1.

If I disable file_cache (opcache.file_cache=) in php.ini, num_cached_scripts and num_cached_keys become 0, blacklist_misses keeps increasing, blacklist_miss_ratio is always 100.

PHP documentation doesn't tell me that file_cache will affect blacklist, is this by design or a bug?

shingo
  • 18,436
  • 5
  • 23
  • 42

1 Answers1

1

You are getting the expected bahavior. Basically you told php to execlude everything from caching!.

A normal blacklist file would look like:

/var/www/html/notcachedfile.php
/var/www/html/notcachedfolder/*

So file notcachedfile.php and everything in notcachedfolder will not be cached.

A cache miss means PHP tries to retrieve data from the opcache, but that specific data is not currently in the opcache

So the reason why blacklist_misses is increased everytime you refresh your test.php file is because it's matched by the pattern you provided in your opcache_blacklist.txt

/* 

Which means everything that is part of the / (The root directory) including your test.php file will not be in the cache, hence it shows a cache miss everytime.

Rain
  • 3,416
  • 3
  • 24
  • 40
  • Yes I attempted to exclude everything, but **opcache_get_status** returns **num_cached_scripts** > 0, every php scripts I accessed are in the **scripts** array. I think this means files are cached. – shingo Aug 20 '22 at 04:50
  • @shingo `file_cache` will not affect your blacklist. PHP will start caching files once you visit them. So when you disable `file_cache` and then restart your server the cache will be cleared. That's why when you visit your `test.php` you see 0 in `num_cached_scripts` . To make things clear don't exclude everything try to exclude only `test.php` and then after you restart your server try to open a non-excluded file and after that open your `test.php` you will see `num_cached_scripts` becomes 1 and the `blacklist_misses` will keep incrementing. – Rain Aug 20 '22 at 10:24
  • Why I will see files are cached when file_cache is enabled? It doesn't make sense because my blacklist should exclusive them all. – shingo Aug 20 '22 at 10:46
  • What I expected is both in memory cache and file cache should filter files listed in the blacklist file, but it seems like the list is ignored. – shingo Aug 20 '22 at 10:51
  • @shingo It seems `file_cache` will not be cleared by PHP. That means files which are not in SHM, but in the file cache, are read from the file cache, unless they are stale. So I think this is what you have experienced. Try to set `opcache.revalidate_freq=0` and see if this resolves the issue. If not maybe your cache file contains files that are not stale yet. – Rain Aug 20 '22 at 15:31
  • This is nearly correct, I found opcache will add cached files from file cache to memory without checking the blacklist file. These files are never changed, so they won't be stale. I fixed the problem by deleting the file cache folder. Thanks for your help. – shingo Aug 21 '22 at 07:04