Simple PHP Cache Manager Class
This class can be used to cache arbitrary data in files.
It can check if the cache with a certain key already exists. If it exists and it is not expired, it can return the cached data. Otherwise it can store newly generated data in a cache file with the given key.
The class can be configured to set the cache life time and the root directory of where all the cache files will be stored.
cachemanager.php
< ?php class CacheManager extends Entity { /** * Contains number of seconds - timeframe for storing cache * @http://www.easemarry.com/blog * @var int * @name timeframe * @access private */ var $timeframe; /** * Contains name of the cache root directory * * @var string * @name _cacheRoot * @access public */ var $_cacheRoot; /** * Constructor, defines timeframe for storing cache * * @param int number of seconds * @param int number of minutes * @param int number of hours * @param int number of days * @param int number of month * @param int number of years */ function CacheManager ($second = 0, $minute = 0, $hour = 0, $day = 0, $month = 0, $year = 0) { $this->timeframe = mktime (2 + $hour, 0 + $minute, 0 + $second, 1 + $month, 1 + $day, 1970 + $year); } /** * Checks if cache contains file no older than timeframe * * @return string */ function CheckCache ($model, $key = 0, $fname = '', $timeframe = 0) { $result = 0; $filename = ($fname ? $fname : $this->_cacheRoot.strtolower($model).'/'.($key ? $key : 'model').'.html'); if ( file_exists ($filename) ) { $tframe = ($timeframe ? $timeframe : $this->timeframe); if (time() - filemtime ($filename) < $tframe) $result = $this->RetrieveData ($filename); } return $result; } /** * Read the content of the file * * @param string filename * @return string */ function RetrieveData ($filename) { $fp = fopen ($filename, 'r'); $content = fread ($fp, filesize ($filename)); fclose ($fp); return $content; } /** * Store content in the file * * @return string */ function SaveToCache ($model, $key = 0, $content = '', $fname = '') { $filename = ($fname ? $fname : $this->_cacheRoot.strtolower($model).'/'.($key ? $key : 'model').'.html'); $fp = fopen ($filename, 'w+'); fwrite ($fp, $content); fclose ($fp); return $filename; } } ?> |
example.php
< ?php /** * This example represents how easily we can automate * caching of both news list, and single news using * CacheManager class */ function GetElement ($key) { /* return single news */ return 'Full News Number '.$key; } function GetElements () { /* return news list */ for ($i=1; $i<11; $i++) $result .= '<a href="./?id='.$i.'">News '.$i.'<br / />'; return $result; } include './cachemanager.php'; $key = (isset($_GET['id']) ? intval($_GET['id']) : 0); $CacheManager = &new CacheManager (0, 5); $CacheManager->_cacheRoot = './cache/'; $content = $CacheManager->CheckCache ('news', $key); if (!$content) { $content = ($key ? GetElement ($key) : $this->GetElements()); $CacheManager->SaveToCache ('news', $key, $content); } echo $content; ?> |
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments
No comments yet.
Leave a comment