/** * Set a value based on an id. Optionally add tags * * @param string $id ID of cache entry * @param mixed $data Data to set to cache * @param integer $lifetime Lifetime in seconds [Optional] * @param array $tags Tags [Optional] * * @return array Returns an array containing the status of the insertion if the "w" option is set * @return boolean Otherwise, returns TRUE if the inserted array is not empty * * @throws Cache_Exception * * @uses Mango_Collection::safeUpdate */ public function set_with_tags($id, $data, $lifetime = NULL, array $tags = NULL) { // Prepare ID $id = System::sanitize_id($this->config('prefix') . $id); // Prepare data $data = serialize($data); // Setup lifetime if ($lifetime === NULL) { $lifetime = 0 === Arr::get($this->_config, 'default_expire', NULL) ? 0 : Arr::get($this->_config, 'default_expire', Cache::DEFAULT_EXPIRE) + time(); } else { $lifetime = 0 === $lifetime ? 0 : $lifetime + time(); } $data = array('id' => $id, 'data' => $data, 'lifetime' => $lifetime, 'tags' => $tags); try { // try to update/create document, throw exception on errors $status = $this->collection->safeUpdate(array('id' => $id), $data, array('upsert' => TRUE)); } catch (MongoException $e) { throw new Cache_Exception('An error occurred saving cache data: :err', array(':err' => $e->getMessage())); } return (bool) $status; }