/** * Returns the item from the static acceleration array if it exists there. * * @param string $key The parsed key * @return mixed|false The data from the static acceleration array or false if it wasn't there. */ protected function get_from_persist_cache($key) { // This method of checking if an array was supplied is faster than is_array. if ($key === (array) $key) { $key = $key['key']; } // This isset check is faster than array_key_exists but will return false // for null values, meaning null values will come from backing store not // the static acceleration array. We think this okay because null usage should be // very rare (see comment in MDL-39472). if (!$this->staticacceleration || !isset($this->staticaccelerationarray[$key])) { $result = false; } else { $data = $this->staticaccelerationarray[$key]; if (!$this->has_a_ttl() || !$data instanceof cache_ttl_wrapper) { if ($data instanceof cache_cached_object) { $data = $data->restore_object(); } $result = $data; } else { if ($data->has_expired()) { $this->delete_from_persist_cache($key); $result = false; } else { if ($data instanceof cache_cached_object) { $data = $data->restore_object(); } $result = $data->data; } } } if ($result) { if ($this->perfdebug) { cache_helper::record_cache_hit('** static acceleration **', $this->definition->get_id()); } if ($this->staticaccelerationsize > 1 && $this->staticaccelerationcount > 1) { // Check to see if this is the last item on the static acceleration keys array. if (end($this->staticaccelerationkeys) !== $key) { // It isn't the last item. // Move the item to the end of the array so that it is last to be removed. unset($this->staticaccelerationkeys[$key]); $this->staticaccelerationkeys[$key] = $key; } } return $result; } else { if ($this->perfdebug) { cache_helper::record_cache_miss('** static acceleration **', $this->definition->get_id()); } return false; } }
/** * Returns the item from the persist cache if it exists there. * * @param string $key The parsed key * @return mixed|false The data from the persist cache or false if it wasn't there. */ protected function get_from_persist_cache($key) { if (is_array($key)) { $key = $key['key']; } if (!$this->persist || !array_key_exists($key, $this->persistcache)) { $result = false; } else { $data = $this->persistcache[$key]; if (!$this->has_a_ttl() || !$data instanceof cache_ttl_wrapper) { if ($data instanceof cache_cached_object) { $data = $data->restore_object(); } $result = $data; } else { if ($data->has_expired()) { $this->delete_from_persist_cache($key); $result = false; } else { if ($data instanceof cache_cached_object) { $data = $data->restore_object(); } $result = $data->data; } } } if ($result) { if ($this->perfdebug) { cache_helper::record_cache_hit('** static persist **', $this->definition->get_id()); } return $result; } else { if ($this->perfdebug) { cache_helper::record_cache_miss('** static persist **', $this->definition->get_id()); } return false; } }
/** * 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->storeid = $definition->generate_definition_hash(); $this->store =& self::register_store_id($this->name . '-' . $definition->get_id()); $this->ttl = $definition->get_ttl(); $maxsize = $definition->get_maxsize(); if ($maxsize !== null) { // Must be a positive int. $this->maxsize = abs((int) $maxsize); $this->storecount = count($this->store); } $this->check_ttl(); }
/** * Hashes a descriptive key to make it shorter and still unique. * @param string|int $key * @param cache_definition $definition * @return string */ public static function hash_key($key, cache_definition $definition) { if ($definition->uses_simple_keys()) { if (debugging() && preg_match('#[^a-zA-Z0-9_]#', $key)) { throw new coding_exception('Cache definition ' . $definition->get_id() . ' requires simple keys. Invalid key provided.', $key); } // We put the key first so that we can be sure the start of the key changes. return (string) $key . '-' . $definition->generate_single_key_prefix(); } $key = $definition->generate_single_key_prefix() . '-' . $key; return sha1($key); }
/** * Returns an array of cache stores that have been initialised for use in definitions. * @param cache_definition $definition * @return array */ public function get_store_instances_in_use(cache_definition $definition) { $id = $definition->get_id(); if (!isset($this->definitionstores[$id])) { return array(); } return $this->definitionstores[$id]; }
/** * 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; }
/** * 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 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->storeid = $definition->generate_definition_hash(); $this->store =& self::register_store_id($definition->get_id()); $this->ttl = $definition->get_ttl(); }