Index: trunk/phase3/includes/specials/SpecialCachedPage.php |
— | — | @@ -1,7 +1,8 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | /** |
5 | | - * Abstract special page class with scaffolding for caching the HTML output. |
| 5 | + * Abstract special page class with scaffolding for caching HTML and other values |
| 6 | + * in a single blob. |
6 | 7 | * |
7 | 8 | * Before using any of the cahing functionality, call startCache. |
8 | 9 | * After the last call to either getCachedValue or addCachedHTML, call saveCache. |
— | — | @@ -75,6 +76,24 @@ |
76 | 77 | } |
77 | 78 | |
78 | 79 | /** |
| 80 | + * Get a cached value if available or compute it if not and then cache it if possible. |
| 81 | + * The provided $computeFunction is only called when the computation needs to happen |
| 82 | + * and should return a result value. $args are arguments that will be passed to the |
| 83 | + * compute function when called. |
| 84 | + * |
| 85 | + * @since 1.20 |
| 86 | + * |
| 87 | + * @param {function} $computeFunction |
| 88 | + * @param array|mixed $args |
| 89 | + * @param string|null $key |
| 90 | + * |
| 91 | + * @return mixed |
| 92 | + */ |
| 93 | + public function getCachedValue( $computeFunction, $args = array(), $key = null ) { |
| 94 | + return $this->cacheHelper->getCachedValue( $computeFunction, $args, $key ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
79 | 98 | * Add some HTML to be cached. |
80 | 99 | * This is done by providing a callback function that should |
81 | 100 | * return the HTML to be added. It will only be called if the |
Index: trunk/phase3/includes/AutoLoader.php |
— | — | @@ -259,6 +259,7 @@ |
260 | 260 | 'ZipDirectoryReader' => 'includes/ZipDirectoryReader.php', |
261 | 261 | |
262 | 262 | # includes/actions |
| 263 | + 'CachedAction' => 'includes/actions/CachedAction.php', |
263 | 264 | 'CreditsAction' => 'includes/actions/CreditsAction.php', |
264 | 265 | 'DeleteAction' => 'includes/actions/DeleteAction.php', |
265 | 266 | 'EditAction' => 'includes/actions/EditAction.php', |
Index: trunk/phase3/includes/CacheHelper.php |
— | — | @@ -1,5 +1,13 @@ |
2 | 2 | <?php |
3 | 3 | |
| 4 | +/** |
| 5 | + * Interface for all classes implementing CacheHelper functionality. |
| 6 | + * |
| 7 | + * @since 1.20 |
| 8 | + * |
| 9 | + * @licence GNU GPL v2 or later |
| 10 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 11 | + */ |
4 | 12 | interface ICacheHelper { |
5 | 13 | |
6 | 14 | /** |
— | — | @@ -22,18 +30,20 @@ |
23 | 31 | function startCache( $cacheExpiry = null, $cacheEnabled = null ); |
24 | 32 | |
25 | 33 | /** |
26 | | - * Add some HTML to be cached. |
27 | | - * This is done by providing a callback function that should |
28 | | - * return the HTML to be added. It will only be called if the |
29 | | - * item is not in the cache yet or when the cache has been invalidated. |
| 34 | + * Get a cached value if available or compute it if not and then cache it if possible. |
| 35 | + * The provided $computeFunction is only called when the computation needs to happen |
| 36 | + * and should return a result value. $args are arguments that will be passed to the |
| 37 | + * compute function when called. |
30 | 38 | * |
31 | 39 | * @since 1.20 |
32 | 40 | * |
33 | 41 | * @param {function} $computeFunction |
34 | | - * @param array $args |
| 42 | + * @param array|mixed $args |
35 | 43 | * @param string|null $key |
| 44 | + * |
| 45 | + * @return mixed |
36 | 46 | */ |
37 | | - function addCachedHTML( $computeFunction, $args = array(), $key = null ); |
| 47 | + function getCachedValue( $computeFunction, $args = array(), $key = null ); |
38 | 48 | |
39 | 49 | /** |
40 | 50 | * Saves the HTML to the cache in case it got recomputed. |
— | — | @@ -73,7 +83,6 @@ |
74 | 84 | * @since 1.20 |
75 | 85 | * |
76 | 86 | * @file CacheHelper.php |
77 | | - * @ingroup SpecialPage |
78 | 87 | * |
79 | 88 | * @licence GNU GPL v2 or later |
80 | 89 | * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
— | — | @@ -216,25 +225,7 @@ |
217 | 226 | } |
218 | 227 | } |
219 | 228 | |
220 | | - |
221 | | - |
222 | 229 | /** |
223 | | - * Add some HTML to be cached. |
224 | | - * This is done by providing a callback function that should |
225 | | - * return the HTML to be added. It will only be called if the |
226 | | - * item is not in the cache yet or when the cache has been invalidated. |
227 | | - * |
228 | | - * @since 1.20 |
229 | | - * |
230 | | - * @param {function} $computeFunction |
231 | | - * @param array $args |
232 | | - * @param string|null $key |
233 | | - */ |
234 | | - public function addCachedHTML( $computeFunction, $args = array(), $key = null ) { |
235 | | - $this->getOutput()->addHTML( $this->getCachedValue( $computeFunction, $args, $key ) ); |
236 | | - } |
237 | | - |
238 | | - /** |
239 | 230 | * Get a cached value if available or compute it if not and then cache it if possible. |
240 | 231 | * The provided $computeFunction is only called when the computation needs to happen |
241 | 232 | * and should return a result value. $args are arguments that will be passed to the |
Index: trunk/phase3/includes/actions/CachedAction.php |
— | — | @@ -0,0 +1,161 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Abstract action class with scaffolding for caching HTML and other values |
| 6 | + * in a single blob. |
| 7 | + * |
| 8 | + * Before using any of the cahing functionality, call startCache. |
| 9 | + * After the last call to either getCachedValue or addCachedHTML, call saveCache. |
| 10 | + * |
| 11 | + * To get a cached value or compute it, use getCachedValue like this: |
| 12 | + * $this->getCachedValue( $callback ); |
| 13 | + * |
| 14 | + * To add HTML that should be cached, use addCachedHTML like this: |
| 15 | + * $this->addCachedHTML( $callback ); |
| 16 | + * |
| 17 | + * The callback function is only called when needed, so do all your expensive |
| 18 | + * computations here. This function should returns the HTML to be cached. |
| 19 | + * It should not add anything to the PageOutput object! |
| 20 | + * |
| 21 | + * @since 1.20 |
| 22 | + * |
| 23 | + * @file CachedAction.php |
| 24 | + * @ingroup Action |
| 25 | + * |
| 26 | + * @licence GNU GPL v2 or later |
| 27 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 28 | + */ |
| 29 | +abstract class CachedAction extends FormlessAction implements ICacheHelper { |
| 30 | + |
| 31 | + /** |
| 32 | + * CacheHelper object to which we foreward the non-SpecialPage specific caching work. |
| 33 | + * Initialized in startCache. |
| 34 | + * |
| 35 | + * @since 1.20 |
| 36 | + * @var CacheHelper |
| 37 | + */ |
| 38 | + protected $cacheHelper; |
| 39 | + |
| 40 | + /** |
| 41 | + * Sets if the cache should be enabled or not. |
| 42 | + * |
| 43 | + * @since 1.20 |
| 44 | + * @param boolean $cacheEnabled |
| 45 | + */ |
| 46 | + public function setCacheEnabled( $cacheEnabled ) { |
| 47 | + $this->cacheHelper->setCacheEnabled( $cacheEnabled ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Initializes the caching. |
| 52 | + * Should be called before the first time anything is added via addCachedHTML. |
| 53 | + * |
| 54 | + * @since 1.20 |
| 55 | + * |
| 56 | + * @param integer|null $cacheExpiry Sets the cache expirty, either ttl in seconds or unix timestamp. |
| 57 | + * @param boolean|null $cacheEnabled Sets if the cache should be enabled or not. |
| 58 | + */ |
| 59 | + public function startCache( $cacheExpiry = null, $cacheEnabled = null ) { |
| 60 | + $this->cacheHelper = new CacheHelper( $this->get ); |
| 61 | + |
| 62 | + $this->cacheHelper->setOnInitializedHandler( array( $this, 'onCacheInitialized' ) ); |
| 63 | + |
| 64 | + $keyArgs = $this->getCacheKey(); |
| 65 | + |
| 66 | + if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) { |
| 67 | + unset( $keyArgs['action'] ); |
| 68 | + } |
| 69 | + |
| 70 | + $this->cacheHelper->setCacheKey( $keyArgs ); |
| 71 | + |
| 72 | + if ( $this->getRequest()->getText( 'action' ) === 'purge' ) { |
| 73 | + $this->cacheHelper->purge(); |
| 74 | + } |
| 75 | + |
| 76 | + $this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Get a cached value if available or compute it if not and then cache it if possible. |
| 81 | + * The provided $computeFunction is only called when the computation needs to happen |
| 82 | + * and should return a result value. $args are arguments that will be passed to the |
| 83 | + * compute function when called. |
| 84 | + * |
| 85 | + * @since 1.20 |
| 86 | + * |
| 87 | + * @param {function} $computeFunction |
| 88 | + * @param array|mixed $args |
| 89 | + * @param string|null $key |
| 90 | + * |
| 91 | + * @return mixed |
| 92 | + */ |
| 93 | + public function getCachedValue( $computeFunction, $args = array(), $key = null ) { |
| 94 | + return $this->cacheHelper->getCachedValue( $computeFunction, $args, $key ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Add some HTML to be cached. |
| 99 | + * This is done by providing a callback function that should |
| 100 | + * return the HTML to be added. It will only be called if the |
| 101 | + * item is not in the cache yet or when the cache has been invalidated. |
| 102 | + * |
| 103 | + * @since 1.20 |
| 104 | + * |
| 105 | + * @param {function} $computeFunction |
| 106 | + * @param array $args |
| 107 | + * @param string|null $key |
| 108 | + */ |
| 109 | + public function addCachedHTML( $computeFunction, $args = array(), $key = null ) { |
| 110 | + $this->getOutput()->addHTML( $this->cacheHelper->getCachedValue( $computeFunction, $args, $key ) ); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Saves the HTML to the cache in case it got recomputed. |
| 115 | + * Should be called after the last time anything is added via addCachedHTML. |
| 116 | + * |
| 117 | + * @since 1.20 |
| 118 | + */ |
| 119 | + public function saveCache() { |
| 120 | + $this->cacheHelper->saveCache(); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.. |
| 125 | + * |
| 126 | + * @since 1.20 |
| 127 | + * |
| 128 | + * @param integer $cacheExpiry |
| 129 | + */ |
| 130 | + public function setExpirey( $cacheExpiry ) { |
| 131 | + $this->cacheHelper->setExpirey( $cacheExpiry ); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Returns the variables used to constructed the cache key in an array. |
| 136 | + * |
| 137 | + * @since 1.20 |
| 138 | + * |
| 139 | + * @return array |
| 140 | + */ |
| 141 | + protected function getCacheKey() { |
| 142 | + return array( |
| 143 | + get_class( $this->page ), |
| 144 | + $this->getName(), |
| 145 | + $this->getLanguage()->getCode() |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Gets called after the cache got initialized. |
| 151 | + * |
| 152 | + * @since 1.20 |
| 153 | + * |
| 154 | + * @param boolean $hasCached |
| 155 | + */ |
| 156 | + public function onCacheInitialized( $hasCached ) { |
| 157 | + if ( $hasCached ) { |
| 158 | + $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) ); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | +} |
\ No newline at end of file |