PHP caching and .htaccess caching

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.

PHP caching and .htaccess cachingphp caching techniques
Comments (0)
Add Comment