joomla-framework / filesystem

Joomla Framework Filesystem Package
GNU General Public License v2.0
9 stars 15 forks source link

[RFC] OpCode Cache purge on delete, copy, move, etc #19

Closed andrepereiradasilva closed 3 years ago

andrepereiradasilva commented 6 years ago

Request For Comments

Hi!

As we all know, php 7.x ships with opcache. It would be nice if the filesystem library invalidates the opcache files whenever there is an operation that changes the op code cached content.

I personaly don't like opcache_reset since AFAIK it resets the cache for all php instance (which can have multiple sites/application hosted), so probably the opcache_invalidate (and alternatives caching) could be a better alternative.

This process actually happens in joomla core restore part https://github.com/joomla/joomla-cms/blob/staging/administrator/components/com_joomlaupdate/restore.php#L8315 but AFAIK does exist in joomla as a whole.

Unless i'm missing something (which is probable ...) adding a method like this to the filesystem framework package and purging the opcache on php file overwrite would allow for a more agressive opcahing server config with joomla, and thus better joomla performance.

Example of more agressive opcaching:

opcache.validate_timestamps = 0     ; Default value "1" | 0 for no timestamp validation
opcache.revalidate_freq     = 60    ; Default value "2" | 0 for always revalidate | x for seconds
opcache.revalidate_path     = 0     ; Default value "0" | 1 for always revalidate

http://php.net/manual/en/opcache.configuration.php

So, something like this called at every changed php file, maybe could do the job:

public function purgeFileFromOpCodeCache(string $filePath)
{
    if (function_exists('opcache_invalidate'))
    {
        return opcache_invalidate($filePath);
    }

    if (function_exists('apc_compile_file'))
    {
        return apc_compile_file($filePath);
    }

    if (function_exists('wincache_refresh_if_changed'))
    {
        return wincache_refresh_if_changed([$filePath]);
    }

    if (function_exists('xcache_asm'))
    {
        return (bool) xcache_asm($filePath);
    }

    return false;
}
mbabker commented 6 years ago

Looks fine to me. Just know you don't need XCache support on PHP 7+ as the extension hasn't been updated in years.

zero-24 commented 5 years ago

Is there a reason this has not resulted into an PR against this repo?

So, something like this called at every changed php file, maybe could do the job:

Just to be sure should we only do this on PHP files? And the other way around should we implement extension based special things into this package?

nibra commented 3 years ago

The filesystem package does not actively support any caching. Thus this package is not the right place to handle cache invalidation. Also, the proposed approach would force us to add every known and not yet known cache solution to the purgeFileFromOpCodeCache() method, violating the Open Closed Principle (the O in SOLID).

However, the use case might be valuable. The right way to approach this is to add events to this package, so other services can react on them. But that's a bigger story that deserves its own RfC.