如何做淘宝客网站推广有效获客的六大渠道
在实际使用过程中,可能会遇到这种情形:一个主体会有多个缓存,比如用户基础信息缓存、用户详情缓存,那么当删除用户信息后就需要同时失效多个缓存中该主体数据,那么jetcache支持这种应用场景么,答案是支持,只需要使用多个@CacheInvalidate注解即可,示例代码如下所示:
@Override@Cached(name = "user-cache", key = "#userId", expire = 10000, cacheType = CacheType.BOTH)public User loadUser(long userId) {User userInfo = new User();userInfo.setUserId(1);userInfo.setUserName("john");return userInfo;}@Cached(name = "user-cache2", key = "#userId", expire = 10000, cacheType = CacheType.BOTH)@Overridepublic UserInfo loadUser2(long userId) {UserInfo userInfo = new UserInfo();userInfo.setUserId(1);userInfo.setUserName("john");userInfo.setAddress("山东济宁");return userInfo;}// 同时失效多个缓存@CacheInvalidate(name = "user-cache", key = "#userId")@CacheInvalidate(name = "user-cache2", key = "#userId")@Overridepublic void delete(Long userId) {}
那么这种支持背后的代码是如何实现的呢,感兴趣的可以看下CacheHandler的
invokeWithInvalidateOrUpdate方法
private static Object invokeWithInvalidateOrUpdate(CacheInvokeContext context) throws Throwable {Object originResult = invokeOrigin(context);context.setResult(originResult);CacheInvokeConfig cic = context.getCacheInvokeConfig();// 注意下面是@CacheInvalidate的多个配置if (cic.getInvalidateAnnoConfigs() != null) {doInvalidate(context, cic.getInvalidateAnnoConfigs());}CacheUpdateAnnoConfig updateAnnoConfig = cic.getUpdateAnnoConfig();if (updateAnnoConfig != null) {doUpdate(context, updateAnnoConfig);}return originResult;}private static void doInvalidate(CacheInvokeContext context, List<CacheInvalidateAnnoConfig> annoConfig) {// 配置几个CacheInvalidate注解就会失效几个缓存,但是Update操作却不支持,大家可以想下为什么?for (CacheInvalidateAnnoConfig config : annoConfig) {doInvalidate(context, config);}}