• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

redis 配置以及缓存(javaconfig)

spring-mvc seal_de 来源:seal_de 17小时前 5次浏览

redis 配置以及缓存(javaconfig)

redis
  • 是一种数据库,一种数据库,一种数据库

  • 如何缓存:将想要缓存的数据添加到 redis 数据库中

  • redis:基于内存亦可持久化的 Key-Value 数据库,所以速度快

maven

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.1.3</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>${redis.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>${spring.data.redis.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
</dependency>

gradle

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-redis")
    compile("com.fasterxml.jackson.core:jackson-databind:$jacksonVersion")
    compile("redis.clients:jedis:$redisVersion")
    ...
}

redis 配置

@EnableCaching
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
    @Bean
    @Override
    public KeyGenerator keyGenerator() {
        return (target, method, params) -> {
            StringBuilder sb = new StringBuilder();
            sb.append(target.getClass().getName());
            sb.append(method.getName());
            for (Object obj : params) {
                sb.append(obj.toString());
            }
            return sb.toString();
        };
    }

    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        return new RedisCacheManager(redisTemplate);
    }


    @Bean
    public RedisConnectionFactory redisCF() {
        JedisConnectionFactory cf = new JedisConnectionFactory();
        cf.setPassword("$2a$10$7e3cgzcCqwapf.TmEl1vi.b4ibSRxhqRjZKmRf9fV5b5u5L/CWaIa");
        return cf;
    }

    @Bean
    public StringRedisTemplate redisTemplate(RedisConnectionFactory cf) {
        StringRedisTemplate redis = new StringRedisTemplate(cf);
        Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        serializer.setObjectMapper(objectMapper);
        redis.setValueSerializer(serializer);
        redis.afterPropertiesSet();
        return redis;
    }

    ...
}
  • @EnableCaching 启动 spring 缓存

  • keyGenerator bean 是默认生成主键的方式

  • cacheManager bean 是缓存管理器

  • StringRedisTemplate 是 spring-data-redis 的一种模板,还有一种模板是 RedisTemplate<K, V>

手动使用 redis 做缓存

public List<Provinces> findAll() {
    long provinces_redis_size = redis.opsForList().size("provinces").longValue();
    List<Provinces> redisProvinces = redis.opsForList().range("provinces", 0, provinces_redis_size);
    if(redisProvinces.size() != 0)
        return redisProvinces;

    List<Provinces> persistProvinces = repository.findAll();
    for(Provinces provinces : persistProvinces) {
        redis.opsForList().rightPush("provinces", provinces);
    }

    return persistProvinces;
}
  • 思路就是查询的时候从 redis 中查询,若查到了,就将数据返回;若没查到,就从正在使用的数据库查(比如 MySql,H2等),然后将数据放入 redis 数据库中

使用 spring 注解缓存

@Cacheable(value = "provincesCache", key = "'provinces'")
public List<Provinces> findAllWithSpringCache() {
    return repository.findAll();
}
@Cacheable(value = "citiesCache", key = "#provinceId")
public List<Cities> findByProvinceId(String provinceId) {
    return repository.findByProvinceId(provinceId);
}

测试

  • 测试缓存省份 provinces

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class ProvincesServiceTest {
    @Autowired
    private ProvincesService provincesService;

    @Autowired
    private RedisTemplate<String, Provinces> redis;

    @Autowired
    private StringRedisTemplate stringRedis;

    @After
    public void cleanUp() {
        redis.delete("provinces");
        stringRedis.delete("provinces");
        stringRedis.delete("provincesCache~keys");
    }

    @Test
    public void findAll() {
        List<Provinces> list = provincesService.findAll();
        Assert.assertEquals(list.size(), 34);
    }

    @Test
    public void findAllWithSpringCache() {
        List<Provinces> list = provincesService.findAllWithSpringCache();
        Assert.assertEquals(list.size(), 34);
    }
}
  • 测试缓存市区 cities

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class CitiesServiceTest {
    @Autowired
    private CitiesService citiesService;

    @Autowired
    private StringRedisTemplate stringRedis;

    @After
    public void cleanUp() {
        stringRedis.delete("440000");
    }

    @Test
    public void findByProvinceId() {
        List<Cities> list = citiesService.findByProvinceId("440000");
        Assert.assertEquals(list.size(), 21);
    }
}

源码地址


本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:redis 配置以及缓存(javaconfig)
喜欢 (0)