| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | * @ingroup Cache |
| 20 | */ |
| 21 | |
| 22 | namespace MediaWiki\Cache; |
| 23 | |
| 24 | use LogicException; |
| 25 | use MediaWiki\Linker\LinkTarget; |
| 26 | use MediaWiki\Page\PageReference; |
| 27 | |
| 28 | /** |
| 29 | * Helper class for mapping value objects representing basic entities to cache keys. |
| 30 | * |
| 31 | * Rationale: |
| 32 | * The logic for deriving the cache key should not be in the value object themselves for two reasons: |
| 33 | * Firstly, the value object should not contain knowledge about caching or keys in general. |
| 34 | * Secondly, all implementations of a given interface must have the exact same logic for deriving |
| 35 | * the cache key. Otherwise, caches will break when different implementations are used when |
| 36 | * interacting with a cache. |
| 37 | * |
| 38 | * Furthermore, the logic for deriving cache keys should not be in a service instance: there can |
| 39 | * only ever be one implementation, it must not depend on configuration, and it should never change. |
| 40 | * |
| 41 | * @ingroup Cache |
| 42 | */ |
| 43 | abstract class CacheKeyHelper { |
| 44 | |
| 45 | /** |
| 46 | * Private constructor to defy instantiation. |
| 47 | * @return never |
| 48 | */ |
| 49 | private function __construct() { |
| 50 | // we should never even get here... |
| 51 | throw new LogicException( 'Should not instantiate ' . __CLASS__ ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param LinkTarget|PageReference $page |
| 56 | * |
| 57 | * @return string |
| 58 | */ |
| 59 | public static function getKeyForPage( $page ): string { |
| 60 | return 'ns' . $page->getNamespace() . ':' . $page->getDBkey(); |
| 61 | } |
| 62 | } |