springboot redis 用法简介

pom.xml

<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
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 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 keys = redisTemplate.keys("name*");
        if (keys.size() > 0)
            redisTemplate.delete(keys);
    }


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

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

你可能感兴趣的:(springboot,redis,springboot)