Springboot+Redis缓存(windows下配置)
下载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; } }