• springboot使用redis


    springboot使用redis

    1.开启redis

    2.导入依赖

    就只需要这一个依赖!不需要spring-boot-starter-cache

      <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-data-redisartifactId>
      dependency>
    
    • 1
    • 2
    • 3
    • 4

    当你导入这一个依赖时,SpringBoot的CacheManager就会使用RedisCache。

    如果你的Redis使用默认配置,这时候已经可以启动程序了。

    3.配置Redis

    # Redis数据库索引(默认为0)
    spring.redis.database=1
    # Redis服务器地址
    spring.redis.host=127.0.0.1
    # Redis服务器连接端口
    spring.redis.port=6379
    # Redis服务器连接密码(默认为空)
    spring.redis.password=
    # 连接池最大连接数(使用负值表示没有限制)
    spring.redis.pool.max-active=1000
    # 连接池最大阻塞等待时间(使用负值表示没有限制)
    spring.redis.pool.max-wait=-1
    # 连接池中的最大空闲连接
    spring.redis.pool.max-idle=10
    # 连接池中的最小空闲连接
    spring.redis.pool.min-idle=2
    # 连接超时时间(毫秒)
    spring.redis.timeout=0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    4.模板编程

        @Autowired
        private StringRedisTemplate stringRedisTemplate;//操作key-value都是字符串
     
        @Autowired
        private RedisTemplate redisTemplate;//操作key-value都是对象
     
        /**
         *  Redis常见的五大数据类型:
         *  stringRedisTemplate.opsForValue();[String(字符串)]
         *  stringRedisTemplate.opsForList();[List(列表)]
         *  stringRedisTemplate.opsForSet();[Set(集合)]
         *  stringRedisTemplate.opsForHash();[Hash(散列)]
         *  stringRedisTemplate.opsForZSet();[ZSet(有序集合)]
         */
        public void test(){
            stringRedisTemplate.opsForValue().append("msg","hello");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    5.简单使用

    //用的hutool
    @Autowired
        private StringRedisTemplate stringRedisTemplate;//操作key-value都是字符串
    	
    	public static final string files_key="FILES_FRONT_ALL";
    
    	@GetMapping("/index")
    public Resut index(){
      String jsonStr=  stringRedisTemplate.opsForValue().get(FILES_KEY)
    	List<Files> files;
        if(StrUtil.isBlannk(jsonStr)){
            files = fileMapper.selectList(null) ;// 查询数据库数据
        	//缓存到redis 注意 保存的是JSON格式
            stringRedisTemplate.opsForValue().set(FILES_KEY,JSONUtil.toJSONStr(files));
        }else{
            files =JSONUtil.toBean(jsonStr,new TypeReference<List<Files>>(){},true);
        }
        return Result.success(files);
    }
    
    
    //解决增删改缓存没及时刷新的问题
    //方案一
    执行增删改之前删除当前key,查询那重新添加key
        
       private void flusRedis(String key){
        stringRedisTemplate.delete(key);
    }
    //方案二
    查询数据库新的数据,重新设置key
        private void setCache(String key,String value){  
        stringRedisTemplate.opsForValue().set(key,value);  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
  • 相关阅读:
    电脑投屏到TCL电视鼠标延迟
    C语言实现调用python绘图
    二叉查找树、平衡二叉树、红黑二叉树简单概念
    java-php-python-火炬中学校刊在线投稿审稿系统计算机毕业设计
    规范 Git 提交说明
    电影下载工具推荐
    计算机图形学中的几何光学
    如何恢复电脑上删除的文件?快速恢复被删除文件的技巧【5个实用方法】
    Linux基础入门到精通之Linux系统配置IP
    1089 不能被3整除的数
  • 原文地址:https://blog.csdn.net/xixihaha_coder/article/details/126159867