* @license PHP License * @package WB * @subpackage base */ /** * load base class */ WBClass::load( 'WBCache_Driver' ); /** * Caching: cache driver file * * Stores everything in filesystem * * @version 0.1.2 * @package WB * @subpackage base */ class WBCache_Driver_File extends WBCache_Driver { /** * Cache Folder * @var string */ protected $_cacheDir; /** * Constructor * * Load index file */ public function __construct() { $this->_cacheDir = WBParam::get( 'wb/dir/base' ) . '/var/cache/general'; if (!is_dir($this->_cacheDir)) { mkdir($this->_cacheDir, 0777, true); chmod($this->_cacheDir, 0777); } $file = $this->_cacheDir . '/_index'; if (file_exists($file)) { $this->_index = unserialize(file_get_contents($file)); } } /** * Destruct * * Save index file */ public function __destruct() { $file = $this->_cacheDir . '/_index'; $chmod = true; if (file_exists($file)) { $chmod = false; } file_put_contents($file, serialize($this->_index)); if ($chmod) { chmod($file, 0666); } } /** * Fetch Cached Data * * @param string $id cache id * @return mixed return data or null if not cached yet */ public function get($id) { $file = $this->_cacheDir . '/' . $id; if (!file_exists($file)) { return null; } $cnt = file_get_contents($file); return unserialize($cnt); } /** * Store Data in Cache * * @param string $id cache id * @return mixed return data or null if not cached yet */ public function set($id, $data) { $cnt = serialize($data); $file = $this->_cacheDir . '/' . $id; $chmod = true; if (file_exists($file)) { $chmod = false; } file_put_contents($file, $cnt); if ($chmod) { chmod($file, 0666); } return true; } /** * Flush Cached Data * * @param string $id cache id * @return bool true on success */ public function flush($id) { $file = $this->_cacheDir . '/' . $id; if (!file_exists($file)) { return true; } unlink($file); return true; } }