本地缓存
- 继承AbstractBaseLocalCache,并实现相应的抽象方法;
- 适用于单实例项目或不会经常变化的数据;
- 代码示例:
/*** 仓库信息缓存类*/
@Service
public class WarehouseCache extends AbstractBaseLocalCache<Long, Warehouse> {/*** 仓库信息服务*/@Autowiredprivate WarehouseFacade warehouseFacade;@Overrideprotected Integer getExpiredMinutes() {return 15;}@Overrideprotected Warehouse query(Long warehouseId) throws PendingException {return warehouseFacade.mustGet(WarehouseQuery.builder().id(warehouseId).build());}
}
Redis缓存
- 继承AbstractBaseRedisCache,并实现相应的抽象方法;
- 适用于多实例项目
- 代码示例:
/*** 商品信息缓存*/
@Service
public class GoodsCache extends AbstractBaseRedisCache<Long, Goods> {/*** 商品服务*/@Autowiredprivate GoodsFacade goodsFacade;@Overrideprotected Integer getExpiredMinutes() {return 15;}@Overrideprotected Goods query(Long id) throws PendingException {return goodsFacade.mustGet(GoodsQuery.builder().id(id).build());}@Overrideprotected int getVersion() {return 0;}
}