I found phpfastcahce class for cached MySQL 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 sample 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 website I have any block for show last news, best news, world news ..... for cache my index, i must cache MySQL result for each block(last news, best news, world news ..... ) using phpfastcache and in admin page remove all cache if I edit existing news Or add new news? this is a true way?
what's best way For cache MySQL result my index page using phpfastcache(any method)?!
phpfastcache cant uderstand your data was changed or not
you must do something after specific data changed in your database
first in your home page cache code must be 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 specific data changed cache code must be like this :
AFTER DATABASE insert | update ....
you can replace old cache by this two way :
1) delete cache (after delete cache , cache automatically rebuild after first visit)
phpFastCache::delete('index_lastnews');
2) update cache
$lastnews = YOUR DB QUERIES || GET_DATA_FUNCTION;
phpFastCache::set("index_lastnews",$lastnews,600);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With