springboot redis 用法简介

pom.xml

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

application.yml

 #redis 配置
    redis:
      database: 0
      host: 127.0.0.1
      port: 6379
      password:
      pool:
        max-active: 8
        max-wait: -1
        max-idle: 8
        min-idle: 0
      timeout: 0

RedisConfig.java


@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {


    @Bean
    public KeyGenerator keyGenerator() {

        return new KeyGenerator() {
            @Override
            public Object generate(Object o, Method method, Object... objects) {
                StringBuilder sb = new StringBuilder();
                sb.append(o.getClass().getName());
                sb.append(method.getName());
                for (Object param : objects
                        ) {
                    sb.append(param.toString());
                }
                return sb.toString();
            }
        };

    }


    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
        //设置缓存过期时间
        // rcm.setDefaultExpiration(60);//秒
        //设置value的过期时间
        return rcm;
    }

    /**
     * RedisTemplate配置
     *
     * @param factory
     * @return
     */
    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);//如果key是String 需要配置一下StringSerializer,不然key会乱码 /XX/XX
        template.afterPropertiesSet();
        //template.setStringSerializer();
        return template;
    }

}

简单的测试


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {LiveApplication.class})
public class RedisTest {


    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    LiveRoomMapper liveRoomMapper;

    @Test
    public void test() {
        stringRedisTemplate.opsForValue().set("name", "xiaozhun");
        Assert.assertEquals("xiaozhun", stringRedisTemplate.opsForValue().get("name"));
    }

    //批量删除
    @Test
    public void delete() {
        Set<Serializable> keys = redisTemplate.keys("name*");
        if (keys.size() > 0)
            redisTemplate.delete(keys);
    }


    //初始化channelID:callback_url 集合
    @Test
    public void initChannelId() {
        List<LiveRoom> list = liveRoomMapper.selectAll();
        list.forEach(liveRoom -> {
            stringRedisTemplate.opsForValue().set("channelId:" + liveRoom.getChannelId(), liveRoom.getCallbackUrl()+"");
        });
    }
}

更多工具类参见 :
https://my.oschina.net/wangxincj/blog/825838

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值