0

There is another thread similar to this that was closed and that didn't have any useful information in it: https://stackoverflow.com/questions/11955822/php-file-caching-vs-cache-through-htaccess

Is it necessary to implement a php caching system if you are caching through .htaccess? 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>

with this file caching, will building out a php caching system improve my site even more? Or would it make more sense to compress data in .htaccess and use php to cache? I'm just trying to understand which method of caching will improve a site more or if using both is recommended.

2 Answers 2

1

For static files, you can cache them by HTML Headers tags, and .htaccess The browsers will cache them in local machines.

For Dynamic content with .PHP, you can cache widget, objects to reduce the query call to mysql database.

You can try this one. Example, it cache $products in 600 seconds, and your PHP only send 1 request to database. If you have like 500 visitors online, your page still use 1 query from first visitors to serve 500.

<?php
    include("php_fast_cache.php");
    // try to get from Cache first.
    $products = phpFastCache::get("products_page");

    if($products == null) {
        $products = YOUR DB QUERIES || GET_PRODUCTS_FUNCTION;

        // set products in to cache in 600 seconds = 5 minutes
        phpFastCache::set("products_page",$products,600);
    }

    foreach($products as $product) {
        // Output Your Contents HERE
    }
?>

If you are using Wordpress, you can see all the cache plugins, they cache your content by PHP ( Files or Memcache ), and cache your images, css, js by .htaccess

We need both of them together will speed up the site and save bandwidth / CPU

Sign up to request clarification or add additional context in comments.

1 Comment

Your explanation of how it gets 500 products with 1 request was spot on. In that one line it explained everything. Now I see it visually as someone doing a google search for a recipe, and then printing it and pinning it to the wall. Its quicker to visit the wall and read it there (the cache), then to do a google search using the computer and printing it again! thanks.......
0

You're doing client-side caching for static files only.

Caching in PHP solves a completely different problem - server-side performace issues of your application. So you should use it if your site is loading too slowly or if you're causing high server load.

There are many strategies how to implement server-side caching and it's up to you what fits best your application.

For example you can cache SQL queries results or you can cache HTML output of whole webpages. Do not forget about cache invalidation when your data changes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.