阿里缓存框架jetcache的使用

引入MAVEN包

        <dependency>
            <groupId>com.alicp.jetcache</groupId>
            <artifactId>jetcache-starter-redis-lettuce</artifactId>
            <version>${jetcache.latest.version}</version>
        </dependency>
        
   <jetcache.latest.version>2.7.2</jetcache.latest.version>

在使用JetCache时,可能会遇到@EnableCreateCacheAnnotation注解被弃用的问题。这是因为JetCache 2.7版本已经被弃用,所以需要降低JetCache的版本来解决该问题。

启动类添加参数

@EnableFeignClients
@EnableMethodCache(basePackages = "com.xx")

配置文件

jetcache.statIntervalMinutes=60
jetcache.areaInCacheName=false
jetcache.local.default.type=caffeine
jetcache.local.default.keyConvertor=fastjson
jetcache.local.default.limit=100
jetcache.remote.default.type=redis.lettuce
jetcache.remote.default.uri=redis://密码@用户名名:6379/
jetcache.remote.default.keyConvertor=fastjson2
jetcache.remote.default.valueEncoder=java
jetcache.remote.default.valueDecoder=java
jetcache.remote.default.poolConfig.minIdle=5
jetcache.remote.default.poolConfig.maxIdle=20
jetcache.remote.default.poolConfig.maxTotal=50

增加配置类,使用代码方式用缓存

@Configuration
public class JetcacheConfig {

    @Autowired
    private CacheManager cacheManager;

    //优惠券规则表
    private Cache<Integer, Object> promotionsRulerCache;

    @PostConstruct
    public void init(){
        QuickConfig qc = QuickConfig.newBuilder(RedisConstants.PROMOTIONS_LIST_RULER)
                .expire(Duration.ofSeconds(600))
                .cacheType(CacheType.REMOTE)
                // 本地缓存更新后,将在所有的节点中删除缓存,以保持强一致性
                .syncLocal(false)
                .build();
        promotionsRulerCache = cacheManager.getOrCreateCache(qc);
    }

    @Bean
    public Cache<Integer, Object> getPromotionsRulerCache(){
        return promotionsRulerCache;
    }
}
    @Autowired
    private Cache<Integer, Object> promotionsRulerCache;

  if(promotionsRulerCache.get(promoId) != null){
            return (PromotionsCouponRule) promotionsRulerCache.get(promoId);
        }

 promotionsRulerCache.remove(actId);

        promotionsRulerCache.put(promoId,promotionsCouponRule);

通过注解的方式

    @Cached(name= RedisConstants.PROMOTIONS_LIST_BIND_GOODS, key = "#promotionsList.id", expire = 120, timeUnit = TimeUnit.SECONDS, cacheType = CacheType.REMOTE)
    public List<Long> getPromotionGoodsList(PromotionsList promotionsList){}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值