Преглед на файлове

新增也删除缓存,防止一直命中null。

zhouhao преди 8 години
родител
ревизия
5dfa96080a

+ 13 - 4
hsweb-web-service/hsweb-web-service-simple/src/main/java/org/hsweb/web/service/impl/resource/ResourcesServiceImpl.java

@@ -7,11 +7,15 @@ import org.hsweb.web.dao.resource.ResourcesMapper;
 import org.hsweb.web.service.config.ConfigService;
 import org.hsweb.web.service.impl.AbstractServiceImpl;
 import org.hsweb.web.service.resource.ResourcesService;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.CachePut;
 import org.springframework.cache.annotation.Cacheable;
+import org.springframework.cache.annotation.Caching;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
+import javax.naming.OperationNotSupportedException;
 
 /**
  * 资源服务类
@@ -32,6 +36,7 @@ public class ResourcesServiceImpl extends AbstractServiceImpl<Resources, String>
         return this.resourcesMapper;
     }
 
+
     @Override
     @Cacheable(value = CACHE_KEY, key = "'id.'+#id")
     @Transactional(readOnly = true)
@@ -48,24 +53,28 @@ public class ResourcesServiceImpl extends AbstractServiceImpl<Resources, String>
      */
     @Cacheable(value = CACHE_KEY, key = "'md5.'+#md5")
     @Transactional(readOnly = true)
-    public Resources selectByMd5(String md5)  {
+    public Resources selectByMd5(String md5) {
         return this.selectSingle(new QueryParam().where("md5", md5));
     }
 
     @Override
     @Transactional(rollbackFor = Throwable.class)
+    @Caching(evict = {
+            @CacheEvict(value = CACHE_KEY, key = "'id.'+#data.id"),
+            @CacheEvict(value = CACHE_KEY, key = "'md5.'+#data.md5")
+    })
     public String insert(Resources data) {
-        data.setId(this.newid(6));//6位随机id
+        data.setId(this.newId(6));//6位随机id
         return super.insert(data);
     }
 
-    public String newid(int len) {
+    public String newId(int len) {
         String id = RandomUtil.randomChar(len);
         for (int i = 0; i < 10; i++) {
             if (this.selectByPk(id) == null) {
                 return id;
             }
         }  //如果10次存在重复则位数+1
-        return newid(len + 1);
+        return newId(len + 1);
     }
 }