- xml中添加redis依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
- application.yml中添加配置
spring redis: database: 1 host: 127.0.0.1 password: 123456 port: 6379 jedis: pool: max-active: 8 max-wait: -1 max-idle: 8 min-idle: 0
- 新建RedisConfig配置类
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 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.HashOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration @EnableAutoConfiguration public class RedisConfig { /** * 注入 RedisConnectionFactory */ @Autowired RedisConnectionFactory redisConnectionFactory; /** * 实例化 RedisTemplate 对象 * * @return */ @Bean public RedisTemplate<String, Object> functionDomainRedisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); //设置value的序列化方式为json,即保存到reids中是一个json,方便查看 redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setConnectionFactory(factory); return redisTemplate; } }
-
代码使用示例
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.junit4.SpringRunner; import java.util.concurrent.TimeUnit; @RunWith(SpringRunner.class) @SpringBootTest(classes = DemoApplication.class) public class MainTest { @Autowired private RedisTemplate<String,Object> redisTemplate; @Test public void test1(){ Pop pop = new Pop(); /** * string 类型 * redis中数据结构为 * key: "demo1" * value:{ * popId: 1, * popName: "popTest" * } */ redisTemplate.opsForValue().set("demo1",pop); /** * hash 类型 * hash类型无法对 hash的key设置过期时间, * 只能对最完成redis Key设置过期时间 */ redisTemplate.opsForHash().put("redisKey","demo1",pop); redisTemplate.expire("redisKey",10, TimeUnit.SECONDS); /** * zset 类型 * zset 参数有三个 ,key, value ,score * 如果想使用hash,又想对hash 的key 设置过期,并且对hash的value值无所谓, * 不如使用zset * 将score 值设为过期时间, 如果score < 当前时间 ,则删除 */ long score = System.currentTimeMillis(); redisTemplate.opsForZSet().add("zsetRedisKey","1", score); // 删除 zsetRedisKey 中 分数 < 当前时间的 value Long count = redisTemplate.opsForZSet().removeRangeByScore("zsetRedisKey", 0, score); } }
SpringBoot整合Redis
最新推荐文章于 2024-04-14 22:59:18 发布