* @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; unlink( $file ); return true; } } ?>