Browse Source

优化缓存逻辑

zhouhao 7 years ago
parent
commit
0219095589

+ 4 - 26
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleAuthorizationSettingService.java

@@ -64,7 +64,6 @@ import static org.hswebframework.web.service.authorization.simple.CacheConstants
  * @author hsweb-generator-online
  */
 @Service("authorizationSettingService")
-@CacheConfig(cacheNames = USER_AUTH_CACHE_NAME)
 public class SimpleAuthorizationSettingService extends GenericEntityService<AuthorizationSettingEntity, String>
         implements AuthorizationSettingService, AuthenticationInitializeService, UserMenuManagerService {
 
@@ -84,7 +83,6 @@ public class SimpleAuthorizationSettingService extends GenericEntityService<Auth
 
     private DataAccessFactory dataAccessFactory;
 
-
     @Override
     protected IDGenerator<String> getIDGenerator() {
         return IDGenerator.MD5;
@@ -105,12 +103,7 @@ public class SimpleAuthorizationSettingService extends GenericEntityService<Auth
     }
 
     @Override
-    @Caching(
-            evict = {
-                    @CacheEvict(allEntries = true),
-                    @CacheEvict(cacheNames = USER_MENU_CACHE_NAME,allEntries = true)
-            }
-    )
+    @CacheEvict(cacheNames = {CacheConstants.USER_AUTH_CACHE_NAME,CacheConstants.USER_MENU_CACHE_NAME}, allEntries = true)
     public String saveOrUpdate(AuthorizationSettingEntity entity) {
         AuthorizationSettingEntity old = select(entity.getType(), entity.getSettingFor());
         if (old != null) {
@@ -121,12 +114,7 @@ public class SimpleAuthorizationSettingService extends GenericEntityService<Auth
     }
 
     @Override
-    @Caching(
-            evict = {
-                    @CacheEvict(allEntries = true),
-                    @CacheEvict(cacheNames = USER_MENU_CACHE_NAME,allEntries = true)
-            }
-    )
+    @CacheEvict(cacheNames = {CacheConstants.USER_AUTH_CACHE_NAME,CacheConstants.USER_MENU_CACHE_NAME}, allEntries = true)
     public String insert(AuthorizationSettingEntity entity) {
         tryValidateProperty(select(entity.getType(), entity.getSettingFor()) == null, AuthorizationSettingEntity.settingFor, "存在相同的配置!");
         entity.setStatus(STATUS_ENABLED);
@@ -151,12 +139,7 @@ public class SimpleAuthorizationSettingService extends GenericEntityService<Auth
     }
 
     @Override
-    @Caching(
-            evict = {
-                    @CacheEvict(allEntries = true),
-                    @CacheEvict(cacheNames = USER_MENU_CACHE_NAME,allEntries = true)
-            }
-    )
+    @CacheEvict(cacheNames = {CacheConstants.USER_AUTH_CACHE_NAME,CacheConstants.USER_MENU_CACHE_NAME}, allEntries = true)
     public int updateByPk(String id, AuthorizationSettingEntity entity) {
         int size = super.updateByPk(id, entity);
         if (entity.getMenus() != null) {
@@ -184,12 +167,7 @@ public class SimpleAuthorizationSettingService extends GenericEntityService<Auth
     }
 
     @Override
-    @Caching(
-            evict = {
-                    @CacheEvict(allEntries = true),
-                    @CacheEvict(cacheNames = USER_MENU_CACHE_NAME,allEntries = true)
-            }
-    )
+    @CacheEvict(cacheNames = {CacheConstants.USER_AUTH_CACHE_NAME,CacheConstants.USER_MENU_CACHE_NAME}, allEntries = true)
     public int deleteByPk(String id) {
         Objects.requireNonNull(id, "id can not be null");
         authorizationSettingMenuService.deleteBySettingId(id);

+ 25 - 23
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleMenuService.java

@@ -30,13 +30,13 @@ import org.springframework.cache.annotation.Cacheable;
 import org.springframework.cache.annotation.Caching;
 import org.springframework.stereotype.Service;
 
+import java.util.Collection;
 import java.util.List;
 
 /**
  * @author zhouhao
  */
 @Service("menuService")
-@CacheConfig(cacheNames = CacheConstants.MENU_CACHE_NAME)
 public class SimpleMenuService
         extends AbstractTreeSortService<MenuEntity, String>
         implements MenuService {
@@ -59,29 +59,36 @@ public class SimpleMenuService
     }
 
     @Override
-    @Caching(
-            evict = {
-                    @CacheEvict(allEntries = true),
-                    @CacheEvict(cacheNames = CacheConstants.USER_MENU_CACHE_NAME, allEntries = true)
-            }
-    )
+    @CacheEvict(cacheNames ={CacheConstants.MENU_CACHE_NAME,CacheConstants.USER_MENU_CACHE_NAME}, allEntries = true)
+    public int updateByPk(MenuEntity entity) {
+        return super.updateByPk(entity);
+    }
+
+    @Override
+    @CacheEvict(cacheNames ={CacheConstants.MENU_CACHE_NAME,CacheConstants.USER_MENU_CACHE_NAME}, allEntries = true)
+    public String saveOrUpdate(MenuEntity entity) {
+        return super.saveOrUpdate(entity);
+    }
+    @Override
+    @CacheEvict(cacheNames ={CacheConstants.MENU_CACHE_NAME,CacheConstants.USER_MENU_CACHE_NAME}, allEntries = true)
+    public int updateBatch(Collection<MenuEntity> data) {
+        return super.updateBatch(data);
+    }
+
+    @Override
+    @CacheEvict(cacheNames ={CacheConstants.MENU_CACHE_NAME,CacheConstants.USER_MENU_CACHE_NAME}, allEntries = true)
     public int updateByPk(String id, MenuEntity entity) {
         return super.updateByPk(id, entity);
     }
 
     @Override
-    @Caching(
-            evict = {
-                    @CacheEvict(allEntries = true),
-                    @CacheEvict(cacheNames = CacheConstants.USER_MENU_CACHE_NAME, allEntries = true)
-            }
-    )
+    @CacheEvict(cacheNames ={CacheConstants.MENU_CACHE_NAME,CacheConstants.USER_MENU_CACHE_NAME}, allEntries = true)
     public int updateByPk(List<MenuEntity> data) {
         return super.updateByPk(data);
     }
 
     @Override
-    @CacheEvict(allEntries = true)
+    @CacheEvict(cacheNames ={CacheConstants.MENU_CACHE_NAME,CacheConstants.USER_MENU_CACHE_NAME}, allEntries = true)
     public String insert(MenuEntity entity) {
         if (entity.getStatus() == null) {
             entity.setStatus((byte) 1);
@@ -90,30 +97,25 @@ public class SimpleMenuService
     }
 
     @Override
-    @Cacheable(key = "'ids:'+(#id==null?'0':#id.hashCode())")
+    @Cacheable(cacheNames =CacheConstants.MENU_CACHE_NAME,key = "'ids:'+(#id==null?'0':#id.hashCode())")
     public List<MenuEntity> selectByPk(List<String> id) {
         return super.selectByPk(id);
     }
 
     @Override
-    @Caching(
-            evict = {
-                    @CacheEvict(allEntries = true),
-                    @CacheEvict(cacheNames = CacheConstants.USER_MENU_CACHE_NAME, allEntries = true)
-            }
-    )
+    @CacheEvict(cacheNames ={CacheConstants.MENU_CACHE_NAME,CacheConstants.USER_MENU_CACHE_NAME}, allEntries = true)
     public int deleteByPk(String id) {
         return super.deleteByPk(id);
     }
 
     @Override
-    @Cacheable(key = "'permission-ids:'+(#permissionId==null?'0':#permissionId.hashCode())")
+    @Cacheable(cacheNames =CacheConstants.MENU_CACHE_NAME,key = "'permission-ids:'+(#permissionId==null?'0':#permissionId.hashCode())")
     public List<MenuEntity> getByPermissionId(List<String> permissionId) {
         return createQuery().noPaging().where().in("permissionId", permissionId).list();
     }
 
     @Override
-    @Cacheable(key = "'permission-id:'+#permissionId")
+    @Cacheable(cacheNames =CacheConstants.MENU_CACHE_NAME,key = "'permission-id:'+#permissionId")
     public MenuEntity getByPermissionId(String permissionId) {
         return createQuery().noPaging().where().is("permissionId", permissionId).single();
     }

+ 4 - 4
hsweb-system/hsweb-system-oauth2-server/hsweb-system-oauth2-server-starter/src/main/java/org/hswebframework/web/oauth2/OAuth2AuthorizationAutoConfiguration.java

@@ -8,12 +8,12 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
-@ConditionalOnClass(UserTokenParser.class)
-@Configuration
-@AutoConfigureAfter(OAuth2GranterAutoConfiguration.class)
+//@ConditionalOnClass(UserTokenParser.class)
+//@Configuration
+//@AutoConfigureAfter(OAuth2GranterAutoConfiguration.class)
 public class OAuth2AuthorizationAutoConfiguration {
 
-    @Bean
+//    @Bean
     public OAuth2UserTokenParser oAuth2UserTokenParser(AccessTokenService accessTokenService) {
         return new OAuth2UserTokenParser(accessTokenService);
     }