Merge "docs: Fix typo"
[mediawiki.git] / includes / cache / CacheKeyHelper.php
blob34a43f6ef1b8aeb95a4584a161f9243967cca4e9
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.
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.
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
18 * @file
19 * @ingroup Cache
22 namespace MediaWiki\Cache;
24 use LogicException;
25 use MediaWiki\Linker\LinkTarget;
26 use MediaWiki\Page\PageReference;
28 /**
29 * Helper class for mapping value objects representing basic entities to cache keys.
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.
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.
41 * @ingroup Cache
43 abstract class CacheKeyHelper {
45 /**
46 * Private constructor to defy instantiation.
47 * @return never
49 private function __construct() {
50 // we should never even get here...
51 throw new LogicException( 'Should not instantiate ' . __CLASS__ );
54 /**
55 * @param LinkTarget|PageReference $page
57 * @return string
59 public static function getKeyForPage( $page ): string {
60 return 'ns' . $page->getNamespace() . ':' . $page->getDBkey();