Springboot操作RedisTemplate-字符串类型

springboot操作 RedisTemplate-字符串类型

redis 的命令很多 工作中也没什么机会去使用Redis 更多的是使用Spring Boot 提供的一套模板方法

redisTemplate 封装了很多的方法

  • ValueOperations:简单字符串类型数据操作
  • SetOperations:set类型数据操作
  • ZSetOperations:zset类型数据操作
  • HashOperations:map类型数据操作
  • ListOperations:list类型数据操作

这里的就使用下string 类型 ,其他类型我使用的不多,毕竟是个CRUD boy

安装redis 使用下docker 吧

docker run -di --name redis -p 6379:6379 redis

创建好boot 项目 导入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>

配置下yml

spring:
 redis: 
  host : 你的地址
  port : 默认6379

开始编写 spring boot test

@SpringBootTest
class RedisTemplateApplicationTests {
    
    

    /**
     * @Autowired默认按类型进行装配,
     * @Resource默认按照名称进行装配
     * redis 默认是 <Object ,Object> 类型
     */
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    /**
     *
     */
    @Test
    void test(){
    
    
        ValueOperations<String, Object> string = redisTemplate.opsForValue();
        string.set("name","翁艳敏");
    }

    @Test
    void contextLoads() {
    
    
    }

}

在这里插入图片描述

这里使用的是默认的 jdk 序列化 不太美观 我改下:

创建redisconfig

package com.wym.redis.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author wym
 * @date 2021/3/1 22:18
 */
@Configuration
public class RedisConfig {
    
    
    @Bean
    public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
    
    
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        // 设置
        template.setValueSerializer(new StringRedisSerializer());
        template.setKeySerializer(new StringRedisSerializer());
        return template;
    }
}

使用String 类型 api 添加 :

 @Test
    void testSet(){
    
    
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
        //直接设置 key value
        operations.set("name","wym");
        //设置偏移量 从哪里开始
        operations.set("name","wym2",3);
        //设置10分钟过期 10 , 单位
        operations.set("name","wym3",10, TimeUnit.MINUTES);
        //设置批量保存
        Map<String,String> map=new HashMap<>();
        map.put("name1","wym3");
        map.put("name2","wym3");
        map.put("name3","wym3");
        map.put("name4","wym3");
        operations.multiSet(map);

        //追加 存在就追加 没有 就保存 当前这个
        operations.append("name4","wym4");
        //存在返回 false 不存在 返回 true  并 添加 这个 key
        Boolean aBoolean = operations.setIfAbsent("lock", "value");
        System.out.println(aBoolean);
    }

在这里插入图片描述

使用 String api 获取 value

@Test
    void testGet() {
    
    
        ValueOperations<String, Object> string = redisTemplate.opsForValue();
        Object name = string.get("name");
        System.out.println(name);

        //批量获取
        List<String> keys=new ArrayList<>();
        keys.add("name");
        keys.add("name1");
        keys.add("name2");

        List<Object> objects = string.multiGet(keys);
        for (Object object : objects) {
    
    
            System.out.println(object);
        }

    }

在这里插入图片描述

使用删除api

@Test
    void testDelete(){
    
    
        Boolean name = redisTemplate.delete("name");
        System.out.println(name);
    }

使用 递增 和递减 api

  /**
     * 测试自增 和减少
     * 使用在 点赞 浏览 等业务方面
     */
    @Test
    void  testIncr(){
    
    
        ValueOperations<String, Object> string  = redisTemplate.opsForValue();
        //返回的值
        Long age = string.increment("age");
        //设置 步长   每次加3
        string.increment("age",3);

        // 每次减少
        string.decrement("money",500);

    }

好了 总结了下 字符串类型 常用的api ,希望能帮助到大家 记不住的时候快速上手

​ 分享让我们进步更快

猜你喜欢

转载自blog.csdn.net/weng74/article/details/114273752