您的当前位置:首页正文

使用redis实现搜索历史

2023-10-31 来源:步旅网
使⽤redis实现搜索历史

要求:

  1.限制搜索历史的个数  2.⼤于三⼗天清除历史纪录  3.最多5条历史纪录 代码:

  第⼀部分:查询接⼝  

@PostMapping(\"/getAiResults\") @SneakyThrows

public AjaxResult uploadFile(@RequestParam(\"file\") MultipartFile file) throws Exception { try {

String encode = Base64.encode(file.getBytes());

ImageRequest imageRequest = new ImageRequest(); imageRequest.setImage(encode);

String result = EasydlImageClassify.easydlImageClassify(imageRequest);

AiResponseEntity jsonObject = JSONUtil.toBean(result, AiResponseEntity.class); HashMap dataMap = new HashMap();

List results = jsonObject.getResults(); results.forEach(item -> {

InsectEntity ins = dataMap.get(item.getName()); if (ins != null) {

dataMap.put(item.getName(), item.getScore() > ins.getScore() ? item : ins); } else {

dataMap.put(item.getName(), item); } });

Set strings = dataMap.keySet();

ArrayList insectEntities = new ArrayList<>(); strings.forEach(item -> {

insectEntities.add(dataMap.get(item)); });

insectEntities.forEach(item -> {

SituationArchivesPests one = situationArchivesPestsService.getOne(Wrappers.lambdaQuery(SituationArchivesPests.class) .eq(SituationArchivesPests::getInsectName, item.getName())); item.setPests(one);

// ==============================核⼼代码

String key = RedisKeyPrefix.农情快讯搜索.getKey() + SecurityUtils.getLoginUser().getUser().getUserId(); redisTemplate.expire(key, 30, TimeUnit.DAYS);// if (one!=null){

redisTemplate.opsForList().leftPush(key, JSONUtil.toJsonStr(item)); Long size = redisTemplate.opsForList().size(key); if (size > 10) {

redisTemplate.opsForList().rightPop(key); }// }

//===============================核⼼代码 });

return AjaxResult.success(insectEntities); } catch (Exception e) {

return AjaxResult.error(e.getMessage()); } }

    第⼀部分:获取历史

@GetMapping(value = \"/serarchHistory\") @ApiOperation(\"识⾍历史记录\") public AjaxResult serarchHistory() {

String key = RedisKeyPrefix.识⾍历史记录.getKey() + SecurityUtils.getLoginUser().getUser().getUserId(); Long size = redisTemplate.opsForList().size(key);

List range = redisTemplate.opsForList().range(key, 0, size - 1); return AjaxResult.success(range);

因篇幅问题不能全部显示,请点此查看更多更全内容