383
383
* Record a cache hit in the stats for the given store and definition.
385
386
* @param string $store
386
387
* @param string $definition
388
* @param int $hits The number of hits to record (by default 1)
388
public static function record_cache_hit($store, $definition) {
390
public static function record_cache_hit($store, $definition, $hits = 1) {
389
391
self::ensure_ready_for_stats($store, $definition);
390
self::$stats[$definition][$store]['hits']++;
392
self::$stats[$definition][$store]['hits'] += $hits;
394
396
* Record a cache miss in the stats for the given store and definition.
396
399
* @param string $store
397
400
* @param string $definition
401
* @param int $misses The number of misses to record (by default 1)
399
public static function record_cache_miss($store, $definition) {
403
public static function record_cache_miss($store, $definition, $misses = 1) {
400
404
self::ensure_ready_for_stats($store, $definition);
401
self::$stats[$definition][$store]['misses']++;
405
self::$stats[$definition][$store]['misses'] += $misses;
405
409
* Record a cache set in the stats for the given store and definition.
407
412
* @param string $store
408
413
* @param string $definition
414
* @param int $sets The number of sets to record (by default 1)
410
public static function record_cache_set($store, $definition) {
416
public static function record_cache_set($store, $definition, $sets = 1) {
411
417
self::ensure_ready_for_stats($store, $definition);
412
self::$stats[$definition][$store]['sets']++;
418
self::$stats[$definition][$store]['sets'] += $sets;
462
468
$class = $store['class'];
464
470
// Found the store: is it ready?
471
/* @var cache_store $instance */
465
472
$instance = new $class($store['name'], $store['configuration']);
466
if (!$instance->is_ready()) {
473
if (!$instance::are_requirements_met() || !$instance->is_ready()) {
467
474
unset($instance);
684
* Returns an array of stores that would meet the requirements for every definition.
686
* These stores would be 100% suitable to map as defaults for cache modes.
688
* @return array[] An array of stores, keys are the store names.
690
public static function get_stores_suitable_for_mode_default() {
691
$factory = cache_factory::instance();
692
$config = $factory->create_config_instance();
694
foreach ($config->get_definitions() as $definition) {
695
$definition = cache_definition::load($definition['component'].'/'.$definition['area'], $definition);
696
$requirements = $requirements | $definition->get_requirements_bin();
699
foreach ($config->get_all_stores() as $name => $store) {
700
if (!empty($store['features']) && ($store['features'] & $requirements)) {
701
$stores[$name] = $store;
708
* Returns stores suitable for use with a given definition.
710
* @param cache_definition $definition
711
* @return cache_store[]
713
public static function get_stores_suitable_for_definition(cache_definition $definition) {
714
$factory = cache_factory::instance();
716
if ($factory->is_initialising() || $factory->stores_disabled()) {
717
// No suitable stores here.
720
$stores = self::get_cache_stores($definition);
721
if (count($stores) === 0) {
722
// No suitable stores we found for the definition. We need to come up with a sensible default.
723
// If this has happened we can be sure that the user has mapped custom stores to either the
724
// mode of the definition. The first alternative to try is the system default for the mode.
725
// e.g. the default file store instance for application definitions.
726
$config = $factory->create_config_instance();
727
foreach ($config->get_stores($definition->get_mode()) as $name => $details) {
728
if (!empty($details['default'])) {
729
$stores[] = $factory->create_store_from_config($name, $details, $definition);