Skip to content

Commit 5dfa960

Browse files
committed
新增也删除缓存,防止一直命中null。
1 parent 7485db4 commit 5dfa960

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
import org.hsweb.web.service.config.ConfigService;
88
import org.hsweb.web.service.impl.AbstractServiceImpl;
99
import org.hsweb.web.service.resource.ResourcesService;
10+
import org.springframework.cache.annotation.CacheEvict;
11+
import org.springframework.cache.annotation.CachePut;
1012
import org.springframework.cache.annotation.Cacheable;
13+
import org.springframework.cache.annotation.Caching;
1114
import org.springframework.stereotype.Service;
1215
import org.springframework.transaction.annotation.Transactional;
1316

1417
import javax.annotation.Resource;
18+
import javax.naming.OperationNotSupportedException;
1519

1620
/**
1721
* 资源服务类
@@ -32,6 +36,7 @@ protected ResourcesMapper getMapper() {
3236
return this.resourcesMapper;
3337
}
3438

39+
3540
@Override
3641
@Cacheable(value = CACHE_KEY, key = "'id.'+#id")
3742
@Transactional(readOnly = true)
@@ -48,24 +53,28 @@ public Resources selectByPk(String id) {
4853
*/
4954
@Cacheable(value = CACHE_KEY, key = "'md5.'+#md5")
5055
@Transactional(readOnly = true)
51-
public Resources selectByMd5(String md5) {
56+
public Resources selectByMd5(String md5) {
5257
return this.selectSingle(new QueryParam().where("md5", md5));
5358
}
5459

5560
@Override
5661
@Transactional(rollbackFor = Throwable.class)
62+
@Caching(evict = {
63+
@CacheEvict(value = CACHE_KEY, key = "'id.'+#data.id"),
64+
@CacheEvict(value = CACHE_KEY, key = "'md5.'+#data.md5")
65+
})
5766
public String insert(Resources data) {
58-
data.setId(this.newid(6));//6位随机id
67+
data.setId(this.newId(6));//6位随机id
5968
return super.insert(data);
6069
}
6170

62-
public String newid(int len) {
71+
public String newId(int len) {
6372
String id = RandomUtil.randomChar(len);
6473
for (int i = 0; i < 10; i++) {
6574
if (this.selectByPk(id) == null) {
6675
return id;
6776
}
6877
} //如果10次存在重复则位数+1
69-
return newid(len + 1);
78+
return newId(len + 1);
7079
}
7180
}

0 commit comments

Comments
 (0)