/** * Sends several key => value pairs to the cache. * * Using this function comes with potential performance implications. * Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call * the equivalent singular method for each item provided. * This should not deter you from using this function as there is a performance benefit in situations where the cache store * does support it, but you should be aware of this fact. * * <code> * // This code will add four entries to the cache, one for each url. * $cache->set_many(array( * 'main' => 'http://moodle.org', * 'docs' => 'http://docs.moodle.org', * 'tracker' => 'http://tracker.moodle.org', * 'qa' => ''http://qa.moodle.net' * )); * </code> * * @param array $keyvaluearray An array of key => value pairs to send to the cache. * @return int The number of items successfully set. It is up to the developer to check this matches the number of items. * ... if they care that is. */ public function set_many(array $keyvaluearray) { if ($this->loader !== false) { // We have a loader available set it there as well. // We have to let the loader do its own parsing of data as it may be unique. $this->loader->set_many($keyvaluearray); } $data = array(); $simulatettl = $this->has_a_ttl() && !$this->store_supports_native_ttl(); $usestaticaccelerationarray = $this->use_static_acceleration(); foreach ($keyvaluearray as $key => $value) { if (is_object($value) && $value instanceof cacheable_object) { $value = new cache_cached_object($value); } else { if (!is_scalar($value)) { // If data is an object it will be a reference. // If data is an array if may contain references. // We want to break references so that the cache cannot be modified outside of itself. // Call the function to unreference it (in the best way possible). $value = $this->unref($value); } } if ($simulatettl) { $value = new cache_ttl_wrapper($value, $this->definition->get_ttl()); } $data[$key] = array('key' => $this->parse_key($key), 'value' => $value); if ($usestaticaccelerationarray) { $this->static_acceleration_set($data[$key]['key'], $value); } } if ($this->perfdebug) { cache_helper::record_cache_set($this->storetype, $this->definition->get_id()); } return $this->store->set_many($data); }