1 springboot操作redis list集合

操作redis集合

  • push 添加操作
@RestController
public class ListController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @ApiOperation(value = "push操作")
    @GetMapping("/push")
    public HealthResult push() {
        redisTemplate.opsForList().rightPush("list4", "one");
        redisTemplate.opsForList().rightPush("list4", "two");
        redisTemplate.opsForList().rightPush("list4", "three");
        redisTemplate.opsForList().rightPush("list4", "four");
        List list4 = redisTemplate.opsForList().range("list4", 0, -1);
        return HealthResult.ok(list4);
    }
}
  • set 替换操作 redisTemplate.opsForList().set(key, index, newValue);
@Api(value = "ListController", description = "springboot操作redis,list集合")
@RestController
public class ListController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @ApiOperation(value = "set操作")
    @GetMapping("/set")
    public HealthResult set() {
        redisTemplate.opsForList().set("list4", 2, "newTwo");
        List list4 = redisTemplate.opsForList().range("list4", 0, -1);
        return HealthResult.ok(list4);
    }
}
  • remove 删除操作 redisTemplate.opsForList().remove(“list4”, 0, “two”);
    (主键,删除个数 0表示删除所有,需要删除的对象),返回删除对象的格式
@Api(value = "ListController", description = "springboot操作redis,list集合")
@RestController
public class ListController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @ApiOperation(value = "删除操作(remove)")
    @GetMapping("/remove")
    public HealthResult remove() {
        Long removeCount = redisTemplate.opsForList().remove("list4", 0, "two");
        return HealthResult.ok(removeCount);
    }
}
  • trim 截取操作 redisTemplate.opsForList().trim(“list4”, 0, 2); (主键,开始位置坐标,结束位置坐标)
@Api(value = "ListController", description = "springboot操作redis,list集合")
@RestController
public class ListController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @ApiOperation(value = "截取操作(trim)")
    @GetMapping("/trim")
    public HealthResult trim() {
        redisTemplate.opsForList().trim("list4", 0, 2);
        List list4 = redisTemplate.opsForList().range("list", 0, -1);
        return HealthResult.ok(list4);
    }
}
  • rightPopAndLeftPush 从尾部删除,从头部插入,并返回这个值(循环遍历)
@Api(value = "ListController", description = "springboot操作redis,list集合")
@RestController
public class ListController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @ApiOperation(value = "循环遍历(rightPopAndLeftPush)")
    @GetMapping("/rightPopAndLeftPush")
    public HealthResult rightPopAndLeftPush() {
        String result = redisTemplate.opsForList().rightPopAndLeftPush("list4", "list4");
        return HealthResult.ok(result);
    }
}

你可能感兴趣的:(springboot,redis)