|
@@ -1,18 +1,30 @@
|
|
package com.project.common.core.redis;
|
|
package com.project.common.core.redis;
|
|
|
|
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
import com.github.benmanes.caffeine.cache.Cache;
|
|
import com.github.benmanes.caffeine.cache.Cache;
|
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
|
|
|
+import com.project.common.core.domain.entity.LargeCache;
|
|
|
|
+import com.project.common.mapper.cache.PlatCacheMapper;
|
|
|
|
+import com.project.common.utils.StringUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
import javax.annotation.PostConstruct;
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
+import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
import java.util.Collection;
|
|
|
|
+import java.util.List;
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@Component
|
|
@Component
|
|
public class MyCache {
|
|
public class MyCache {
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private PlatCacheMapper platCacheMapper;
|
|
|
|
+
|
|
// 使用ConcurrentHashMap作为数据的存储
|
|
// 使用ConcurrentHashMap作为数据的存储
|
|
// private ConcurrentHashMap<String, Object> storage = new ConcurrentHashMap<>();
|
|
// private ConcurrentHashMap<String, Object> storage = new ConcurrentHashMap<>();
|
|
|
|
|
|
@@ -54,10 +66,27 @@ public class MyCache {
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public boolean hasKey(String key){
|
|
public boolean hasKey(String key){
|
|
- if (caffeineCache.getIfPresent(key) != null){
|
|
|
|
- return true;
|
|
|
|
|
|
+ if (StringUtils.isBlank(key)){
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ LargeCache cache = platCacheMapper.selectCache(key);
|
|
|
|
+ if (cache == null || StringUtils.isBlank(cache.getKey())){
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
- return false;
|
|
|
|
|
|
+ LocalDateTime createTimePlus30 = cache.getCreateTime().toInstant()
|
|
|
|
+ .atZone(java.time.ZoneId.systemDefault())
|
|
|
|
+ .toLocalDateTime()
|
|
|
|
+ .plusMinutes(cache.getExpirationDate());
|
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
|
+ if (createTimePlus30.isBefore(now)){
|
|
|
|
+ platCacheMapper.deleteCache(key);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+// if (caffeineCache.getIfPresent(key) != null){
|
|
|
|
+// return true;
|
|
|
|
+// }
|
|
|
|
+// return false;
|
|
}
|
|
}
|
|
|
|
|
|
// @Override
|
|
// @Override
|
|
@@ -70,13 +99,30 @@ public class MyCache {
|
|
// }
|
|
// }
|
|
|
|
|
|
/**
|
|
/**
|
|
- * get方法, 获取缓存数据,无需反序列化
|
|
|
|
|
|
+ * get方法, 获取缓存数据
|
|
* @param key
|
|
* @param key
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public Object get(Object key) {
|
|
public Object get(Object key) {
|
|
- String k = key.toString();
|
|
|
|
- return caffeineCache.getIfPresent(k);
|
|
|
|
|
|
+// String k = key.toString();
|
|
|
|
+// return caffeineCache.getIfPresent(k);
|
|
|
|
+ if (Objects.isNull(key)){
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ LargeCache cache = platCacheMapper.selectCache(key.toString());
|
|
|
|
+ if (cache == null || StringUtils.isBlank(cache.getKey())){
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ LocalDateTime createTimePlus30 = cache.getCreateTime().toInstant()
|
|
|
|
+ .atZone(java.time.ZoneId.systemDefault())
|
|
|
|
+ .toLocalDateTime()
|
|
|
|
+ .plusMinutes(cache.getExpirationDate());
|
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
|
+ if (createTimePlus30.isBefore(now)){
|
|
|
|
+ platCacheMapper.deleteCache(key.toString());
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ return cache.getData();
|
|
}
|
|
}
|
|
|
|
|
|
// @Override
|
|
// @Override
|
|
@@ -105,8 +151,17 @@ public class MyCache {
|
|
* @param value
|
|
* @param value
|
|
*/
|
|
*/
|
|
public void put(Object key, Object value) {
|
|
public void put(Object key, Object value) {
|
|
- if (Objects.nonNull(value)) {
|
|
|
|
- caffeineCache.put(key.toString(), value);
|
|
|
|
|
|
+// if (Objects.nonNull(value)) {
|
|
|
|
+// caffeineCache.put(key.toString(), value);
|
|
|
|
+// }
|
|
|
|
+ if (Objects.isNull(key) || Objects.isNull(value)){
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if (value instanceof String){
|
|
|
|
+ platCacheMapper.insertCache(key.toString(), value.toString());
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ platCacheMapper.insertCache(key.toString(), JSON.toJSONString(value));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -121,7 +176,11 @@ public class MyCache {
|
|
* @param key
|
|
* @param key
|
|
*/
|
|
*/
|
|
public void evict(Object key) {
|
|
public void evict(Object key) {
|
|
- caffeineCache.invalidate(key.toString());
|
|
|
|
|
|
+// caffeineCache.invalidate(key.toString());
|
|
|
|
+ if (Objects.isNull(key)){
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ platCacheMapper.deleteCache(key.toString());
|
|
}
|
|
}
|
|
|
|
|
|
// 删除集合
|
|
// 删除集合
|
|
@@ -138,9 +197,15 @@ public class MyCache {
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public boolean deleteObject(final Collection collection){
|
|
public boolean deleteObject(final Collection collection){
|
|
|
|
+// collection.forEach(o -> {
|
|
|
|
+// caffeineCache.invalidate(o.toString());
|
|
|
|
+// } );
|
|
|
|
+// return true;
|
|
|
|
+ List<String> list = new ArrayList<>();
|
|
collection.forEach(o -> {
|
|
collection.forEach(o -> {
|
|
- caffeineCache.invalidate(o.toString());
|
|
|
|
- } );
|
|
|
|
|
|
+ list.add(o.toString());
|
|
|
|
+ });
|
|
|
|
+ platCacheMapper.deleteCaches(list);
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -155,7 +220,8 @@ public class MyCache {
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public Collection<String> keys(final String pattern){
|
|
public Collection<String> keys(final String pattern){
|
|
- return caffeineCache.asMap().keySet();
|
|
|
|
|
|
+// return caffeineCache.asMap().keySet();
|
|
|
|
+ return platCacheMapper.selectKeys();
|
|
}
|
|
}
|
|
|
|
|
|
// @Override
|
|
// @Override
|
|
@@ -167,6 +233,7 @@ public class MyCache {
|
|
* 清除所有缓存
|
|
* 清除所有缓存
|
|
*/
|
|
*/
|
|
public void clear() {
|
|
public void clear() {
|
|
- caffeineCache.invalidateAll();
|
|
|
|
|
|
+// caffeineCache.invalidateAll();
|
|
|
|
+ platCacheMapper.clearAll();
|
|
}
|
|
}
|
|
}
|
|
}
|