/** * Initialises the store instance for a definition. * @param cache_definition $definition */ public function initialise(cache_definition $definition) { // If the definition isn't using static acceleration then we need to be store data here. // The reasoning behind this is that: // - If the definition is using static acceleration then the cache loader is going to // store things in its static array. // - If the definition is not using static acceleration then the cache loader won't try to store anything // and we will need to store it here in order to make sure it is accessible. if ($definition->get_mode() !== self::MODE_APPLICATION) { // Neither the request cache nor the session cache provide static acceleration. $this->persist = true; } else { $this->persist = !$definition->use_static_acceleration(); } }
/** * Creates a store instance given its name and configuration. * * If the store has already been instantiated then the original objetc will be returned. (reused) * * @param string $name The name of the store (must be unique remember) * @param array $details * @param cache_definition $definition The definition to instantiate it for. * @return boolean|cache_store */ public function create_store_from_config($name, array $details, cache_definition $definition) { if (!array_key_exists($name, $this->stores)) { // Properties: name, plugin, configuration, class. $class = $details['class']; $store = new $class($details['name'], $details['configuration']); $this->stores[$name] = $store; } $store = $this->stores[$name]; if (!$store->is_ready() || !$store->is_supported_mode($definition->get_mode())) { return false; } // We always create a clone of the original store. // If we were to clone a store that had already been initialised with a definition then // we'd run into a myriad of issues. // We use a method of the store to create a clone rather than just creating it ourselves // so that if any store out there doesn't handle cloning they can override this method in // order to address the issues. $store = $this->stores[$name]->create_clone($details); $store->initialise($definition); $definitionid = $definition->get_id(); if (!isset($this->definitionstores[$definitionid])) { $this->definitionstores[$definitionid] = array(); } $this->definitionstores[$definitionid][] = $store; return $store; }
/** * Gets all of the stores that are to be used for the given definition. * * @param cache_definition $definition * @return array */ public function get_stores_for_definition(cache_definition $definition) { // Check if MUC has been disabled. $factory = cache_factory::instance(); if ($factory->stores_disabled()) { // Yip its been disabled. // To facilitate this we are going to always return an empty array of stores to use. // This will force all cache instances to use the cachestore_dummy. // MUC will still be used essentially so that code using it will still continue to function but because no cache stores // are being used interaction with MUC will be purely based around a static var. return array(); } $availablestores = $this->get_stores($definition->get_mode(), $definition->get_requirements_bin()); $stores = array(); $id = $definition->get_id(); // Now get any mappings and give them priority. foreach ($this->configdefinitionmappings as $mapping) { if ($mapping['definition'] !== $id) { continue; } $storename = $mapping['store']; if (!array_key_exists($storename, $availablestores)) { continue; } if (array_key_exists($storename, $stores)) { $store = $stores[$storename]; unset($stores[$storename]); $stores[$storename] = $store; } else { $stores[$storename] = $availablestores[$storename]; } } if (empty($stores) && !$definition->is_for_mappings_only()) { $mode = $definition->get_mode(); // Load the default stores. foreach ($this->configmodemappings as $mapping) { if ($mapping['mode'] === $mode && array_key_exists($mapping['store'], $availablestores)) { $store = $availablestores[$mapping['store']]; if (empty($store['mappingsonly'])) { $stores[$mapping['store']] = $store; } } } } return $stores; }
/** * Returns a string to describe the definition. * * This method supports the definition as a string due to legacy requirements. * It is backwards compatible when a string is passed but is not accurate. * * @since 2.9 * @param cache_definition|string $definition * @return string */ protected static function get_definition_stat_id_and_mode($definition) { if (!$definition instanceof cache_definition) { // All core calls to this method have been updated, this is the legacy state. // We'll use application as the default as that is the most common, really this is not accurate of course but // at this point we can only guess and as it only affects calls to cache stat outside of core (of which there should // be none) I think that is fine. debugging('Please update you cache stat calls to pass the definition rather than just its ID.', DEBUG_DEVELOPER); return array((string) $definition, cache_store::MODE_APPLICATION); } return array($definition->get_id(), $definition->get_mode()); }
/** * Creates a store instance given its name and configuration. * * If the store has already been instantiated then the original objetc will be returned. (reused) * * @param string $name The name of the store (must be unique remember) * @param array $details * @param cache_definition $definition The definition to instantiate it for. * @return boolean|cache_store */ public function create_store_from_config($name, array $details, cache_definition $definition) { if (!array_key_exists($name, $this->stores)) { // Properties: name, plugin, configuration, class. $class = $details['class']; $store = new $class($details['name'], $details['configuration']); $this->stores[$name] = $store; } $store = $this->stores[$name]; if (!$store->is_ready() || !$store->is_supported_mode($definition->get_mode())) { return false; } $store = clone $this->stores[$name]; $store->initialise($definition); return $store; }
/** * Initialises the cache. * * Once this has been done the cache is all set to be used. * * @param cache_definition $definition */ public function initialise(cache_definition $definition) { $this->definition = $definition; $hash = preg_replace('#[^a-zA-Z0-9]+#', '_', $this->definition->get_id()); $this->path = $this->filestorepath . '/' . $hash; make_writable_directory($this->path); if ($this->prescan && $definition->get_mode() !== self::MODE_REQUEST) { $this->prescan = false; } if ($this->prescan) { $this->prescan_keys(); } }
/** * Initialises a new instance of the cache store given the definition the instance is to be used for. * * This function should prepare any given connections etc. * * @param cache_definition $definition * @return bool */ public function initialise(cache_definition $definition) { $this->definition = $definition; $definition_id = $definition->get_mode() . '/' . $definition->get_component() . '/' . $definition->get_area(); $this->prefix = $this->name . '/data/' . $definition_id . '/'; $this->prefix_lock = $this->name . '/lock/' . $definition_id . '/'; $this->prefix_length = strlen($this->prefix); return true; }