diff --git a/src/main/java/neatlogic/framework/dao/cache/NeatLogicConcurrentSafeCache.java b/src/main/java/neatlogic/framework/dao/cache/NeatLogicConcurrentSafeCache.java
index 60411433a3d81dace4a4214bf801a22afbe1837d..3784cca931030b9fb697198c6b8985b8c0957819 100644
--- a/src/main/java/neatlogic/framework/dao/cache/NeatLogicConcurrentSafeCache.java
+++ b/src/main/java/neatlogic/framework/dao/cache/NeatLogicConcurrentSafeCache.java
@@ -19,11 +19,10 @@ import neatlogic.framework.asynchronization.threadlocal.TenantContext;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
+import net.sf.ehcache.config.CacheConfiguration;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.cache.Cache;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
@@ -35,8 +34,6 @@ import java.util.concurrent.locks.ReentrantLock;
* 高并发场景下,防止缓存击穿
*/
public class NeatLogicConcurrentSafeCache implements Cache {
-
- private final static Logger logger = LoggerFactory.getLogger(NeatLogicConcurrentSafeCache.class);
/**
* The cache manager reference.
*/
@@ -76,12 +73,20 @@ public class NeatLogicConcurrentSafeCache implements Cache {
}
if (StringUtils.isNotBlank(tenant)) {
if (!CACHE_MANAGER.cacheExists(tenant + ":" + id)) {
- CACHE_MANAGER.addCache(tenant + ":" + id);
+ Ehcache ehcache = CACHE_MANAGER.addCacheIfAbsent(tenant + ":" + id);
+ CacheConfiguration cacheConfiguration = ehcache.getCacheConfiguration();
+ // 缓存5分钟
+ cacheConfiguration.setTimeToIdleSeconds(300);
+ cacheConfiguration.setTimeToLiveSeconds(300);
}
return CACHE_MANAGER.getEhcache(tenant + ":" + id);
} else {
if (!CACHE_MANAGER.cacheExists(id)) {
- CACHE_MANAGER.addCache(id);
+ Ehcache ehcache = CACHE_MANAGER.addCacheIfAbsent(id);
+ CacheConfiguration cacheConfiguration = ehcache.getCacheConfiguration();
+ // 缓存5分钟
+ cacheConfiguration.setTimeToIdleSeconds(300);
+ cacheConfiguration.setTimeToLiveSeconds(300);
}
return CACHE_MANAGER.getEhcache(id);
}
@@ -98,7 +103,7 @@ public class NeatLogicConcurrentSafeCache implements Cache {
if (CollectionUtils.isNotEmpty(keys)) {
for (Object key : keys) {
ReentrantLock lock = LOCAL_LOCK_MAP.remove(generateLockKey(getId(), key));
- if (lock != null) {
+ if (lock != null && lock.isLocked()) {
lock.unlock();
}
}
@@ -130,7 +135,9 @@ public class NeatLogicConcurrentSafeCache implements Cache {
}
cachedElement = getCache().get(key);
if (cachedElement != null) {
- lock.unlock();
+ if (lock.isLocked()) {
+ lock.unlock();
+ }
return cachedElement.getObjectValue();
}
return null;
diff --git a/src/main/java/neatlogic/framework/dao/mapper/LoginMapper.xml b/src/main/java/neatlogic/framework/dao/mapper/LoginMapper.xml
index 9bc2424f26344ca34115e432e9ad5662d0846960..7cdd84155f5cb7742f3edec06489b6b12163fd27 100644
--- a/src/main/java/neatlogic/framework/dao/mapper/LoginMapper.xml
+++ b/src/main/java/neatlogic/framework/dao/mapper/LoginMapper.xml
@@ -16,7 +16,7 @@ along with this program. If not, see .-->
-
+