springboot+redis缓存(windows下配置)

zhouxiujuan      2022-05-04     337

关键词:

下载redis免安装压缩包,解压

技术图片

 

 

 

1.cmd命令窗口进入当前路径下,执行启动命令:

H:

cd H:Redis-x64-3.2.100

redis-server redis.windows.conf 

 

一、使用随机默认的数据库方式(使用springboot2.2.7+redis3.1.0)

1..pom文件引入依赖

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

2.


@Controller
public class MainController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
 
    @RequestMapping(value = "getBaseData")
    public @ResponseBody
    Map getBaseData(HttpServletRequest request){
        String userId = request.getParameter("userId");
        Map baseData = new HashMap<>();
        if(stringRedisTemplate.hasKey(userId)){
            String baseDataInfo =stringRedisTemplate.opsForValue().get(userId);
            Gson gson = new Gson();
            baseData = gson.fromJson(baseDataInfo, baseData.getClass());
        }
        else{
            //设置缓存
            stringRedisTemplate.opsForValue().set(userId,new Gson().toJson(baseData),60, TimeUnit.SECONDS);
        }
        return  baseData;
    }

}

方法总结:

  1.判断redis数据库中是否存在key为keyName的数据(boolean)

    stringRedisTemplate.hasKey(keyName)  

  2.新增数据(value转化成JSON字符串存放)

  stringRedisTemplate.opsForValue().set(key,value);  //未设置失效时间
 stringRedisTemplate.opsForValue().set(key,value,60, TimeUnit.SECONDS);//60s后失效
  3.获取数据(字符串)
  stringRedisTemplate.opsForValue().get(userId);

  4.根据key删除缓存数据
  stringRedisTemplate.delete(key);

二、指定不同数据存储数据
因为需要用到多个数据库缓存数据,所以要指定不同数据缓存,但是基于springboot和redis版本,使用
LettuceConnectionFactory缓存指定数据库并不生效。最后降低redis版本,改用jedis

1.pom文件引入如下依赖
     <!--Redis缓存-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--因为数据要分库,所以只能用jedis-->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

2.创建redis工具类

public class RedisUtil {

//    /**
//     * 切换redis数据库(3.1版本发现使用这个报错,只能修改pom文件降到jedis版本)
//     * @param redisTemplate springboot封装的redis对象
//     * @param index  数据库下标
//     */
//    public static void select(RedisTemplate redisTemplate, int index){
//        LettuceConnectionFactory lettuceConnectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory();
//        if(lettuceConnectionFactory != null){
//            lettuceConnectionFactory.setDatabase(index);
//            redisTemplate.setConnectionFactory(lettuceConnectionFactory);
//            lettuceConnectionFactory.resetConnection();
//        }
//    }

    public static void change(RedisTemplate stringRedisTemplate, int index){
        JedisConnectionFactory jedisConnectionFactory = (JedisConnectionFactory) stringRedisTemplate.getConnectionFactory();
        System.out.println("当前所在的db:"+jedisConnectionFactory.getDatabase());
        jedisConnectionFactory.setDatabase(index);
        stringRedisTemplate.setConnectionFactory(jedisConnectionFactory);
        ValueOperations valueOperations = stringRedisTemplate.opsForValue();
        System.out.println("所在的db:"+jedisConnectionFactory.getDatabase());
    }
}

3.使用

@Controller
public class MainController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
   
  @Override
    public Map queryUserData(String userId ){
        Map userMap = new HashMap();
        //指定数据库1
        RedisUtil.change(stringRedisTemplate,1);
        Gson gson = new Gson();
        if(stringRedisTemplate.hasKey(userId)){
            String userInfo =stringRedisTemplate.opsForValue().get(userId);
            userMap = gson.fromJson(userInfo, userMap.getClass());//json字符串转换为Map
        }else{
            System.out.println("=======缓存不存在重新加载:");
        stringRedisTemplate.opsForValue().set(userId,gson.toJson(userMap),60, TimeUnit.SECONDS);
        }
        return userMap;
    }

}

 



 
 

shiro整合springboot缓存之redis实现(代码片段)

Shiro整合Springboot缓存之Redis实现Redis下载安装引入redis依赖配置redis连接启动redis服务自定义redis缓存的实现自定义shiro缓存管理器自定义salt实现实现序列化接口自定义Realm改造开启redis缓存Redis下载安装Windows系统中Redis下载安装其它... 查看详情

springboot日记——redis整合

...windows安装使用redis吧~(看这里也可以)  正题:  SpringBoot对应(自带)Redis 查看详情

springboot整合redis实现缓存

...纲一、缓存的应用场景二、更新缓存的策略三、运行 springboot-mybatis-redis 工程案例四、springboot-mybatis-redis 工程代码配置详解 运行环境:MacOS10.12.xJDK8+Redis3.2.8SpringBoot1.5.1.RELEASE 一、缓存的应用场景什么是缓存?... 查看详情

springboot开启redis缓存(代码片段)

...之前不是说过Redis可以当作缓存用嘛现在我们就配置一下SpringBoot使用Redis的缓存Redis缓存为什么用Redis作缓存用redis做缓存,是因为re 查看详情

springboot整合redis缓存

...非常流行的NoSQL数据库(Redis)来实现我们的缓存需求。SpringBoot整合Redis是非常方便快捷的,我用的是Mybatis,这里就不说Springboot整合Mybatis了网上有很多,同样非常简单。下面进入正题: 查看详情

springboot2.x集成redis缓存(代码片段)

SpringBoot集成Redis缓存在此章,我们将SpringBoot集成Redis缓存,Redis是一个开源的,基于内存的数据结构存储,可以用作数据库、缓存和消息代理,在本章仅讲解缓存集成。准备工作当前项目工具及环境开发工具IDEA2020.3依赖管理MavenS... 查看详情

springboot+mybatis+redis实现二级缓存

对于查询比较多的项目可以考虑配置二级缓存,mybatis本身的二级缓存是缓存到本地,但是对于多个节点的项目来说,可能会出现数据不一致的问题,所以采用redis缓存,这样二级缓存的数据就可以缓存到内存,可实现多个节点项... 查看详情

springboot中使用redistemplate实现redis数据缓存

SpringBoot整合Redis数据库实现数据缓存的本质是整合Redis数据库,通过对需要“缓存”的数据存入Redis数据库中,下次使用时先从Redis中获取,Redis中没有再从数据库中获取,这样就实现了Redis做数据缓存。??按照惯例,下面一步一步... 查看详情

springboot+redis形成数据缓存

 步骤:第一 引入redis依赖包第二 配置application.propresties配之类  配置redis服务器地址第三 创建redis配置类开启缓存配置(@enableCaching)配置缓存管理器(cacheManger)配置KEY生成策略第四 仓库中直接使用@ca... 查看详情

springboot集成redis(缓存篇)

一前言公众号:知识追寻者知识追寻者(Inheritingthespiritofopensource,Spreadingtechnologyknowledge;)pring为我们提供的缓存注解SpringCache。Spring支持多种缓存技术:RedisCacheManager,EhCacheCacheManager、GuavaCacheManager等,今天的内容是集成RedisCacheMan 查看详情

springboot集成redis(缓存篇)

一前言公众号:知识追寻者知识追寻者(Inheritingthespiritofopensource,Spreadingtechnologyknowledge;)pring为我们提供的缓存注解SpringCache。Spring支持多种缓存技术:RedisCacheManager,EhCacheCacheManager、GuavaCacheManager等,今天的内容是集成RedisCacheMan 查看详情

springboot整合redis缓存

使用springBoot添加redis缓存需要在POM文件里引入<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><dependency 查看详情

精通springboot--整合redis实现缓存

今天我们来讲讲怎么在springboot中整合redis实现对数据库查询结果的缓存。首先第一步要做的就是在pom.xml文件添加spring-boot-starter-data-redis。要整合缓存,必不可少的就是我们要继承一个父类CachingConfigurerSupport。我们先看看这个类... 查看详情

springboot集成redis缓存

1、pom.xml增加redis缓存起步依赖(spring-boot-starter-parent包含许多starter版本)<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactI 查看详情

springboot入门:集成redis哨兵模式,实现mybatis二级缓存

本片文章续《SpringBoot入门(九):集成Quartz定时任务》。本文主要基于redis实现了mybatis二级缓存。较redis缓存,mybaits自带缓存存在缺点(自行谷歌)。本文是基于docker安装redis主从模式。1.redis安装(1)首先安装redis集群模式,... 查看详情

32springboot——缓存之整合redis

springboot缓存默认使用ConcurrentMapCacheManager 将数据保存在下面的Map中1、docker中开启Redis 2、添加Redis相关依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-st 查看详情

springboot缓存--redis单机缓存

 pom.xml  <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><dependency><groupId&g 查看详情

springboot和redis处理页面缓存

   页面缓存是应对高并发的一个比较常见的方案,当请求页面的时候,会先查询redis缓存中是否存在,若存在则直接从缓存中返回页面,否则会通过代码逻辑去渲染页面,并将渲染后的页面缓存到redis中,然后返回。... 查看详情