asp化妆品网站,邢台提供网站建设公司电话,快代理ip,比特币做空网站在实际使用过程中#xff0c;可能会遇到这种情形#xff1a;一个主体会有多个缓存#xff0c;比如用户基础信息缓存、用户详情缓存#xff0c;那么当删除用户信息后就需要同时失效多个缓存中该主体数据#xff0c;那么jetcache支持这种应用场景么#xff0c;答案是支持可能会遇到这种情形一个主体会有多个缓存比如用户基础信息缓存、用户详情缓存那么当删除用户信息后就需要同时失效多个缓存中该主体数据那么jetcache支持这种应用场景么答案是支持只需要使用多个CacheInvalidate注解即可示例代码如下所示 OverrideCached(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, ListCacheInvalidateAnnoConfig annoConfig) {// 配置几个CacheInvalidate注解就会失效几个缓存但是Update操作却不支持大家可以想下为什么for (CacheInvalidateAnnoConfig config : annoConfig) {doInvalidate(context, config);}}