Come from:PHPCache, a simple, powerful object caching solution for PHP applications
Purpose: cache the result of that piece of code for as long as you want.
Download PHPCache
The example code:
=======================================================
require_once 'phpcache.class.php';
$database = array(
'type' => 'mysql',
'name' => 'YOUR DATABASE NAME',
'table' => 'PHPCache',
'user' => 'YOUR DATABASE USERNAME',
'pass' => 'YOUR DATABASE USERNAME\'S PASSWORD',
'host' => 'localhost' /* Or maybe IP address of your database server */
);
PHPCache::configure($database);
$cache = PHPCache::instance();
//$cache->create_table(); /* Call this the first time to create the table and then comment it out... */
if( ($results = $cache->get('result_of_some_nasty_code')) !== false ) {
$tpl->assign($results);
/* Or maybe return $results or whatever depending on where you use this */
} else {
/***********************
* Your slow code here
***********************/
$cache->store('result_of_some_nasty_code', $results_of_your_slow_code, PHPCACHE_1_HOUR * 5); /* Cache for 5 hours */
}
===========================================================================
Notice!
1, You need to use a database to store the cache info.
replace the info of your own database in $database above:
$database = array(
'type' => 'mysql',
'name' => 'YOUR DATABASE NAME',
'table' => 'PHPCache',
'user' => 'YOUR DATABASE USERNAME',
'pass' => 'YOUR DATABASE USERNAME\'S PASSWORD',
'host' => 'localhost' /* Or maybe IP address of your database server */
);
2, The table PHPCache will be created automatically by the code
$cache->create_table();
so when you first use this page, remove the '//' before '$cache->create_table();'
3, Date constants
PHPCACHE_1_SECOND
PHPCACHE_1_MINUTE
PHPCACHE_1_HOUR
PHPCACHE_1_DAY
PHPCACHE_1_WEEK
PHPCACHE_1_MONTH
PHPCACHE_1_YEAR
Example:PHPCACHE_1_HOUR * 5 /* Cache for 5 hours */
没有评论:
发表评论