参考链接:https://www.jianshu.com/p/c2e33c451ee0
在项目中,我们会使用到自定义bean,做一些特定义的功能方法,示例如下所示:
@Service
public class ProductService implements InitializingBean, DisposableBean {
@Autowired
private ProductSvcRemoteService productRemoteService;
// 创建本地缓存1:缓存产品信息
private Cache<String, ProductInfoGson> localeProductInfoCache;
// 创建本地缓存2:缓存产品id
private Cache<String, Long> productCodeToIdCache;
private static final String SEPARATOR = “|”;
// 程序停止,销毁
@Override
public void destroy() throws Exception {
localeProductInfoCache.cleanUp();
productCodeToIdCache.cleanUp();
}
@Override
public void afterPropertiesSet() throws Exception {
// 初始化缓存实例:缓存5秒没有更新,失效
localeProductInfoCache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.SECONDS).maximumSize(100).build();
// 初始化缓存实例:缓存10秒没有更新,失效
productCodeToIdCache = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).maximumSize(100).build();
}
private String getCacheKey(Long productId, String locale) {
return “productId:” + productId + SEPARATOR + locale;
}
private ProductInfoGson loadCachedProductInfo(final Long productId, final String locale) throws Exception {
String cacheKey = getCacheKey(productId, StringUtils.isEmpty(locale) ? Constants.DEFAULT_LOCALE : locale);
return localeProductInfoCache.get(cacheKey, new Callable() {
@Override
public ProductInfoGson call() throws Exception {
ProductInfoGson productInfo = productRemoteService.getProductDetailById(productId);
if (productInfo != null) {
mergeLocaleProductEntity(productId, locale, productInfo);
}
return productInfo;
}
});
}
private List loadCachedProductInfoByCodeList(List productCodeList, String locale) throws Exception {
List productInfos = new ArrayList();
String cacheLocale = StringUtils.isEmpty(locale) ? Constants.DEFAULT_LOCALE : locale;
// 需要做缓存的产品编码信息
List cacheProductCodeList = new ArrayList();
// 从本地缓存中获多语言产品信息
for (String productCode : productCodeList) {
Long productId = productCodeToIdCache.getIfPresent(productCode);
if (productId != null) {
ProductInfoGson productInfo = localeProductInfoCache.getIfPresent(getCacheKey(productId, cacheLocale));
if (productInfo != null) {
productInfos.add(productInfo);
continue;
}
}
cacheProductCodeList.add(productCode);
}
if (CollectionUtils.isEmpty(cacheProductCodeList)) {
return productInfos;
}
// 缓存中不存在的, 查询远程服务
List qProductInfos = productRemoteService.getProductDetailListByCodes(cacheProductCodeList);
if (CollectionUtils.isNotEmpty(qProductInfos)) {
Map<String, ProductInfoGson> cacheProductInfoMap = new HashMap<String, ProductInfoGson>();
Map<String, Long> cacheProductIdMap = new HashMap<String, Long>();
for (ProductInfoGson qProductInfo : qProductInfos) {
productInfos.add(qProductInfo);
cacheProductInfoMap.put(getCacheKey(qProductInfo.getId(), cacheLocale), qProductInfo);
cacheProductIdMap.put(qProductInfo.getCode(), qProductInfo.getId());
}
// 产品信息–>批量存入缓存
localeProductInfoCache.putAll(cacheProductInfoMap);
// 产品id—>批量存入缓存
productCodeToIdCache.putAll(cacheProductIdMap);
}
return productInfos;
}
}
spring InitializingBean与DisposableBean的使用
最新推荐文章于 2025-03-20 08:30:00 发布