Redis学习《2.RedisTemplate五种数据类型基本API操作》

Java中使用redis很少会去使用原始的jedis的API,除非是自己重构框架之类,所以使用spring有的RedisTemplate。

一、maven引入依赖好,我是springboot项目,如下pom.xml文件:

  
            org.springframework.boot
            spring-boot-starter-data-redis
   

 

二、配置文件的application.yml文件,如:

  redis:
    host: localhost
    port: 6379
    pool:
      max-active: 8
      max-wait: 1
      max-idle: 8
      min-idle: 0
      timeout: 0

三、使用RedisTemplate模本要先配置一下,即就是对模板的序列化。

@Configuration
public class RedisConfig {

    @Bean(value = {"DEMO"})
    @Autowired
    public RedisTemplate objectRedis(RedisConnectionFactory connectionFactory){
        RedisTemplate redis = new RedisTemplate();
        redis.setKeySerializer(new StringRedisSerializer());
        Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(String.class);
        redis.setValueSerializer(serializer);
        redis.setHashKeySerializer(new StringRedisSerializer());
        redis.setHashValueSerializer(serializer);
        redis.setConnectionFactory(connectionFactory);
        return redis;
    }
}

四、下面就可以使用了:

1、string的操作:

@RestController
@RequestMapping("/redis")
public class RedisTempalteDemo {

    @Qualifier("DEMO")
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * string 操作
     * @param key
     * @param value
     * @return
     */
    @RequestMapping("/string")
    public String stringRedis(@RequestParam String key, @RequestParam String value) {
       //添加
        redisTemplate.opsForValue().set(key, value);
        //添加并设置过期时间
        redisTemplate.opsForValue().set("kak","4552",7200, TimeUnit.SECONDS);
        //自增
        redisTemplate.opsForValue().increment("user_count",1);
        //获取重新设置value
        redisTemplate.opsForValue().getAndSet("kak","hello");
        //追加到字符串的末尾
        redisTemplate.opsForValue().append("kak","world");
        //获取字符长度
        redisTemplate.opsForValue().size("kak");

        return redisTemplate.opsForValue().get(key);
    }
}

2、hash的操作:

    /***
     * hash操作
     * @return
     */
    @GetMapping("/hash")
    public Map  hashRedis(){
        //添加单个
        redisTemplate.opsForHash().put("user","id","001");
        //点加多个
        Map map = new HashMap<>();
        map.put("name","小明");
        map.put("sex","男");
        map.put("age","20");
        redisTemplate.opsForHash().putAll("user",map);

        //键集合
       Set keys =  redisTemplate.opsForHash().keys("user");
        System.out.println("keys:"+keys);
       //value集合
        List values =  redisTemplate.opsForHash().values("user");
        System.out.println("values:"+values);
        //遍历map
        Cursor> entryCursor  = redisTemplate.opsForHash().scan("user", ScanOptions.NONE);
        while (entryCursor.hasNext()){
            Map.Entry entry =entryCursor.next();
            System.out.println("键:"+entry.getKey()+"值:"+entry.getValue());
        }
       //获取整个map
        return redisTemplate.opsForHash().entries("user");
    } 
  

3、set的操作:

     /**
     * set操作
     * @param key
     * @return
     */
    @GetMapping("/set")
    public Set setRedis(@RequestParam(required = false) String key){
        String lkl = key == null ?"tel":key;
        //添加一个或者多个
        String[] ste = new String[]{"123","456","789","45","6"};
        redisTemplate.opsForSet().add(lkl,ste);
        //移除一个或多个
        ste= new String[]{"123"};
        redisTemplate.opsForSet().remove(lkl,ste);
        //遍历
        Cursor cursor = redisTemplate.opsForSet().scan(lkl,ScanOptions.NONE);
        while (cursor.hasNext()){
            System.out.println("set成员元素:"+cursor.next());
        }
        //获取所有元素
        return  redisTemplate.opsForSet().members(key);
    }

4、list的操作:

    /**
     * list操作
     * @return
     */
    @GetMapping("/list")
    public List listRedis(){
        //表头插入单个
        redisTemplate.opsForList().leftPush("left-list","java");
        //表头插入多个
        String [] arr = new String[]{"js","html","c#","C++"};
        redisTemplate.opsForList().leftPushAll("left-list",arr);
        //表尾插入单个
        redisTemplate.opsForList().rightPush("rught-list","java");
        //表尾插入多个
        redisTemplate.opsForList().rightPushAll("rught-list",arr);
        //设置位置
        redisTemplate.opsForList().set("rught-list",0,"第一个");
        //删除:count> 0:删除等于从头到尾移动的值的元素。count <0:删除等于从尾到头移动的值的元素。count = 0:删除等于value的所有元素。
        redisTemplate.opsForList().remove("rught-list",1,"js");//

        return  redisTemplate.opsForList().range("left-list",0,100);
    }

 

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