Example #1
0
 /**
  * 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)
 {
     $this->check_tracked_user();
     $session = $this->get_session_data();
     $simulatettl = $this->has_a_ttl();
     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->get_definition()->get_ttl());
         }
         $parsedkey = $this->parse_key($key);
         $session[$parsedkey] = $value;
     }
     if ($this->perfdebug) {
         cache_helper::record_cache_set($this->storetype, $this->get_definition()->get_id());
     }
     $this->save_session($session);
     return count($keyvaluearray);
 }
Example #2
0
 /**
  * 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)
 {
     $this->check_tracked_user();
     $loader = $this->get_loader();
     if ($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.
         $loader->set_many($keyvaluearray);
     }
     $data = array();
     $definitionid = $this->get_definition()->get_ttl();
     $simulatettl = $this->has_a_ttl() && !$this->store_supports_native_ttl();
     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, $definitionid);
         }
         $data[$key] = array('key' => $this->parse_key($key), 'value' => $value);
     }
     $successfullyset = $this->get_store()->set_many($data);
     if ($this->perfdebug && $successfullyset) {
         cache_helper::record_cache_set($this->storetype, $definitionid, $successfullyset);
     }
     return $successfullyset;
 }
Example #3
0
 /**
  * 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)
 {
     $data = array();
     $simulatettl = $this->has_a_ttl() && !$this->store_supports_native_ttl();
     $usepersistcache = $this->is_using_persist_cache();
     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 ($usepersistcache) {
             $this->set_in_persist_cache($data[$key]['key'], $value);
         }
     }
     if ($this->perfdebug) {
         cache_helper::record_cache_set($this->storetype, $this->definition->get_id());
     }
     return $this->store->set_many($data);
 }