April 25, 2024

PHP caching and .htaccess caching

A cache is a collection of duplicate data, where the original data is expensive to fetch or compute (usually in terms of access time) relative to the cache. In PHP, caching is used to minimize page generation time. PHP basically has two main types of caching: ‘output caching’ and ‘parser caching’. PHP ‘output caching’ saves a chunk of data somewhere that can later be read by another script faster than it can generate it. ‘Parser caching’ is a specific feature. PHP is a scripting language and code is not compiled or optimized to a particular computer. Every PHP file must be parsed and that takes time. This type of time minimization is ‘parser caching’.

The simplest way to implement caching in php, just update your .htaccess file. Here is my current .htaccess caching:

<IfModule mod_headers.c>
    # Cache Media Files
    <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$">
        Header set Cache-Control "public"
        Header set Expires "Mon, 20 Apr 2015 20:00:00 GMT"
        Header unset Last-Modified
    </FilesMatch>

    # Cache JavaScript & CSS
    <FilesMatch "\.(js|css)$">
        Header set Cache-Control "public"
        Header set Expires "Mon, 20 Apr 2015 20:00:00 GMT"
        Header unset Last-Modified
    </FilesMatch>

    # Disable Caching for Scripts and Other Dynamic Files
    <FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$">
        Header unset Cache-Control
    </FilesMatch>
</IfModule>

After updating the .htaccess file, dont forget to restart your httpd/apache server.

Vedant Kumar

Currently I'm working as an Implementation Engineer, Started my career as an System Administrator - Linux. Additionally loves to explore new technologies and research about new open-source software that ease the development cycle.

View all posts by Vedant Kumar →

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

close

Ad Blocker Detected!

VEDANT EXPLAINS
We've noticed that you are using an ad blocker. Advertising helps fund our server cost and keep it truly independent. It helps to build our content creator team. So please disable your ad blocker, and help us to keep providing you with free- great content - for free. Thank you for your support.

Refresh