/** * @param RedisKey $key * @return boolean */ public function deleteKeyValues(RedisKey $key) { $result = false; switch ($key->getType()) { case static::$KEY_TYPES[\Redis::REDIS_HASH]: foreach ($key->getValue('delete') as $keyValue) { $result = $this->db->hdel($key->getName(), $keyValue); } break; case static::$KEY_TYPES[\Redis::REDIS_LIST]: foreach ($key->getValue('delete') as $keyIndex) { // workaround to delete list item by index: // http://redis.io/commands/lrem#comment-1375293995 $deleteValue = uniqid('phpredexpert-delete-', true); $result = $this->db->multi()->lset($key->getName(), $keyIndex, $deleteValue)->lrem($key->getName(), $deleteValue)->exec(); } // save result of last operation in a transaction $result = end($result); break; case static::$KEY_TYPES[\Redis::REDIS_SET]: foreach ($key->getValue('delete') as $keyValue) { $result = $this->db->srem($key->getName(), $keyValue); } break; case static::$KEY_TYPES[\Redis::REDIS_ZSET]: foreach ($key->getValue('delete') as $keyValue) { $result = $this->db->zrem($key->getName(), $keyValue); } break; } return $result; }
/** * Delete key value(s). * * @Post("/server/{serverId}/db/{dbId}/keys/values/delete", * requirements = {"serverId": "\d+", "dbId": "\d+"} * ) * @RequestParam(name="name", requirements=".+") * @RequestParam(name="type", requirements=".+") * @RequestParam(name="value", array=true) * @ParamConverter("key", converter="fos_rest.request_body", class="Eugef\PhpRedExpert\ApiBundle\Model\RedisKey") * @View() * * @param integer $serverId * @param integer $dbId * @param RedisKey $key * @throws HttpException * @return array */ public function deleteKeyValuesAction($serverId, $dbId, RedisKey $key) { $this->initialize($serverId, $dbId); if (!$key->validName()) { throw new HttpException(400, 'Key name is not specified'); } if (!$key->validType()) { throw new HttpException(400, 'Key type is invalid'); } if (!$key->hasValue('delete')) { throw new HttpException(400, 'Key values to be deleted are not specified'); } $result = $this->redis->deleteKeyValues($key); if ($result === false) { throw new HttpException(404, 'Key values are not deleted'); } return array('key' => $this->redis->getKey($key->getName())); }