1、redis的具体安装就不说了,直接连接程序,配置在项目test002中,依赖
<!-- redis数据存储 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- JSON处理器 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.70</version> </dependency>
2、配置yml
spring: profiles: active: dev datasource: #配置数据库 driverClassName: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3307/springcloud_test002?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC username: root password: root redis: host: 192.168.110.12 port: 6379 password: Cc68db0e
3、配置redis公共类
package com.example.test002.mapper.common; import com.alibaba.fastjson.JSON; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.concurrent.TimeUnit; @Component public class RedisMapper { private StringRedisTemplate stringRedisTemplate; @Resource public void RedisMapper(StringRedisTemplate stringRedisTemplate) { this.stringRedisTemplate = stringRedisTemplate; } /** * 判断key是否存在 * @param key 键 * @return true 存在 false不存在 */ public boolean hasKey(String folder, String key) { try { return stringRedisTemplate.hasKey(folder + key); } catch (Exception e) { e.printStackTrace(); return false; } } /** * 功能描述: 普通缓存放入,全部对象都以JSON保存 * @param folder, key, value * @return boolean true成功 false失败 * @author admin * @date 2021/7/19 11:03 * @update [序号] [时间] [修改人] [自定义变更内容] */ public boolean set(String folder, String key, Object value) { try { String json = JSON.toJSONString(value); stringRedisTemplate.opsForValue().set(folder + key, json); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 普通缓存获取 * @param key 键 * @return 值 */ public String get(String folder, String key) { return key == null ? null : stringRedisTemplate.opsForValue().get(folder + key); } /** * 普通缓存放入并设置时间 * @param key 键 * @param value 值 * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期 * @return true成功 false 失败 */ public boolean set(String folder, String key, Object value, long time) { try { String json = JSON.toJSONString(value); if (time > 0) { stringRedisTemplate.opsForValue().set(folder + key, json, time, TimeUnit.SECONDS); } else { stringRedisTemplate.opsForValue().set(folder + key, json, 36000, TimeUnit.SECONDS); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } }
4、编写方法测试
package com.example.test002.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.example.test002.entity.SysUserDO; import com.example.test002.mapper.SysUserMapper; import com.example.test002.mapper.common.RedisMapper; import org.apache.tomcat.jni.User; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; import java.util.Map; /** * redis 测试 * @author admin * @date 2022/2/10 9:47 */ @RestController @RefreshScope //nacos配置中心动态刷新 public class RedisController { @Resource private RedisMapper redisMapper; /** * 功能描述: 判断redis中是否存在这个key * @author admin * @return boolean * @date 2022/2/10 13:30 */ @GetMapping(value = "/redis/hasKey") public boolean hasKey() throws InterruptedException { String folder = "xxxTest:"; String key = "aaaaa"; boolean hasKey = redisMapper.hasKey(folder, key); return hasKey; } /** * 功能描述: redis存储值 * @author admin * @return boolean * @date 2022/2/10 13:30 */ @GetMapping(value = "/redis/set") public boolean set() throws InterruptedException { String folder = "xxxTest:"; String key = "aaaaa"; SysUserDO user = new SysUserDO(); user.setId(111); user.setRolename("测试数据"); boolean set = redisMapper.set(folder, key, user); return set; } /** * 功能描述: redis获取值 * @author admin * @return boolean * @date 2022/2/10 13:30 */ @GetMapping(value = "/redis/get") public SysUserDO get() throws InterruptedException { String folder = "xxxTest:"; String key = "aaaaa"; SysUserDO sysUserDO = JSON.parseObject(redisMapper.get(folder, key), new TypeReference<SysUserDO>() { }); return sysUserDO; } }
访问接口,都能成功