Php cache dynamic index page

I found phpfastcahce class for MySQL cached result. in details that support WinCache, MemCache, files, X-Cache, APC Cache and say:

PHP Caching Class For Database : Your website have 10,000 visitors who are online, and your dynamic page have to send 10,000 same queries to database on every page load. With phpFastCache, your page only send 1 query to DB, and use the cache to serve 9,999 other visitors.

in the example code:

<?php
    // In your config file
    include("php_fast_cache.php");
    // This is Optional Config only. You can skip these lines.
    // phpFastCache support "apc", "memcache", "memcached", "wincache" ,"files", "pdo", "mpdo" and "xcache"
    // You don't need to change your code when you change your caching system. Or simple keep it auto
    phpFastCache::$storage = "auto";
    // End Optionals

    // In your Class, Functions, PHP Pages
    // 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 = 10 minutes
        phpFastCache::set("products_page",$products,600);
    }

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

      

NOW, In my index of my site I have some block to show the latest news, best news, world news ..... for my index cache, I have to cache the result MySQL

for each block (last news, best news, world news ..... )

using phpfastcache and in the admin page delete all cache if I edit existing news or add new news? is this the true way?

What's the best way Cache MySQL

returns my index page using phpfastcache (any method) ?!

+3


source to share


1 answer


phpfastcache cant uderstand your data has been changed or not

you have to do something after changing certain data in the database

first in your home page caching code it should be something like this:

$lastnews = phpFastCache::get('index_lastnews');
$bestnews = phpFastCache::get('index_bestnews');
$worldnews = phpFastCache::get('index_worldnews');

if($lastnews == null) {
    $lastnews = YOUR DB QUERIES || GET_DATA_FUNCTION;
    phpFastCache::set('index_lastnews',$lastnews,600);
}
if($bestnews == null) {
    $bestnews = YOUR DB QUERIES || GET_DATA_FUNCTION;
    phpFastCache::set('index_bestnews',$bestnews,600);
}

      

... ,.

and in your admin page, when the specific data changed the cache code should be like this:



AFTER DATABASE insert | update ....

      

you can replace the old cache in two ways:

1) delete the cache (after deleting the cache, the cache is automatically restored after the first visit)

phpFastCache::delete('index_lastnews');

      

2) update cache

$lastnews =   YOUR DB QUERIES || GET_DATA_FUNCTION;
phpFastCache::set("index_lastnews",$lastnews,600);

      

+3


source







All Articles