File modules/util/cache/FileCache.class.php

Last commit: Sun Nov 1 00:36:50 2020 +0100	Jan Dankert	Refactoring: Only using the configuration object.
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 }
Download modules/util/cache/FileCache.class.php
History Sun, 1 Nov 2020 00:36:50 +0100 Jan Dankert Refactoring: Only using the configuration object. Mon, 28 Sep 2020 21:07:21 +0200 Jan Dankert Cleanup: Removing unused code. Sat, 26 Sep 2020 13:11:23 +0200 Jan Dankert Refactoring: No global variables any more. All constants are capsulated by classes. Sat, 26 Sep 2020 04:26:55 +0200 Jan Dankert Refactoring: read configuration values with a class. Sat, 26 Sep 2020 02:26:39 +0200 Jan Dankert Refactoring: No global functions any more, the database object is read from the Db class. Mon, 21 Sep 2020 22:48:59 +0200 Jan Dankert Complexe refactoring: Moving all generation logic from the model (Value,Page,File) to generators classes. Sat, 22 Feb 2020 23:58:02 +0100 Jan Dankert Refactoring: Namespacing for module 'util'. Sat, 18 May 2019 22:37:59 +0200 Jan Dankert FileCache deaktiviert, da durchaus problematisch, wenn sich abhängige Objekte ändern. Sat, 18 May 2019 22:27:04 +0200 Jan Dankert New: Filecache mit Ablauf-Zeitpunkt. Mon, 13 May 2019 23:35:07 +0200 Jan Dankert Einfache Umbenennung.