测试中要经常修改redis数据,满足测试场景。以前的做法是把相应环境的配置写到测试中,然后对相应的redis进行增删改查的操作。
有没有一种办法,写上redis的值和操作类型,就能通过接口进行调用呢。
通过接口调用,拼接成字符串,写上相应的信息,通过链接可以直接调用。
示例(给15038887008添加值为88的redis):
https://IP:port/XXX/redis/operation
{
"key":"15038887008",
"value":"88",
"operType":"add",
"resmsg":""
}
代码如下:
import java.util.Map;
@Controller
@RequestMapping(value = "/redis")
@Api(tags = "Redis的操作")
@Slf4j
public class RedisController extends BaseController {
@Autowired
JedisUtil jedisUtil;
@RequestMapping(value = "/operation" , method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value="redis", notes="redis的操作")
public Map updateUser(@RequestBody ReqRedisVO reqRedisVO){
try {
String opertype = reqRedisVO.getOperType();
String key = reqRedisVO.getKey();
String value = reqRedisVO.getValue();
String resMsg = "";
if(!StringUtil.isEmpty(key)){
if(key.indexOf(",")!=-1){
String[] splitKey = key.split(",");
if("add".equals(opertype)){
String resaddall = "";
for (int i = 0; i < splitKey.length; i++) {
String resadd = jedisUtil.set(splitKey[i],value);
resaddall = resaddall + resadd;
}
if(resaddall.indexOf("0")!=-1){
resMsg = "更新过程中有部分出错,请检查。";//ok成功
}
resMsg = "redis已经更新成功,可以进行查询操作进行验证。";//ok成功
}else if("query".equals(opertype)){
resMsg = "暂不支持批量查询。";
}else if("del".equals(opertype)){
long num = jedisUtil.del(splitKey);
if(splitKey.length==num){
resMsg = "全部删除成功。";
}else{
resMsg = "遗漏"+(splitKey.length-num)+"条,请求删除共"+splitKey.length+"条,删除成功"+num+"条。";
}
}else{
resMsg = "操作类型选择错误,请检查。";
}
}else{
if("add".equals(opertype)){
jedisUtil.set(reqRedisVO.getKey(),reqRedisVO.getValue());
resMsg = "redis已经更新成功,可以进行查询操作进行验证。";//ok成功
}else if("query".equals(opertype)){
value = jedisUtil.get(reqRedisVO.getKey());
resMsg = "查询成功";
}else if("del".equals(opertype)){
jedisUtil.del(reqRedisVO.getKey());
resMsg = "删除成功";
}else{
resMsg = "操作类型选择错误,请检查。";
}
}
}
reqRedisVO.setValue(value);
reqRedisVO.setResmsg(resMsg);
JSONObject fromObject = JSONObject.fromObject(reqRedisVO);
String string = fromObject.toString();
return data(string);
}catch (Exception e){
log.error("" , e);
return fail();
}
}
}
网友评论