SpringBoot 操作Redis

Redis的Java客户端

Redis的Java客户端常用的有:

  • Jedis
  • Lettuce
  • Spring Data Redis

Spring Data Redis是Spring的一部分,对Redis底层开发包进行了高度封装。在Spring项目中,可以使用Spring Data Redis来简化操作。

Spring Data Redis使用方式

操作步骤:

  1. 导入Spring Data Redis的Maven
  2. 配置Redis数据源
  3. 编写配置类,创建RedisTemplate对象
  4. 通过RedisTemplate对象操作Redis
<dependency>
  <groupId>org.springframework.bootgroupId>
  <artifactId>spring-boot-starter-data-redisartifactId>
dependency>

配置Redis数据源详情
4.SpringBoot整合Redis

spring:
	redis:
  	host:
    port:
    password:
@Configuration
@Slf4j
public class RedisConfiguration{
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
        log.info("开始创建Redis模板类...");
        RedisTemplate redisTemplate=new RedisTemplate();
        //设置key的序列化器,默认为JdkSerializationRedisSerializer
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }
}
	/**
     * 操作字符串类型的数据
     */
    @Test
    public void testString(){
        // set get setex setnx
        
        //set
        redisTemplate.opsForValue().set("city","北京");
        //get
        String city = (String)redisTemplate.opsForValue().get("city");
        System.out.println(city);
        //setex
        // set(key,value,timeout,TimeUnit) key:键,value:值,timeout:时间,TimeUnit:时间单位(枚举型)
        redisTemplate.opsForValue().set("code","1234",3, TimeUnit.MINUTES);
        //setnx
        redisTemplate.opsForValue().setIfAbsent("lock","1");
        redisTemplate.opsForValue().setIfAbsent("lock","2");
    }
	/**
     * 操作哈希类型的数据
     */
    @Test
    public void testHash(){
        //hset hget hdel hkeys hvals
        HashOperations hashOperations = redisTemplate.opsForHash();
        //hset
        hashOperations.put("100","name","Tom");
        hashOperations.put("100","age","20");

        //hget
        String name = (String) hashOperations.get("100", "name");
        System.out.println(name);

        //hkeys
        Set keys = hashOperations.keys("100");
        System.out.println(keys);

        //hvals
        List values = hashOperations.values("100");
        System.out.println(values);

        //hdel
        hashOperations.delete("100","age");
    }
	/**
     * 操作列表类型的数据
     */
    @Test
    public void testList(){
        //lpush lrange rpop llen
        ListOperations listOperations = redisTemplate.opsForList();

        //lpush
        listOperations.leftPushAll("mylist","a","b","c");
        listOperations.leftPush("mylist","d");

        //lrange
        List mylist = listOperations.range("mylist", 0, -1);
        System.out.println(mylist);

        //rpop
        listOperations.rightPop("mylist");

        //llen
        Long size = listOperations.size("mylist");
        System.out.println(size);
    }
	/**
     * 操作集合类型的数据
     */
    @Test
    public void testSet(){
        //sadd smembers scard sinter sunion srem
        SetOperations setOperations = redisTemplate.opsForSet();

        //sadd
        setOperations.add("set1","a","b","c","d");
        setOperations.add("set2","a","b","x","y");

        //smembers
        Set set1 = setOperations.members("set1");
        System.out.println(set1);

        //scard
        Long size = setOperations.size("set1");
        System.out.println(size);

        //sinter
        Set intersect = setOperations.intersect("set1", "set2");
        System.out.println(intersect);

        //sunion
        Set union = setOperations.union("set1", "set2");
        System.out.println(union);

        //srem
        setOperations.remove("set1","a","b");
    }
	/**
     * 操作有序集合类型的数据
     */
    @Test
    public void testZset(){
        //zadd zrange zincrby zrem
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();

        //zadd
        zSetOperations.add("zset1","a",10);
        zSetOperations.add("zset1","b",12);
        zSetOperations.add("zset1","c",9);

        //zrange
        Set zset1 = zSetOperations.range("zset1", 0, -1);
        System.out.println(zset1);

        //zincrby
        zSetOperations.incrementScore("zset1","c",10);

        //zrem
        zSetOperations.remove("zset1","a","b");
    }
	/**
     * 通用命令操作
     */
    @Test
    public void testCommon(){
        //keys exists type del
        //keys
        Set keys = redisTemplate.keys("*");
        System.out.println(keys);

        //exists
        Boolean name = redisTemplate.hasKey("name");
        Boolean set1 = redisTemplate.hasKey("set1");

        //type
        for (Object key:keys){
            DataType type = redisTemplate.type(key);
            System.out.println(type);
        }

        //del
        redisTemplate.delete("mylist");
    }

你可能感兴趣的:(SpringBoot,数据库,spring,boot,redis,后端)