前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringCache学习实践

SpringCache学习实践

作者头像
老梁
发布2019-09-10 18:12:40
3810
发布2019-09-10 18:12:40
举报

1. SpringCache学习实践

1.1. 引用

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

1.2. 启用方式

1.2.1. 添加一种cacheManager的bean

  1. 若注解了@EnableCaching,则spring可自动发现并配置cacheManager,只要有一种可用于缓存提供的即可,详情见文献[1]。常用的有Ehcache、redis等实现
代码语言:javascript
复制
@Configuration
    @EnableCaching
    public class CacheConfiguration {
        @Bean
        public CacheManager cacheManager() {
            SimpleCacheManager cacheManager = new SimpleCacheManager();
            cacheManager.setCaches(Collections.singletonList(new ConcurrentMapCache("models")));
            return cacheManager;
        }
    }

1.3. 使用方式

  1. 三个主要的注解 Cacheable (最常用的注解,用于标注需要缓存方法)、CacheEvict(用于仅清除缓存)、CachePut(用于仅存放缓存)
  2. 先定义一个测试POJO: TestModel。 含有name和address两个字符串变量。
代码语言:javascript
复制
class TestModel {
       String name;
       String address;
       // 省略getter和setter
   }

1.3.1. Cacheable

代码语言:javascript
复制
@Cacheable(value = "models", key = "#testModel.name", condition = "#testModel.address !=  '' ")
public TestModel getFromMem(TestModel testModel) throws InterruptedException {
    TimeUnit.SECONDS.sleep(1);
    testModel.setName(testModel.getName().toUpperCase());
    return testModel;
}
  1. 例子里的注解@Cacheable中存在有以下几个元素
    • value (也可使用 cacheNames) : 可看做命名空间,表示存到哪个缓存里了。
    • key : 表示命名空间下缓存唯一key,使用Spring Expression Language(简称SpEL,详见参考文献[5])生成。
    • condition : 表示在哪种情况下才缓存结果(对应的还有unless,哪种情况不缓存),同样使用SpEL
  2. 当第一次使用
代码语言:javascript
复制
{name: 'XiaoMing', address: 'ChengDu'}

调用getFromMem时,会等待一秒钟,然后返回

代码语言:javascript
复制
{name: 'XIAOMING', address: 'ChengDu'}

再次使用name为’XiaoMing’的对象作为参数调用getFromMem时,会立即返回上一个结果,无论参数中的address是什么。 但是如果第一次调用时,address为空字符串,第二次调用仍然需要等待一秒钟,这就是condition的作用。

1.3.2. CacheEvict

代码语言:javascript
复制
@CacheEvict(value = "models", allEntries = true)
@Scheduled(fixedDelay = 10000)
public void deleteFromRedis() {
}

@CacheEvict(value = "models", key = "#name")
public void deleteFromRedis(String name) {
}
  1. 例子里的注解 @CacheEvict 中存在有以下几个元素
    • value (也可使用 cacheNames) : 同Cacheable注解,可看做命名空间。表示删除哪个命名空间中的缓存
    • allEntries: 标记是否删除命名空间下所有缓存,默认为false
    • key: 同Cacheable注解,代表需要删除的命名空间下唯一的缓存key
  2. 例子中第一段,与 @Scheduled 注解同时使用,每十秒删除命名空间name下所有的缓存
  3. 第二段,调用此方法后删除命名空间models下, key == 参数 的缓存 同样含有unless与condition

1.3.3. CachePut

代码语言:javascript
复制
@CachePut(value = "models", key = "#name")
public TestModel saveModel(String name, String address) {
    return new TestModel(name, address);
}
  1. 例子里的注解 @CachePut 中存在有以下几个元素
    • value: 同上
    • key: 同上
    • condition(unless): 同上
  2. 比如可用于后台保存配置时及时刷新缓存。

1.4. Redis作为缓存配置

1.4.1. 引用

  1. 再加依赖
代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>
  1. 然后在配置中添加RedisConnectionFactory用于获取redis链接
代码语言:javascript
复制
 @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private int redisPort;

    @Value("${spring.redis.timeout}")
    private int redisTimeout;

    @Value("${spring.redis.password}")
    private String redisAuth;

    @Value("${spring.redis.database}")
    private int redisDb;

    @Value("${spring.redis.pool.max-active}")
    private int maxActive;

    @Value("${spring.redis.pool.max-wait}")
    private int maxWait;

    @Value("${spring.redis.pool.max-idle}")
    private int maxIdle;

    @Value("${spring.redis.pool.min-idle}")
    private int minIdle;

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(maxActive);
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMaxWaitMillis(maxWait);
        poolConfig.setMinIdle(minIdle);
        poolConfig.setTestOnBorrow(true);
        poolConfig.setTestOnReturn(false);
        poolConfig.setTestWhileIdle(true);
        JedisClientConfiguration clientConfig = JedisClientConfiguration.builder()
                .usePooling().poolConfig(poolConfig).and().readTimeout(Duration.ofMillis(redisTimeout)).build();

        // 单点redis
        RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
        // 哨兵redis
        // RedisSentinelConfiguration redisConfig = new RedisSentinelConfiguration();
        // 集群redis
        // RedisClusterConfiguration redisConfig = new RedisClusterConfiguration();
        redisConfig.setHostName(redisHost);
        redisConfig.setPassword(RedisPassword.of(redisAuth));
        redisConfig.setPort(redisPort);
        redisConfig.setDatabase(redisDb);

        return new JedisConnectionFactory(redisConfig,clientConfig);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        Jackson2JsonRedisSerializer<Object> serializer = jackson2JsonRedisSerializer();
        redisTemplate.setConnectionFactory(redisConnectionFactory());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(serializer);
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(serializer);
        return redisTemplate;
    }

    @Bean
    public Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer() {
        final Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        final ObjectMapper objectMapper = Jackson2ObjectMapperBuilder
                .json().build();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        return jackson2JsonRedisSerializer;
    }
本文参与?腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-01-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体同步曝光计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. SpringCache学习实践
    • 1.1. 引用
      • 1.2. 启用方式
        • 1.2.1. 添加一种cacheManager的bean
      • 1.3. 使用方式
        • 1.3.1. Cacheable
        • 1.3.2. CacheEvict
        • 1.3.3. CachePut
      • 1.4. Redis作为缓存配置
        • 1.4.1. 引用
    相关产品与服务
    云数据库 Redis
    腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
    http://www.vxiaotou.com