/**
  * 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);
 }
Exemplo n.º 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)
 {
     $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);
 }
Exemplo n.º 3
0
 /**
  * Sets an item in the cache given its key and data value.
  *
  * @param string $key The key to use.
  * @param mixed $data The data to set.
  * @return bool True if the operation was a success false otherwise.
  */
 public function set($key, $data)
 {
     if ($this->encode) {
         // We must serialise this data.
         $data = serialize($data);
     }
     return $this->connection->set($this->parse_key($key), $data, MEMCACHE_COMPRESSED, $this->definition->get_ttl());
 }
Exemplo n.º 4
0
 /**
  * Sets many items in the cache in a single transaction.
  *
  * @param array $keyvaluearray An array of key value pairs. Each item in the array will be an associative array with two
  *      keys, 'key' and 'value'.
  * @return int The number of items successfully set. It is up to the developer to check this matches the number of items
  *      sent ... if they care that is.
  */
 public function set_many(array $keyvaluearray)
 {
     $store = array();
     foreach ($keyvaluearray as $pair) {
         $store[$this->prepare_key($pair['key'])] = $pair['value'];
     }
     $result = apcu_store($store, null, $this->definition->get_ttl());
     return count($keyvaluearray) - count($result);
 }
Exemplo n.º 5
0
 /**
  * Sets many items in the cache in a single transaction.
  *
  * @param array $keyvaluearray An array of key value pairs. Each item in the array will be an associative array with two
  *      keys, 'key' and 'value'.
  * @return int The number of items successfully set. It is up to the developer to check this matches the number of items
  *      sent ... if they care that is.
  */
 public function set_many(array $keyvaluearray)
 {
     $count = 0;
     foreach ($keyvaluearray as $pair) {
         if ($this->connection->set($this->parse_key($pair['key']), $pair['value'], MEMCACHE_COMPRESSED, $this->definition->get_ttl())) {
             $count++;
         }
     }
     return $count;
 }
Exemplo n.º 6
0
 /**
  * Sets many items in the cache in a single transaction.
  *
  * @param array $keyvaluearray An array of key value pairs. Each item in the array will be an associative array with two
  *      keys, 'key' and 'value'.
  * @return int The number of items successfully set. It is up to the developer to check this matches the number of items
  *      sent ... if they care that is.
  */
 public function set_many(array $keyvaluearray)
 {
     $pairs = array();
     foreach ($keyvaluearray as $pair) {
         $pairs[$pair['key']] = $pair['value'];
     }
     if ($this->connection->setMulti($pairs, $this->definition->get_ttl())) {
         return count($keyvaluearray);
     }
     return 0;
 }
Exemplo n.º 7
0
 /**
  * Sets an item in the cache given its key and data value.
  *
  * @param string $key The key to use.
  * @param mixed $data The data to set.
  * @return bool True if the operation was a success false otherwise.
  */
 public function set($key, $data)
 {
     if ($this->clustered) {
         $status = true;
         foreach ($this->setconnections as $connection) {
             $status = $connection->set($this->parse_key($key), $data, MEMCACHE_COMPRESSED, $this->definition->get_ttl()) && $status;
         }
         return $status;
     }
     return $this->connection->set($this->parse_key($key), $data, MEMCACHE_COMPRESSED, $this->definition->get_ttl());
 }
Exemplo n.º 8
0
 /**
  * Sets an item in the cache given its key and data value.
  *
  * @param string $key The key to use.
  * @param mixed $data The data to set.
  * @return bool True if the operation was a success false otherwise.
  */
 public function set($key, $data)
 {
     if ($this->encode) {
         // We must serialise this data.
         $data = serialize($data);
     }
     if ($this->clustered) {
         $status = true;
         foreach ($this->setconnections as $connection) {
             $status = $connection->set($this->parse_key($key), $data, MEMCACHE_COMPRESSED, $this->definition->get_ttl()) && $status;
         }
         return $status;
     }
     return $this->connection->set($this->parse_key($key), $data, MEMCACHE_COMPRESSED, $this->definition->get_ttl());
 }
Exemplo n.º 9
0
 /**
  * Sets many items in the cache in a single transaction.
  *
  * @param array $keyvaluearray An array of key value pairs. Each item in the array will be an associative array with two
  *      keys, 'key' and 'value'.
  * @return int The number of items successfully set. It is up to the developer to check this matches the number of items
  *      sent ... if they care that is.
  */
 public function set_many(array $keyvaluearray)
 {
     $pairs = array();
     foreach ($keyvaluearray as $pair) {
         $pairs[$pair['key']] = $pair['value'];
     }
     $status = true;
     if ($this->clustered) {
         foreach ($this->setconnections as $connection) {
             $status = $connection->setMulti($pairs, $this->definition->get_ttl()) && $status;
         }
     } else {
         $status = $this->connection->setMulti($pairs, $this->definition->get_ttl());
     }
     if ($status) {
         return count($keyvaluearray);
     }
     return 0;
 }
Exemplo n.º 10
0
 /**
  * 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();
 }
Exemplo n.º 11
0
 /**
  * Checks if the store has a record for the given key and returns true if so.
  *
  * @param string $key
  * @return bool
  */
 public function has($key)
 {
     $filename = $key . '.cache';
     $maxtime = cache::now() - $this->definition->get_ttl();
     if ($this->prescan) {
         return array_key_exists($filename, $this->keys) && $this->keys[$filename] >= $maxtime;
     }
     $file = $this->file_path_for_key($key);
     return file_exists($file) && ($this->definition->get_ttl() == 0 || filemtime($file) >= $maxtime);
 }
Exemplo n.º 12
0
 /**
  * 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->storeid);
     $this->ttl = $definition->get_ttl();
 }
Exemplo n.º 13
0
 /**
  * Sets an item in the cache given its key and data value.
  *
  * @param string $key The key to use.
  * @param mixed $data The data to set.
  * @return bool True if the operation was a success false otherwise.
  */
 public function set($key, $data)
 {
     return xcache_set($this->prefix . $key, $data, $this->definition->get_ttl());
 }