SpringBoot整合Redis

  1. xml中添加redis依赖
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

  2. 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
    		

  3. 新建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;
        }
    }
  4. 代码使用示例

    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);
    
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值