lightbulb Joomla Performance: clean JCH's Cache Files with Cron or Manually with PHP

Topic Rating


08 Nov 2017 06:42 - 08 Nov 2017 06:50 #1 by Trony

  • Administrator
  • Administrator
  • Location: Roma
  • Time Zone: UTC +2:00
  • Local Time: 11:13

  • Posts: 939
  • Profile views: 3260
  • Thank you received: 8

  • Register Date: 01 Jul 2008
  • Last login: 22 Sep 2021 22:50
  • Joomla Performance: clean JCH's Cache Files with Cron or Manually with PHP was created by Trony



    JCH Optimize is a Very Good Plugin for to Optimize and Speed Up Joomla.
    In the Basic Options, You have Three Modes of use for Combined files delivery:

    By default the combined files will be loaded as static css and javascript files. You would need to include directives in your .htaccess file to gzip these files. You can use PHP files instead that will be gzipped if that option is set. PHP files can be loaded with a query attached with the information to find the combined files, or you can use url rewrite if it's available on the server so the files can be masked as static files. If your server prohibits the use of the Options +FollowSymLinks directive in .htaccess files use the respective option.



    Static CSS and Javascript Files [Standard Mode]


    In this mode, JCH will create Two Folders:
    cache > plg_jch_optimize
    media > plg_jchoptimize > cache

    Now if You have a Limited Disk Space on your Hosting, can be a problem because those folders can contain thousands of files if Your Website is Medium / Big.
    With Standard JCH Clean, You can erase ALL Files but it's my Opinion that it would be a waste of resources because the plugin must Restart ALL Work.

    Delete Files older than X Days with Cron


    I modded, expanded and adapted this PHP Script .
    You have to create in the root of Your WebSite a PHP file called cleanJCH.php (for Example) and You paste in it this:
    <?php
    $days = 3;
    $dir = '/home/YOUR_FOLDER/public_html/cache/plg_jch_optimize';
     
        if ($handle = opendir($dir)) {
        while (( $file = readdir($handle)) !== false ) {
            if ( $file == '.' || $file == '..' || is_dir($dir.'/'.$file) ) {
                continue;
            }
     
            if ((time() - filemtime($dir.'/'.$file)) > ($days *86400)) {
                unlink($dir.'/'.$file);
            }
        }
        closedir($handle);
    }
    
    $daysJCH = 3; 
    $dirJCH = '/home/YOUR_FOLDER/public_html/media/plg_jchoptimize/cache';  
     
        if ($handleJCH = opendir($dirJCH)) {
        while (( $fileJCH = readdir($handleJCH)) !== false ) {
            if ( $fileJCH == '.' || $fileJCH == '..' || is_dir($dirJCH.'/'.$fileJCH) ) {
                continue;
            }
     
            if ((time() - filemtime($dirJCH.'/'.$fileJCH)) > ($daysJCH *86400)) {
                unlink($dirJCH.'/'.$fileJCH);
            }
        }
        closedir($handleJCH);
    }
    ?>

    I used Absolute Path for $dir and $dirJCH but You can use Relative Path if You Want.

    Now You must to go on Your Hosting Control Panel and You create a Cron Process.
    I have configured it to work every day at 3am:
    0 3 * * * curl --silent –compressed 'http://www.YOUR_WEBSITE.COM/cleanJCH.php'

    Delete Files older than X Days Manually


    <?php
    $days = 3;
    $dir = '/home/YOUR_FOLDER/public_html/cache/plg_jch_optimize';
     
    $nofiles = 0;
     
        if ($handle = opendir($dir)) {
        while (( $file = readdir($handle)) !== false ) {
            if ( $file == '.' || $file == '..' || is_dir($dir.'/'.$file) ) {
                continue;
            }
     
            if ((time() - filemtime($dir.'/'.$file)) > ($days *86400)) {
                $nofiles++;
                unlink($dir.'/'.$file);
            }
        }
        closedir($handle);
        echo "Total files deleted in Joomla Cache Folder: $nofiles \n";
    }
    
    $daysJCH = 3; 
    $dirJCH = '/home/YOUR_FOLDER/public_html/media/plg_jchoptimize/cache';  
      
    $nofilesJCH = 0;
     
        if ($handleJCH = opendir($dirJCH)) {
        while (( $fileJCH = readdir($handleJCH)) !== false ) {
            if ( $fileJCH == '.' || $fileJCH == '..' || is_dir($dirJCH.'/'.$fileJCH) ) {
                continue;
            }
     
            if ((time() - filemtime($dirJCH.'/'.$fileJCH)) > ($daysJCH *86400)) {
                $nofilesJCH++;
                unlink($dirJCH.'/'.$fileJCH);
            }
        }
        closedir($handleJCH);
        echo "Total files deleted in JCH Cache Folder: $nofilesJCH \n";
    }
    ?>
    It's so similar to Cron Version but for to work You must use the Browser with a Url like this:
    http://www.YOUR_WEBSITE.COM/cleanJCH.php

    Conclusion


    With this PHP Script, You erase ONLY the files older than 3 days.
    Of Course, You can change this value in 7 Days (example) if You Want.
    You must to change this Value Strings: $days and $daysJCH.

    I suggest using Equal Value for a more correct work of JCH Optimize.
    Music and Electronic Devices Designer. Drum and Bass Addict. Creative Mind as Lifestyle. Cat in past Life. Soccer, Ice Hockey and Snowboard Lover. Marxist.
    Sono Il Re sul Carro Danzante. Attraversando La Città Getto Via I Coriandoli Della Vostra Tristezza Regalando Passione Ed Estasi Nei Vostri Occhi.
    Follow Trony on: FusoElektronique | Facebook | Twitter | Soundcloud
    Last edit: 08 Nov 2017 06:50 by Trony.

    Please Log in or Create an account to join the conversation.

    Moderators: Trony

    You might also be interested...

    FusoElektronique - Pure Electronic Music.

    Community and Web Radio of Electronic Music since July 2008. Rome, Italy

    Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0) 2021.

    About us | Our team | Cookie Policy | Privacy Policy | Terms and Conditions | Contact us | Sitemap