/** * Save some string datas into a cache record * * Note : $data is always "string" (serialization is done by the * core not by the backend) * * @param string $data Datas to cache * @param string $id Cache id * @param array $tags Array of strings, the cache record will be tagged by each string entry * @param bool|int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime) * @throws Exception * @return boolean True if no problem */ public function save($data, $id, $tags = array(), $specificLifetime = false) { if (!is_array($tags)) { $tags = $tags ? array($tags) : array(); } $lifetime = $this->getLifetime($specificLifetime); // Get list of tags previously assigned $oldTags = $this->_decodeData($this->_redis->hGet(self::PREFIX_KEY . $id, self::FIELD_TAGS)); $oldTags = $oldTags ? explode(',', $oldTags) : array(); //$this->_redis->pipeline()->multi(); // Set the data $result = $this->_redis->hMSet(self::PREFIX_KEY . $id, array(self::FIELD_DATA => $this->_encodeData($data, $this->_compressData), self::FIELD_TAGS => $this->_encodeData(implode(',', $tags), $this->_compressTags), self::FIELD_MTIME => time(), self::FIELD_INF => $lifetime ? 0 : 1)); if (!$result) { throw new Exception("Could not set cache key {$id}"); } // Set expiration if specified if ($lifetime) { $this->_redis->expire(self::PREFIX_KEY . $id, min($lifetime, self::MAX_LIFETIME)); } // Process added tags if ($tags) { // Update the list with all the tags $this->_redis->sAdd(self::SET_TAGS, $tags); // Update the id list for each tag foreach ($tags as $tag) { $this->_redis->sAdd(self::PREFIX_TAG_IDS . $tag, $id); } } // Process removed tags if ($remTags = $oldTags ? array_diff($oldTags, $tags) : FALSE) { // Update the id list for each tag foreach ($remTags as $tag) { $this->_redis->sRem(self::PREFIX_TAG_IDS . $tag, $id); } } // Update the list with all the ids if ($this->_notMatchingTags) { $this->_redis->sAdd(self::SET_IDS, $id); } //$this->_redis->exec(); return TRUE; }