Ver Fonte

优化缓存

zhouhao há 7 anos atrás
pai
commit
5199678f59

+ 72 - 0
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/EnableCacheAllEvictGenericEntityService.java

@@ -0,0 +1,72 @@
+package org.hswebframework.web.service;
+
+import org.hswebframework.web.commons.entity.GenericEntity;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.Cacheable;
+import org.springframework.cache.annotation.Caching;
+
+import java.util.List;
+
+/**
+ * @author zhouhao
+ * @see org.springframework.cache.annotation.CacheConfig
+ * @see Cacheable
+ * @see CacheEvict
+ * @since 3.0
+ */
+public abstract class EnableCacheAllEvictGenericEntityService<E extends GenericEntity<PK>, PK> extends GenericEntityService<E, PK> {
+
+    @Override
+    @Cacheable(key = "'id:'+#pk")
+    public E selectByPk(PK pk) {
+        return super.selectByPk(pk);
+    }
+
+    @Override
+    @CacheEvict(allEntries = true)
+    public int updateByPk(List<E> data) {
+        return super.updateByPk(data);
+    }
+
+    @Override
+    @CacheEvict(allEntries = true)
+    public int updateByPk(PK pk, E entity) {
+        return super.updateByPk(pk, entity);
+    }
+
+    @Override
+    @CacheEvict(allEntries = true)
+    public PK insert(E entity) {
+        return super.insert(entity);
+    }
+
+    @Override
+    @CacheEvict(allEntries = true)
+    public int deleteByPk(PK pk) {
+        return super.deleteByPk(pk);
+    }
+
+    @Override
+    @CacheEvict(allEntries = true)
+    public PK saveOrUpdate(E entity) {
+        return super.saveOrUpdate(entity);
+    }
+
+    @Override
+    @Cacheable(key = "'all'")
+    public List<E> select() {
+        return super.select();
+    }
+
+    @Override
+    @Cacheable(key = "'id-in:'+#id.hashCode()")
+    public List<E> selectByPk(List<PK> id) {
+        return super.selectByPk(id);
+    }
+
+    @Override
+    @Cacheable(key = "'count'")
+    public int count() {
+        return super.count();
+    }
+}

+ 76 - 0
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/EnableCacheAllEvictTreeSortService.java

@@ -0,0 +1,76 @@
+package org.hswebframework.web.service;
+
+import org.hswebframework.web.commons.entity.TreeSortSupportEntity;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.Cacheable;
+
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * @author zhouhao
+ * @since 3.0
+ */
+public abstract class EnableCacheAllEvictTreeSortService<E extends TreeSortSupportEntity<PK>, PK>
+        extends AbstractTreeSortService<E, PK> {
+
+    @Override
+    @CacheEvict(allEntries = true)
+    public int updateBatch(Collection<E> data) {
+        return super.updateBatch(data);
+    }
+
+    @Override
+    @Cacheable(key = "'id:'+#pk")
+    public E selectByPk(PK pk) {
+        return super.selectByPk(pk);
+    }
+
+    @Override
+    @CacheEvict(allEntries = true)
+    public int updateByPk(List<E> data) {
+        return super.updateByPk(data);
+    }
+
+    @Override
+    @CacheEvict(allEntries = true)
+    public int updateByPk(PK pk, E entity) {
+        return super.updateByPk(pk, entity);
+    }
+
+    @Override
+    @CacheEvict(allEntries = true)
+    public PK insert(E entity) {
+        return super.insert(entity);
+    }
+
+    @Override
+    @CacheEvict(allEntries = true)
+    public int deleteByPk(PK pk) {
+        return super.deleteByPk(pk);
+    }
+
+    @Override
+    @CacheEvict(allEntries = true)
+    public PK saveOrUpdate(E entity) {
+        return super.saveOrUpdate(entity);
+    }
+
+    @Override
+    @Cacheable(key = "'id-in:'+#id.hashCode()")
+    public List<E> selectByPk(List<PK> id) {
+        return super.selectByPk(id);
+    }
+
+    @Override
+    @Cacheable(key = "'all'")
+    public List<E> select() {
+        return super.select();
+    }
+
+    @Override
+    @Cacheable(key = "'count'")
+    public int count() {
+        return super.count();
+    }
+}

+ 0 - 12
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/EnableCacheGenericEntityService.java

@@ -43,18 +43,6 @@ public abstract class EnableCacheGenericEntityService<E extends GenericEntity<PK
         return super.updateByPk(pk, entity);
     }
 
-    @Override
-    @Caching(
-            evict = {
-                    @CacheEvict(key = "'id:'+#entity.id"),
-                    @CacheEvict(key = "'all'"),
-                    @CacheEvict(key = "'count'")
-            }
-    )
-    protected int updateByPk(E entity) {
-        return super.updateByPk(entity);
-    }
-
     @Override
     @Caching(
             evict = {

+ 95 - 0
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/EnableCacheTreeSortService.java

@@ -0,0 +1,95 @@
+package org.hswebframework.web.service;
+
+import org.hswebframework.web.commons.entity.TreeSortSupportEntity;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.Cacheable;
+import org.springframework.cache.annotation.Caching;
+
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * @author zhouhao
+ * @since 3.0
+ */
+public abstract class EnableCacheTreeSortService<E extends TreeSortSupportEntity<PK>, PK>
+        extends AbstractTreeSortService<E, PK> {
+
+    @Override
+    @CacheEvict(allEntries = true)
+    public int updateBatch(Collection<E> data) {
+        return super.updateBatch(data);
+    }
+
+    @Override
+    @Cacheable(key = "'id:'+#pk")
+    public E selectByPk(PK pk) {
+        return super.selectByPk(pk);
+    }
+
+    @Override
+    @CacheEvict(allEntries = true)
+    public int updateByPk(List<E> data) {
+        return super.updateByPk(data);
+    }
+
+    @Override
+    @Caching(
+            evict = {
+                    @CacheEvict(key = "'id:'+#pk"),
+                    @CacheEvict(key = "'all'"),
+                    @CacheEvict(key = "'count'")
+            }
+    )
+    public int updateByPk(PK pk, E entity) {
+        return super.updateByPk(pk, entity);
+    }
+
+    @Override
+    @Caching(
+            evict = {
+                    @CacheEvict(key = "'id:'+#result"),
+                    @CacheEvict(key = "'all'"),
+                    @CacheEvict(key = "'count'")
+            }
+    )
+    public PK insert(E entity) {
+        return super.insert(entity);
+    }
+
+    @Override
+    @Caching(
+            evict = {
+                    @CacheEvict(key = "'id:'+#pk"),
+                    @CacheEvict(key = "'all'"),
+                    @CacheEvict(key = "'count'")
+            }
+    )
+    public int deleteByPk(PK pk) {
+        return super.deleteByPk(pk);
+    }
+
+    @Override
+    @Caching(
+            evict = {
+                    @CacheEvict(key = "'id:'+#result"),
+                    @CacheEvict(key = "'all'"),
+                    @CacheEvict(key = "'count'")
+            }
+    )
+    public PK saveOrUpdate(E entity) {
+        return super.saveOrUpdate(entity);
+    }
+
+    @Override
+    @Cacheable(key = "'all'")
+    public List<E> select() {
+        return super.select();
+    }
+
+    @Override
+    @Cacheable(key = "'count'")
+    public int count() {
+        return super.count();
+    }
+}