Kaynağa Gözat

优化缓存策略

zhouhao 7 yıl önce
ebeveyn
işleme
4b38045889
7 değiştirilmiş dosya ile 80 ekleme ve 5 silme
  1. 9 0
      hsweb-system/hsweb-system-organizational/hsweb-system-organizational-service/hsweb-system-organizational-service-api/src/main/java/org/hswebframework/web/service/organizational/event/ClearPersonCacheEvent.java
  2. 13 0
      hsweb-system/hsweb-system-organizational/hsweb-system-organizational-service/hsweb-system-organizational-service-simple/src/main/java/org/hswebframework/web/service/organizational/simple/SimpleDepartmentService.java
  3. 9 0
      hsweb-system/hsweb-system-organizational/hsweb-system-organizational-service/hsweb-system-organizational-service-simple/src/main/java/org/hswebframework/web/service/organizational/simple/SimpleDistrictService.java
  4. 18 1
      hsweb-system/hsweb-system-organizational/hsweb-system-organizational-service/hsweb-system-organizational-service-simple/src/main/java/org/hswebframework/web/service/organizational/simple/SimpleOrganizationalService.java
  5. 8 2
      hsweb-system/hsweb-system-organizational/hsweb-system-organizational-service/hsweb-system-organizational-service-simple/src/main/java/org/hswebframework/web/service/organizational/simple/SimplePersonService.java
  6. 15 0
      hsweb-system/hsweb-system-organizational/hsweb-system-organizational-service/hsweb-system-organizational-service-simple/src/main/java/org/hswebframework/web/service/organizational/simple/SimplePositionService.java
  7. 8 2
      hsweb-system/hsweb-system-organizational/hsweb-system-organizational-service/hsweb-system-organizational-service-simple/src/main/java/org/hswebframework/web/service/organizational/simple/SimpleRelationDefineService.java

+ 9 - 0
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-service/hsweb-system-organizational-service-api/src/main/java/org/hswebframework/web/service/organizational/event/ClearPersonCacheEvent.java

@@ -0,0 +1,9 @@
+package org.hswebframework.web.service.organizational.event;
+
+/**
+ * @author zhouhao
+ * @since 1.0
+ */
+public class ClearPersonCacheEvent {
+
+}

+ 13 - 0
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-service/hsweb-system-organizational-service-simple/src/main/java/org/hswebframework/web/service/organizational/simple/SimpleDepartmentService.java

@@ -27,10 +27,12 @@ import org.hswebframework.web.service.DefaultDSLQueryService;
 import org.hswebframework.web.service.EnableCacheAllEvictTreeSortService;
 import org.hswebframework.web.service.GenericEntityService;
 import org.hswebframework.web.service.organizational.DepartmentService;
+import org.hswebframework.web.service.organizational.event.ClearPersonCacheEvent;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.annotation.CacheConfig;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
+import org.springframework.context.ApplicationEventPublisher;
 import org.springframework.stereotype.Service;
 
 import java.util.Collection;
@@ -52,6 +54,9 @@ public class SimpleDepartmentService
     @Autowired
     protected PositionDao positionDao;
 
+    @Autowired
+    private ApplicationEventPublisher publisher;
+
     @Override
     public DepartmentDao getDao() {
         return departmentDao;
@@ -87,6 +92,14 @@ public class SimpleDepartmentService
                 .total() > 0) {
             throw new BusinessException("部门下存在职位信息,无法删除!");
         }
+        publisher.publishEvent(new ClearPersonCacheEvent());
         return super.deleteByPk(id);
     }
+
+    @Override
+    public int updateByPk(String id, DepartmentEntity entity) {
+        publisher.publishEvent(new ClearPersonCacheEvent());
+        return super.updateByPk(id, entity);
+    }
+
 }

+ 9 - 0
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-service/hsweb-system-organizational-service-simple/src/main/java/org/hswebframework/web/service/organizational/simple/SimpleDistrictService.java

@@ -12,11 +12,13 @@ import org.hswebframework.web.service.DefaultDSLQueryService;
 import org.hswebframework.web.service.GenericEntityService;
 import org.hswebframework.web.id.IDGenerator;
 import org.hswebframework.web.service.organizational.DistrictService;
+import org.hswebframework.web.service.organizational.event.ClearPersonCacheEvent;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.annotation.CacheConfig;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.cache.annotation.Caching;
+import org.springframework.context.ApplicationEventPublisher;
 import org.springframework.stereotype.Service;
 
 import java.util.Collection;
@@ -40,6 +42,9 @@ public class SimpleDistrictService extends AbstractTreeSortService<DistrictEntit
     @Autowired
     private OrganizationalDao organizationalDao;
 
+    @Autowired
+    private ApplicationEventPublisher publisher;
+
     @Override
     protected IDGenerator<String> getIDGenerator() {
         return IDGenerator.MD5;
@@ -59,6 +64,7 @@ public class SimpleDistrictService extends AbstractTreeSortService<DistrictEntit
     @Override
     @CacheEvict(allEntries = true)
     public int updateByPk(String id, DistrictEntity entity) {
+        publisher.publishEvent(new ClearPersonCacheEvent());
         return super.updateByPk(id, entity);
     }
 
@@ -82,6 +88,7 @@ public class SimpleDistrictService extends AbstractTreeSortService<DistrictEntit
                 .total() > 0) {
             throw new BusinessException("行政区域下存在机构信息,无法删除!");
         }
+        publisher.publishEvent(new ClearPersonCacheEvent());
         return super.deleteByPk(id);
     }
 
@@ -117,6 +124,7 @@ public class SimpleDistrictService extends AbstractTreeSortService<DistrictEntit
                 .set(DistrictEntity.status, DataStatus.STATUS_DISABLED)
                 .where(DistrictEntity.id, id)
                 .exec();
+        publisher.publishEvent(new ClearPersonCacheEvent());
     }
 
     @Override
@@ -127,5 +135,6 @@ public class SimpleDistrictService extends AbstractTreeSortService<DistrictEntit
                 .set(DistrictEntity.status, DataStatus.STATUS_ENABLED)
                 .where(DistrictEntity.id, id)
                 .exec();
+        publisher.publishEvent(new ClearPersonCacheEvent());
     }
 }

+ 18 - 1
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-service/hsweb-system-organizational-service-simple/src/main/java/org/hswebframework/web/service/organizational/simple/SimpleOrganizationalService.java

@@ -26,10 +26,13 @@ import org.hswebframework.web.id.IDGenerator;
 import org.hswebframework.web.service.DefaultDSLQueryService;
 import org.hswebframework.web.service.EnableCacheAllEvictTreeSortService;
 import org.hswebframework.web.service.organizational.OrganizationalService;
+import org.hswebframework.web.service.organizational.event.ClearPersonCacheEvent;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.annotation.CacheConfig;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
+import org.springframework.cache.annotation.Caching;
+import org.springframework.context.ApplicationEventPublisher;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.StringUtils;
@@ -51,6 +54,9 @@ public class SimpleOrganizationalService extends EnableCacheAllEvictTreeSortServ
     @Autowired
     private DepartmentDao departmentDao;
 
+    @Autowired
+    private ApplicationEventPublisher publisher;
+
     @Override
     public OrganizationalDao getDao() {
         return organizationalDao;
@@ -62,13 +68,14 @@ public class SimpleOrganizationalService extends EnableCacheAllEvictTreeSortServ
     }
 
     @Override
+    @CacheEvict(allEntries = true)
     public int deleteByPk(String id) {
         if (DefaultDSLQueryService.createQuery(departmentDao)
                 .where(DepartmentEntity.orgId, id)
                 .total() > 0) {
             throw new BusinessException("机构下存在部门信息,无法删除");
         }
-
+        publisher.publishEvent(new ClearPersonCacheEvent());
         return super.deleteByPk(id);
     }
 
@@ -76,9 +83,16 @@ public class SimpleOrganizationalService extends EnableCacheAllEvictTreeSortServ
     @CacheEvict(allEntries = true)
     public String insert(OrganizationalEntity entity) {
         entity.setStatus(DataStatus.STATUS_ENABLED);
+        publisher.publishEvent(new ClearPersonCacheEvent());
         return super.insert(entity);
     }
 
+    @Override
+    public int updateByPk(String id, OrganizationalEntity entity) {
+        publisher.publishEvent(new ClearPersonCacheEvent());
+        return super.updateByPk(id, entity);
+    }
+
     @Override
     @CacheEvict(allEntries = true)
     public void disable(String id) {
@@ -87,6 +101,7 @@ public class SimpleOrganizationalService extends EnableCacheAllEvictTreeSortServ
                 .set(OrganizationalEntity.status, DataStatus.STATUS_DISABLED)
                 .where(OrganizationalEntity.id, id)
                 .exec();
+        publisher.publishEvent(new ClearPersonCacheEvent());
     }
 
     @Override
@@ -97,6 +112,7 @@ public class SimpleOrganizationalService extends EnableCacheAllEvictTreeSortServ
                 .set(OrganizationalEntity.status, DataStatus.STATUS_ENABLED)
                 .where(OrganizationalEntity.id, id)
                 .exec();
+        publisher.publishEvent(new ClearPersonCacheEvent());
     }
 
     @Override
@@ -116,6 +132,7 @@ public class SimpleOrganizationalService extends EnableCacheAllEvictTreeSortServ
         if (StringUtils.isEmpty(name)) {
             return null;
         }
+
         return createQuery().where(OrganizationalEntity.name, name).single();
     }
 }

+ 8 - 2
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-service/hsweb-system-organizational-service-simple/src/main/java/org/hswebframework/web/service/organizational/simple/SimplePersonService.java

@@ -21,7 +21,6 @@ import org.hswebframework.web.commons.entity.TreeSupportEntity;
 import org.hswebframework.web.dao.dynamic.QueryByEntityDao;
 import org.hswebframework.web.dao.organizational.*;
 import org.hswebframework.web.entity.authorization.UserEntity;
-import org.hswebframework.web.entity.authorization.bind.BindRoleUserEntity;
 import org.hswebframework.web.entity.organizational.*;
 import org.hswebframework.web.id.IDGenerator;
 import org.hswebframework.web.organizational.authorization.*;
@@ -31,15 +30,16 @@ import org.hswebframework.web.organizational.authorization.relation.SimpleRelati
 import org.hswebframework.web.organizational.authorization.simple.*;
 import org.hswebframework.web.service.DefaultDSLQueryService;
 import org.hswebframework.web.service.GenericEntityService;
-import org.hswebframework.web.service.authorization.AuthorizationSettingTypeSupplier;
 import org.hswebframework.web.service.authorization.UserService;
 import org.hswebframework.web.service.organizational.*;
+import org.hswebframework.web.service.organizational.event.ClearPersonCacheEvent;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.annotation.CacheConfig;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.cache.annotation.Caching;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.event.TransactionalEventListener;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
 
@@ -136,6 +136,12 @@ public class SimplePersonService extends GenericEntityService<PersonEntity, Stri
         return this.updateByPk(((PersonEntity) authBindEntity));
     }
 
+    @TransactionalEventListener
+    @CacheEvict(allEntries = true)
+    public void handleClearCache(ClearPersonCacheEvent event) {
+        logger.debug("clear all user cache");
+    }
+
     @Override
     @Cacheable(key = "'person-name'+#name")
     public List<PersonEntity> selectByName(String name) {

+ 15 - 0
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-service/hsweb-system-organizational-service-simple/src/main/java/org/hswebframework/web/service/organizational/simple/SimplePositionService.java

@@ -23,7 +23,9 @@ import org.hswebframework.web.entity.organizational.PositionEntity;
 import org.hswebframework.web.id.IDGenerator;
 import org.hswebframework.web.service.AbstractTreeSortService;
 import org.hswebframework.web.service.organizational.PositionService;
+import org.hswebframework.web.service.organizational.event.ClearPersonCacheEvent;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationEventPublisher;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
@@ -35,6 +37,10 @@ import org.springframework.util.CollectionUtils;
 @Service("positionService")
 public class SimplePositionService extends AbstractTreeSortService<PositionEntity, String>
         implements PositionService {
+
+    @Autowired
+    private ApplicationEventPublisher publisher;
+
     @Autowired
     private PositionDao positionDao;
 
@@ -56,6 +62,15 @@ public class SimplePositionService extends AbstractTreeSortService<PositionEntit
         if(!CollectionUtils.isEmpty(personDao.selectByPositionId(id))){
             throw new BusinessException("岗位中还有人员,无法删除!");
         }
+        publisher.publishEvent(new ClearPersonCacheEvent());
         return super.deleteByPk(id);
     }
+
+    @Override
+    public int updateByPk(String id, PositionEntity entity) {
+        publisher.publishEvent(new ClearPersonCacheEvent());
+        return super.updateByPk(id, entity);
+    }
+
+    
 }

+ 8 - 2
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-service/hsweb-system-organizational-service-simple/src/main/java/org/hswebframework/web/service/organizational/simple/SimpleRelationDefineService.java

@@ -2,10 +2,13 @@ package org.hswebframework.web.service.organizational.simple;
 
 import org.hswebframework.web.dao.organizational.RelationDefineDao;
 import org.hswebframework.web.entity.organizational.RelationDefineEntity;
+import org.hswebframework.web.service.EnableCacheGenericEntityService;
 import org.hswebframework.web.service.GenericEntityService;
 import org.hswebframework.web.id.IDGenerator;
 import org.hswebframework.web.service.organizational.RelationDefineService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.annotation.CacheConfig;
+import org.springframework.cache.annotation.EnableCaching;
 import org.springframework.stereotype.Service;
 
 /**
@@ -14,14 +17,17 @@ import org.springframework.stereotype.Service;
  * @author hsweb-generator-online
  */
 @Service("relationDefineService")
-public class SimpleRelationDefineService extends GenericEntityService<RelationDefineEntity, String>
+@CacheConfig(cacheNames = "hsweb-relation-define")
+public class SimpleRelationDefineService extends EnableCacheGenericEntityService<RelationDefineEntity, String>
         implements RelationDefineService {
     @Autowired
     private RelationDefineDao relationDefineDao;
-   @Override
+
+    @Override
     protected IDGenerator<String> getIDGenerator() {
         return IDGenerator.MD5;
     }
+
     @Override
     public RelationDefineDao getDao() {
         return relationDefineDao;