added getRelatedCache()
authorTim Starling <tstarling@users.mediawiki.org>
Tue, 5 Sep 2006 14:44:50 +0000 (14:44 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Tue, 5 Sep 2006 14:44:50 +0000 (14:44 +0000)
includes/Title.php

index 7eae801..cf79780 100644 (file)
@@ -2244,6 +2244,46 @@ class Title {
                }
        }
 
+       /**
+        * Get the last touched timestamp
+        */
+       function getTouched() {
+               $dbr =& wfGetDB( DB_SLAVE );
+               $touched = $dbr->selectField( 'page', 'page_touched',
+                       array( 
+                               'page_namespace' => $this->getNamespace(),
+                               'page_title' => $this->getDBkey()
+                       ), __METHOD__
+               );
+               return $touched;
+       }
+
+       /**
+        * Get a cached value from a global cache that is invalidated when this page changes
+        * @param string $key the key
+        * @param callback $callback A callback function which generates the value on cache miss
+        */
+       function getRelatedCache( $memc, $key, $expiry, $callback, $params = array() ) {
+               $touched = $this->getTouched();
+               $cacheEntry = $memc->get( $key );
+               if ( $cacheEntry ) {
+                       if ( $cacheEntry['touched'] >= $touched ) {
+                               return $cacheEntry['value'];
+                       } else {
+                               wfDebug( __METHOD__.": $key expired\n" );
+                       }
+               } else {
+                       wfDebug( __METHOD__.": $key not found\n" );
+               }
+               $value = call_user_func_array( $callback, $params );
+               $cacheEntry = array(
+                       'value' => $value,
+                       'touched' => $touched
+               );
+               $memc->set( $key, $cacheEntry, $expiry );
+               return $value;
+       }
+
        function trackbackURL() {
                global $wgTitle, $wgScriptPath, $wgServer;