通过Spring操作Redis

前置工作

引入依赖

通过Spring操作Redis_第1张图片

配置端口

spring:
  redis:
    host: 127.0.0.1
    port: 8888

使用StringRedisTemplate

@Autowired
private StringRedisTemplate redisTemplate;

Spring通过StringRedisTemplate操作redis
StringRedisTemplate是RedisTemplate的子类,专门用来处理文本数据
相比于jedis来说,RedisTemplate根据类型分类,做了更进一步的封装,如下图所示:
通过Spring操作Redis_第2张图片

而redis的全局命令并没有进行封装,需要通过execute回调方法执行。

redisTemplate.execute( (RedisConnection connection) -> {
    connection.flushAll();
    return null;
});

通过Spring操作Redis_第3张图片
五种数据类型的操作

@RestController
public class Controller {
    /*
    Spring通过StringRedisTemplate操作redis
    StringRedisTemplate是RedisTemplate的子类,专门用来处理文本数据
    相比于jedis来说,RedisTemplate根据类型分类,做了更进一步的封装
     */
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/testString")
    public String testString() {

        redisTemplate.execute( (RedisConnection connection) -> {
            connection.flushAll();
            //execute要求回调方法中需要写return语句,
            //返回的对象,会作为execute本身的返回值
            return null;
        });
        redisTemplate.opsForValue().set("key", "111");
        redisTemplate.opsForValue().set("key2", "222");
        redisTemplate.opsForValue().set("key3", "333");

        String value = redisTemplate.opsForValue().get("key");
        System.out.println("value:" + value);
        return "ok";
    }

    @GetMapping("/testList")
    public String testList() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });
        redisTemplate.opsForList().leftPush("key", "111");
        redisTemplate.opsForList().leftPushAll("key", "222", "333");

        String value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value:" + value);
        value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value:" + value);
        value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value:" + value);
        return "ok";
    }

    @GetMapping("/testSet")
    public String testSet() {
        redisTemplate.execute((RedisConnection connection)-> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForSet().add("key", "111", "222", "333");
        Set<String> result = redisTemplate.opsForSet().members("key");
        System.out.println("result:" + result);

        Boolean exists = redisTemplate.opsForSet().isMember("key", "222");
        System.out.println("exists:" + exists);

        Long count = redisTemplate.opsForSet().size("key");
        System.out.println("count:" + count);

        redisTemplate.opsForSet().remove("key", "111", "222");
        result = redisTemplate.opsForSet().members("key");
        System.out.println("result:" + result);
        return "ok";
    }

    @GetMapping("/testHash")
    public String testHash() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForHash().put("key", "f1", "111");
        Map<String, String> fields = new HashMap<>();
        fields.put("f2", "222");
        fields.put("f3", "333");
        redisTemplate.opsForHash().putAll("key", fields);

        String value = (String) redisTemplate.opsForHash().get("key", "f1");
        System.out.println("value:" + value);

        Boolean exists = redisTemplate.opsForHash().hasKey("key", "f1");
        System.out.println("exists: " + exists);

        redisTemplate.opsForHash().delete("key", "f1", "f2");

        Long size = redisTemplate.opsForHash().size("key");
        System.out.println("size: " + size);
        return "ok";
    }

    @GetMapping("/testZSet")
    public String testZSet() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
           return null;
        });

        redisTemplate.opsForZSet().add("key", "zhangsan", 10);
        redisTemplate.opsForZSet().add("key", "lisi", 20);
        redisTemplate.opsForZSet().add("key", "wangwu", 30);

        Set<String> members = redisTemplate.opsForZSet().range("key", 0, -1);
        System.out.println("members:" + members);


        Set<ZSetOperations.TypedTuple<String>> membersWithScore =
                redisTemplate.opsForZSet().rangeWithScores("key", 0, -1);
        System.out.println("membersWithScore: " + membersWithScore);

        Double score = redisTemplate.opsForZSet().score("key", "zhangsan");
        System.out.println("score:" + score);

        redisTemplate.opsForZSet().remove("key", "zhangsan");

        Long size = redisTemplate.opsForZSet().size("key");
        System.out.println("size:" + size);

        Long rank = redisTemplate.opsForZSet().rank("key", "lisi");
        System.out.println("rank:" + rank);
        return "ok";
    }
    
}

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