BoldGrid / w3-total-cache

GNU General Public License v2.0
151 stars 81 forks source link

Fixed PageCache File Cleaner using wrong expire value #889

Closed jacobd91 closed 3 weeks ago

jacobd91 commented 4 weeks ago

This PR simply changes the cleanup_local method which calls the Cache_File_Cleaner_Generic under the Disk: Enhanced engine to use the PageCache Lifetime setting instead of the Browser Cache HTML Lifetime setting

Additionally, I've reviewed the Garbage Collection process and it appears that it does use the PageCache Garbage Collection Interval setting and will both rename expired cached files to _old and remove expired _old files

function _clean( $path, $remove = false ) {
    $dir = false;
    if ( is_dir( $path ) ) {
    $dir = @opendir( $path );
    }
    if ( $dir ) {
    while ( ( $entry = @readdir( $dir ) ) !== false ) {
            if ( $entry == '.' || $entry == '..' ) {
        continue;
        }
        $full_path = $path . DIRECTORY_SEPARATOR . $entry;
            if ( substr( $entry, -4 ) === '_old' && !$this->is_old_file_expired( $full_path ) ) {
        continue;
        }
        foreach ( $this->_exclude as $mask ) {
                if ( fnmatch( $mask, basename( $entry ) ) ) {
            continue 2;
        }
            }
        if ( @is_dir( $full_path ) ) {
                $this->_clean( $full_path );
        } else {
        $this->_clean_file( $entry, $full_path );
        }
    }
        @closedir( $dir );
    if ( $this->is_empty_dir( $path ) )
        @rmdir( $path );
    }
}

function _clean_file( $entry, $full_path ) {
    if ( substr( $entry, -4 ) === '_old' ) {
    $this->processed_count++;
    @unlink( $full_path );
    } elseif ( !$this->is_valid( $full_path ) ) {
    $old_entry_path = $full_path . '_old';
    $this->processed_count++;
    if ( !@rename( $full_path, $old_entry_path ) ) {
        // if we can delete old entry -
        // do second attempt to store in old-entry file
            if ( @unlink( $old_entry_path ) ) {
        if ( !@rename( $full_path, $old_entry_path ) ) {
            // last attempt - just remove entry
            @unlink( $full_path );
        }
        }
    }
    }
}

function is_valid( $file ) {
    if ( $this->_expire <= 0 )
    return false;
    if ( file_exists( $file ) ) {
    $ftime = @filemtime( $file );
    if ( $ftime && $ftime > ( time() - $this->_expire ) ) {
        return true;
    }
    }
    return false;
}

function is_old_file_expired( $file ) {
    $ftime = @filemtime( $file );
    $expire = $this->_expire ? $this->_expire * 5 : W3TC_CACHE_FILE_EXPIRE_MAX;
    if ( $ftime && $ftime < ( time() - $expire ) ) {
    return true;
    }
    return false;
}
codecov-commenter commented 4 weeks ago

Codecov Report

Attention: Patch coverage is 0% with 1 line in your changes missing coverage. Please review.

Project coverage is 4.21%. Comparing base (3d1b00c) to head (212c478).

Files Patch % Lines
PgCache_Plugin_Admin.php 0.00% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #889 +/- ## ======================================== Coverage 4.21% 4.21% Complexity 21594 21594 ======================================== Files 873 873 Lines 95182 95182 ======================================== Hits 4010 4010 Misses 91172 91172 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.