openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs | README

FileCache.class.php (2931B)


      1 <?php
      2 
      3 namespace util\cache;
      4 
      5 use cms\base\Configuration as C;
      6 use cms\base\DB;
      7 use cms\base\Startup;
      8 use util\FileUtils;
      9 
     10 
     11 /**
     12  * File-based cache.
     13  */
     14 class FileCache implements Cache
     15 {
     16 	const CACHE_FILENAME_PREFIX = 'openrat-cache';
     17 
     18 	/**
     19 	 * A loader which gets the value
     20      * @var Callable
     21      */
     22     private $loader;
     23 
     24     /**
     25 	 * Filename of the cache.
     26 	 *
     27      * @var string
     28      */
     29     private $filename;
     30 
     31     /**
     32      * Creates a new Cache entry.
     33      *
     34      * @param $key array|string Cache-Key
     35      * @param $loader Callable
     36      */
     37     public function __construct( $key, $loader, $lastModified = 0 )
     38     {
     39         if   ( !is_array($key))
     40         	$key = [ (string) $key ];
     41         $key = array_merge([ DB::get()->id ], $key);
     42 
     43         $filename  = FileUtils::getTempDir() . '/'. self::CACHE_FILENAME_PREFIX;
     44         $filename .= array_reduce($key,function($carry,$item){
     45         	return $carry.'-'.$item;
     46 		});
     47         $filename .= '.tmp';
     48 
     49         $this->filename = $filename;
     50         $this->loader   = $loader;
     51 
     52         if   ( C::subset('publishing')->is('cache_enabled',false) )
     53             $this->invalidateIfOlderThan( $lastModified );
     54         else
     55             $this->invalidateIfOlderThan( Startup::getStartTime() ); // Invalidate all before this request.
     56     }
     57 
     58 
     59 	/**
     60 	 * Invalidates the cache entry, if it is older than a specific date.
     61 	 * @param $invalidateIfOlderDate
     62 	 */
     63 	public function invalidateIfOlderThan($invalidateIfOlderDate) {
     64 
     65         if   ( is_file($this->filename) && filemtime($this->filename) < $invalidateIfOlderDate )
     66             $this->invalidate();
     67     }
     68 
     69 
     70     /**
     71      * Invalidates a cache entry.
     72      */
     73     public function invalidate() {
     74 
     75         if   ( is_file($this->filename))
     76             // Should use '@' here to deny race conditions, where another request is calling this method.
     77             @unlink( $this->filename);
     78     }
     79 
     80 
     81     /**
     82      * Get the content. Loads the value if nessecary.
     83      */
     84     public function get() {
     85 
     86         if   ( ! is_file($this->filename)) {
     87             file_put_contents($this->filename,call_user_func($this->loader) );
     88         }
     89 
     90         return file_get_contents($this->filename);
     91     }
     92 
     93 
     94     /**
     95      * Makes sure that the value is loaded.
     96 	 *
     97 	 * @return FileCache
     98      */
     99     public function load() {
    100 
    101         if   ( ! is_file($this->filename)) {
    102             file_put_contents($this->filename,call_user_func($this->loader) );
    103         }
    104 
    105         return $this;
    106     }
    107 
    108 
    109 	/**
    110 	 * Refreshes the cache.
    111 	 *
    112 	 * @return $this
    113 	 */
    114     public function refresh() {
    115 
    116 		file_put_contents($this->filename,call_user_func($this->loader) );
    117 
    118 		return $this;
    119 	}
    120 
    121 
    122 	/**
    123 	 * Gets the filename of the cache value.
    124 	 * Warning: The file may not exist. If you want to make sure that the file exists, you have to call #load() first.
    125 	 *
    126 	 * @return string filename of cache content
    127 	 */
    128     public function getFilename() {
    129         return $this->filename;
    130     }
    131 
    132 }