CacheDependency.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. /**
  3. * This class stores an arbitrary value along with its dependencies.
  4. * Users should typically only use DependencyWrapper::getFromCache(), rather
  5. * than instantiating one of these objects directly.
  6. * @ingroup Cache
  7. */
  8. class DependencyWrapper {
  9. var $value;
  10. var $deps;
  11. /**
  12. * Create an instance.
  13. * @param mixed $value The user-supplied value
  14. * @param mixed $deps A dependency or dependency array. All dependencies
  15. * must be objects implementing CacheDependency.
  16. */
  17. function __construct( $value = false, $deps = array() ) {
  18. $this->value = $value;
  19. if ( !is_array( $deps ) ) {
  20. $deps = array( $deps );
  21. }
  22. $this->deps = $deps;
  23. }
  24. /**
  25. * Returns true if any of the dependencies have expired
  26. */
  27. function isExpired() {
  28. foreach ( $this->deps as $dep ) {
  29. if ( $dep->isExpired() ) {
  30. return true;
  31. }
  32. }
  33. return false;
  34. }
  35. /**
  36. * Initialise dependency values in preparation for storing. This must be
  37. * called before serialization.
  38. */
  39. function initialiseDeps() {
  40. foreach ( $this->deps as $dep ) {
  41. $dep->loadDependencyValues();
  42. }
  43. }
  44. /**
  45. * Get the user-defined value
  46. */
  47. function getValue() {
  48. return $this->value;
  49. }
  50. /**
  51. * Store the wrapper to a cache
  52. */
  53. function storeToCache( $cache, $key, $expiry = 0 ) {
  54. $this->initialiseDeps();
  55. $cache->set( $key, $this, $expiry );
  56. }
  57. /**
  58. * Attempt to get a value from the cache. If the value is expired or missing,
  59. * it will be generated with the callback function (if present), and the newly
  60. * calculated value will be stored to the cache in a wrapper.
  61. *
  62. * @param object $cache A cache object such as $wgMemc
  63. * @param string $key The cache key
  64. * @param integer $expiry The expiry timestamp or interval in seconds
  65. * @param mixed $callback The callback for generating the value, or false
  66. * @param array $callbackParams The function parameters for the callback
  67. * @param array $deps The dependencies to store on a cache miss. Note: these
  68. * are not the dependencies used on a cache hit! Cache hits use the stored
  69. * dependency array.
  70. *
  71. * @return mixed The value, or null if it was not present in the cache and no
  72. * callback was defined.
  73. */
  74. static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
  75. $callbackParams = array(), $deps = array() )
  76. {
  77. $obj = $cache->get( $key );
  78. if ( is_object( $obj ) && $obj instanceof DependencyWrapper && !$obj->isExpired() ) {
  79. $value = $obj->value;
  80. } elseif ( $callback ) {
  81. $value = call_user_func_array( $callback, $callbackParams );
  82. # Cache the newly-generated value
  83. $wrapper = new DependencyWrapper( $value, $deps );
  84. $wrapper->storeToCache( $cache, $key, $expiry );
  85. } else {
  86. $value = null;
  87. }
  88. return $value;
  89. }
  90. }
  91. /**
  92. * @ingroup Cache
  93. */
  94. abstract class CacheDependency {
  95. /**
  96. * Returns true if the dependency is expired, false otherwise
  97. */
  98. abstract function isExpired();
  99. /**
  100. * Hook to perform any expensive pre-serialize loading of dependency values.
  101. */
  102. function loadDependencyValues() {}
  103. }
  104. /**
  105. * @ingroup Cache
  106. */
  107. class FileDependency extends CacheDependency {
  108. var $filename, $timestamp;
  109. /**
  110. * Create a file dependency
  111. *
  112. * @param string $filename The name of the file, preferably fully qualified
  113. * @param mixed $timestamp The unix last modified timestamp, or false if the
  114. * file does not exist. If omitted, the timestamp will be loaded from
  115. * the file.
  116. *
  117. * A dependency on a nonexistent file will be triggered when the file is
  118. * created. A dependency on an existing file will be triggered when the
  119. * file is changed.
  120. */
  121. function __construct( $filename, $timestamp = null ) {
  122. $this->filename = $filename;
  123. $this->timestamp = $timestamp;
  124. }
  125. function loadDependencyValues() {
  126. if ( is_null( $this->timestamp ) ) {
  127. if ( !file_exists( $this->filename ) ) {
  128. # Dependency on a non-existent file
  129. # This is a valid concept!
  130. $this->timestamp = false;
  131. } else {
  132. $this->timestamp = filemtime( $this->filename );
  133. }
  134. }
  135. }
  136. function isExpired() {
  137. if ( !file_exists( $this->filename ) ) {
  138. if ( $this->timestamp === false ) {
  139. # Still nonexistent
  140. return false;
  141. } else {
  142. # Deleted
  143. wfDebug( "Dependency triggered: {$this->filename} deleted.\n" );
  144. return true;
  145. }
  146. } else {
  147. $lastmod = filemtime( $this->filename );
  148. if ( $lastmod > $this->timestamp ) {
  149. # Modified or created
  150. wfDebug( "Dependency triggered: {$this->filename} changed.\n" );
  151. return true;
  152. } else {
  153. # Not modified
  154. return false;
  155. }
  156. }
  157. }
  158. }
  159. /**
  160. * @ingroup Cache
  161. */
  162. class TitleDependency extends CacheDependency {
  163. var $titleObj;
  164. var $ns, $dbk;
  165. var $touched;
  166. /**
  167. * Construct a title dependency
  168. * @param Title $title
  169. */
  170. function __construct( Title $title ) {
  171. $this->titleObj = $title;
  172. $this->ns = $title->getNamespace();
  173. $this->dbk = $title->getDBkey();
  174. }
  175. function loadDependencyValues() {
  176. $this->touched = $this->getTitle()->getTouched();
  177. }
  178. /**
  179. * Get rid of bulky Title object for sleep
  180. */
  181. function __sleep() {
  182. return array( 'ns', 'dbk', 'touched' );
  183. }
  184. function getTitle() {
  185. if ( !isset( $this->titleObj ) ) {
  186. $this->titleObj = Title::makeTitle( $this->ns, $this->dbk );
  187. }
  188. return $this->titleObj;
  189. }
  190. function isExpired() {
  191. $touched = $this->getTitle()->getTouched();
  192. if ( $this->touched === false ) {
  193. if ( $touched === false ) {
  194. # Still missing
  195. return false;
  196. } else {
  197. # Created
  198. return true;
  199. }
  200. } elseif ( $touched === false ) {
  201. # Deleted
  202. return true;
  203. } elseif ( $touched > $this->touched ) {
  204. # Updated
  205. return true;
  206. } else {
  207. # Unmodified
  208. return false;
  209. }
  210. }
  211. }
  212. /**
  213. * @ingroup Cache
  214. */
  215. class TitleListDependency extends CacheDependency {
  216. var $linkBatch;
  217. var $timestamps;
  218. /**
  219. * Construct a dependency on a list of titles
  220. */
  221. function __construct( LinkBatch $linkBatch ) {
  222. $this->linkBatch = $linkBatch;
  223. }
  224. function calculateTimestamps() {
  225. # Initialise values to false
  226. $timestamps = array();
  227. foreach ( $this->getLinkBatch()->data as $ns => $dbks ) {
  228. if ( count( $dbks ) > 0 ) {
  229. $timestamps[$ns] = array();
  230. foreach ( $dbks as $dbk => $value ) {
  231. $timestamps[$ns][$dbk] = false;
  232. }
  233. }
  234. }
  235. # Do the query
  236. if ( count( $timestamps ) ) {
  237. $dbr = wfGetDB( DB_SLAVE );
  238. $where = $this->getLinkBatch()->constructSet( 'page', $dbr );
  239. $res = $dbr->select( 'page',
  240. array( 'page_namespace', 'page_title', 'page_touched' ),
  241. $where, __METHOD__ );
  242. while ( $row = $dbr->fetchObject( $res ) ) {
  243. $timestamps[$row->page_namespace][$row->page_title] = $row->page_touched;
  244. }
  245. }
  246. return $timestamps;
  247. }
  248. function loadDependencyValues() {
  249. $this->timestamps = $this->calculateTimestamps();
  250. }
  251. function __sleep() {
  252. return array( 'timestamps' );
  253. }
  254. function getLinkBatch() {
  255. if ( !isset( $this->linkBatch ) ){
  256. $this->linkBatch = new LinkBatch;
  257. $this->linkBatch->setArray( $this->timestamps );
  258. }
  259. return $this->linkBatch;
  260. }
  261. function isExpired() {
  262. $newTimestamps = $this->calculateTimestamps();
  263. foreach ( $this->timestamps as $ns => $dbks ) {
  264. foreach ( $dbks as $dbk => $oldTimestamp ) {
  265. $newTimestamp = $newTimestamps[$ns][$dbk];
  266. if ( $oldTimestamp === false ) {
  267. if ( $newTimestamp === false ) {
  268. # Still missing
  269. } else {
  270. # Created
  271. return true;
  272. }
  273. } elseif ( $newTimestamp === false ) {
  274. # Deleted
  275. return true;
  276. } elseif ( $newTimestamp > $oldTimestamp ) {
  277. # Updated
  278. return true;
  279. } else {
  280. # Unmodified
  281. }
  282. }
  283. }
  284. return false;
  285. }
  286. }
  287. /**
  288. * @ingroup Cache
  289. */
  290. class GlobalDependency extends CacheDependency {
  291. var $name, $value;
  292. function __construct( $name ) {
  293. $this->name = $name;
  294. $this->value = $GLOBALS[$name];
  295. }
  296. function isExpired() {
  297. return $GLOBALS[$this->name] != $this->value;
  298. }
  299. }
  300. /**
  301. * @ingroup Cache
  302. */
  303. class ConstantDependency extends CacheDependency {
  304. var $name, $value;
  305. function __construct( $name ) {
  306. $this->name = $name;
  307. $this->value = constant( $name );
  308. }
  309. function isExpired() {
  310. return constant( $this->name ) != $this->value;
  311. }
  312. }