We have to many different cache objects, configuration variables, methods and factories for various types of cache interfaces.
A few of the things we use right now:
* `wfGetCache( "hash" )`, `new HashBagOStuff();`.
* `wfGetCache( CACHE_ANYTHING )`, `ObjectCache::newAnything()`.
* `ObjectCache::newAccelerator( fallback )`.
* `wfGetMainCache()`, `ObjectCache::getMainClusterInstance()`.
* `ObjectCache::getMainWANInstance()`.
* `ObjectCache::getMainStashInstance()`.
* `ObjectCache::getInstance()`.
* `ObjectCache::getWANInstance()`.
* more..
* variations on the above due to back-compat aliases.
We should reduce this to a handful only that we use everywhere.
Some configuration:
* `$wgObjectCaches array( .. )`
* `$wgWANObjectCaches array( .. )`
* `$wgMainCacheType`
* `$wgMainWANCache`
* `$wgMainStash`
* `$wgMessageCacheType`, `$wgParserCacheType`, `$wgSessionCacheType`, `$wgLanguageConverterCacheType` (maybe introduce some kind of cache grouping to allow overriding backends for individual cache groups, defaulting to main cache).
With the current multi-datacenter (#Availability) we're also changing which cache developers should use by default. Namely one should now default to WANCache instead of the local-dc cache. It may make sense to also update the notion of "main cache" to start referring to WAN cache to clarify this convention shift.
I propose we standardise on the following four main entry points:
* Server cache or Process cache (e.g. APC, fallback to none, or hash.)
* Local cluster cache (e.g. Memcached).
* WAN cache (e.g. Memcached, with relayed purges and other improvements).
* Main stash (e.g. db-replicated).
With two extra entry points for special cases:
* Identified cache group (e.g. `ObjectCache::getGroup( "language-converter" )`).
* Identified cache backend (should be unused, but for back-compat, e.g. `ObjectCache::getInstance( CACHE_DB );`_
Reduced interface:
```lang=php
class ObjectCache {
// Takes key from $wgObjectCaches
static function getInstance( string $type );
// Takes optional key from $wgObjectCacheGroups, defaults to main cache
static function getGroup( string $groupName );
// Get cache object for in-memory values on the current server (APC, fallback to in-process hash).
static function newAccelerator( $fallback = "hash" );
// Get cache object for basic caching in the local DC (aka wgMemc, or "main cache").
static function getMainClusterInstance();
// Get cache object for the main cache in the local DC that is multi-DC aware (relayed purges, and other improvements).
static function getMainWANInstance();
// Replicated store
static function getMainStashInstance();
}
```
Refactored interface (stretch goal, methods renamed from the above):
```lang=php
class ObjectCache {
static function getInstance( string $type );
static function getGroup( string $groupName );
static function newAccelerator();
static function getLocalClusterCache();
static function getMainCache();
static function getMainStash();
}```
Proposed actionables:
* Don't require custom accelerator fallback. Set a default one (empty or hash) and deprecate the fallback parameter.
* Deprecate the concept of CACHE_ANYTHING. Convert uses to main cache. Consider changing the default main cache in stock MediaWiki from CACHE_NONE to CACHE_DB.
* (Later) Remove `ObjectCache::newAnything()`.
* Change WANObjectCache to inherit from BagOStuff (or extract its interface and make it implement that).
* Remove `ObjectCache::getWANInstance` and `$wgWANObjectCaches`.
* Deprecate `CACHE_` constants so that we only use string keys with wfGetCache and `$wgObjectCaches`. The only cache constant keys that refer to factories (Anything, Accelerator, Memcached) and a special one (empty).
* CACHE_NONE can be "none" instead.
* CACHE_ANYTHING is deprecated above.
* CACHE_ACCEL should be used via `ObjectCache::newAccelerator` instead (previously due to fallback requirement, now for interface consistency)
* CACHE_MEMCACHED is discouraged as it uses a deterministic factory (based on discovery). It's already unused by default, and both the installer and documentation encourage setting a specific implementation. We can keep it for back-compat or rename to "memcache" if we find valid use cases.
* Deprecate cache group config vars (`$wgMessageCacheType`, `$wgParserCacheType` etc.) in favour of e.g. `$wgObjectCacheGroups`.
Thoughts for later:
* Once we have `$wgObjectCacheGroups`, it'll be less expensive to introduce new cache groups. Making it easy to move around things in wmf-production and re-use caches without adding cruft to MediaWiki.