1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- /**
- * Cache with file system
- */
- class FileCache extends CacheAbstract{
- protected $cacheDirCreated; // boolean to avoid always chck dir cache existance
- public function loadData(){
- $this->isPrepareCache();
- $datas = json_decode(file_get_contents($this->getCacheFile()),true);
- $items = array();
- foreach($datas as $aData){
- $item = new \Item();
- foreach($aData as $name => $value){
- $item->$name = $value;
- }
- $items[] = $item;
- }
- return $items;
- }
- public function saveData($datas){
- $this->isPrepareCache();
- file_put_contents($this->getCacheFile(), json_encode($datas));
- return $this;
- }
- public function getTime(){
- $this->isPrepareCache();
- $cacheFile = $this->getCacheFile();
- if( file_exists($cacheFile) ){
- return filemtime($cacheFile);
- }
- return false;
- }
- /**
- * Cache is prepared ?
- * Note : Cache name is based on request information, then cache must be prepare before use
- * @return \Exception|true
- */
- protected function isPrepareCache(){
- if( is_null($this->param) ){
- throw new \Exception('Please feed "prepare" method before try to load');
- }
- return true;
- }
- /**
- * Return cache path (and create if not exist)
- * @return string Cache path
- */
- protected function getCachePath(){
- $cacheDir = __DIR__ . '/../cache/'; // FIXME : configuration ?
- // FIXME : implement recursive dir creation
- if( is_null($this->cacheDirCreated) && !is_dir($cacheDir) ){
- $this->cacheDirCreated = true;
- mkdir($cacheDir,0705);
- chmod($cacheDir,0705);
- }
- return $cacheDir;
- }
- /**
- * Get the file name use for cache store
- * @return string Path to the file cache
- */
- protected function getCacheFile(){
- return $this->getCachePath() . $this->getCacheName();
- }
- /**
- * Determines file name for store the cache
- * return string
- */
- protected function getCacheName(){
- $this->isPrepareCache();
- $stringToEncode = $_SERVER['REQUEST_URI'] . http_build_query($this->param);
- return hash('sha1', $stringToEncode) . '.cache';
- }
- }
|