forked from zfile-dev/zfile
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e61d7d
commit 399e961
Showing
15 changed files
with
329 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/im/zhaojun/common/aop/FileListCacheAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package im.zhaojun.common.aop; | ||
|
||
import im.zhaojun.common.cache.ZFileCache; | ||
import im.zhaojun.common.model.dto.FileItemDTO; | ||
import im.zhaojun.common.service.SystemConfigService; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
import javax.annotation.Resource; | ||
import java.util.List; | ||
|
||
/** | ||
* 操作日志切面. | ||
*/ | ||
@Aspect | ||
@Component | ||
public class FileListCacheAspect { | ||
|
||
@Resource | ||
private ZFileCache zFileCache; | ||
|
||
@Resource | ||
private SystemConfigService systemConfigService; | ||
|
||
@Pointcut("execution(public * im.zhaojun.common.service.AbstractFileService.fileList(..))") | ||
public void pointcut() { | ||
} | ||
|
||
@Around(value = "pointcut()") | ||
public Object around(ProceedingJoinPoint point) throws Throwable { | ||
List<FileItemDTO> result; | ||
|
||
Object[] args = point.getArgs(); | ||
String path = String.valueOf(args[0]); | ||
|
||
boolean enableCache = systemConfigService.getEnableCache(); | ||
|
||
if (enableCache) { | ||
List<FileItemDTO> cacheFileList = zFileCache.get(path); | ||
if (CollectionUtils.isEmpty(cacheFileList)) { | ||
result = (List<FileItemDTO>) point.proceed(); | ||
zFileCache.put(path, result); | ||
} else { | ||
result = cacheFileList; | ||
} | ||
} else { | ||
result = (List<FileItemDTO>) point.proceed(); | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package im.zhaojun.common.cache; | ||
|
||
import cn.hutool.core.util.StrUtil; | ||
import im.zhaojun.common.model.dto.FileItemDTO; | ||
import im.zhaojun.common.model.dto.SystemConfigDTO; | ||
import im.zhaojun.common.model.enums.FileTypeEnum; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
/** | ||
* @author zhaojun | ||
*/ | ||
@Component | ||
public class ZFileCache { | ||
|
||
private ConcurrentMap<String, List<FileItemDTO>> fileCache = new ConcurrentHashMap<>(); | ||
|
||
private ConcurrentMap<String, Integer> fileCountCache = new ConcurrentHashMap<>(); | ||
|
||
private SystemConfigDTO systemConfigCache; | ||
|
||
public static final String CACHE_FILE_COUNT_KEY = "file-count"; | ||
|
||
public static final String CACHE_DIRECTORY_COUNT_KEY = "directory-count"; | ||
|
||
public synchronized void put(String key, List<FileItemDTO> value) { | ||
for (FileItemDTO fileItemDTO : value) { | ||
if (FileTypeEnum.FILE.equals(fileItemDTO.getType())) { | ||
incrCacheFileCount(); | ||
} else { | ||
incrCacheDirectoryCount(); | ||
} | ||
} | ||
fileCache.put(key, value); | ||
} | ||
|
||
public List<FileItemDTO> get(String key) { | ||
return fileCache.get(key); | ||
} | ||
|
||
public void clear() { | ||
fileCache.clear(); | ||
fileCountCache.clear(); | ||
} | ||
|
||
public long cacheCount() { | ||
return fileCache.size(); | ||
} | ||
|
||
public List<FileItemDTO> find(String key, boolean ignoreCase) { | ||
List<FileItemDTO> result = new ArrayList<>(); | ||
|
||
Collection<List<FileItemDTO>> values = fileCache.values(); | ||
for (List<FileItemDTO> fileItemList : values) { | ||
for (FileItemDTO fileItemDTO : fileItemList) { | ||
boolean testResult; | ||
|
||
if (ignoreCase) { | ||
testResult = StrUtil.containsIgnoreCase(fileItemDTO.getName(), key); | ||
} else { | ||
testResult = fileItemDTO.getName().contains(key); | ||
} | ||
|
||
if (testResult) { | ||
result.add(fileItemDTO); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public Set<String> keySet() { | ||
return fileCache.keySet(); | ||
} | ||
|
||
public void remove(String key) { | ||
fileCache.remove(key); | ||
} | ||
|
||
private void incrCacheFileCount() { | ||
Integer originValue = fileCountCache.getOrDefault(CACHE_FILE_COUNT_KEY, 0); | ||
fileCountCache.put(CACHE_FILE_COUNT_KEY, originValue + 1); | ||
} | ||
|
||
private void incrCacheDirectoryCount() { | ||
Integer originValue = fileCountCache.getOrDefault(CACHE_DIRECTORY_COUNT_KEY, 0); | ||
fileCountCache.put(CACHE_DIRECTORY_COUNT_KEY, originValue + 1); | ||
} | ||
|
||
public int getCacheFileCount() { | ||
return fileCountCache.getOrDefault(CACHE_FILE_COUNT_KEY, 0); | ||
} | ||
|
||
public int getCacheDirectorCount() { | ||
return fileCountCache.getOrDefault(CACHE_DIRECTORY_COUNT_KEY, 0); | ||
} | ||
|
||
public void updateConfig(SystemConfigDTO systemConfigCache) { | ||
this.systemConfigCache = systemConfigCache; | ||
} | ||
|
||
public SystemConfigDTO getConfig() { | ||
return this.systemConfigCache; | ||
} | ||
|
||
public void removeConfig() { | ||
this.systemConfigCache = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package im.zhaojun.common.config; | ||
|
||
import im.zhaojun.common.model.dto.FileItemDTO; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
/** | ||
* @author zhaojun | ||
*/ | ||
@Configuration | ||
public class CacheConfig { | ||
|
||
@Bean | ||
public ConcurrentMap<String, List<FileItemDTO>> concurrentMapCache() { | ||
return new ConcurrentHashMap<>(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/im/zhaojun/common/config/ContentTypeTextToTextJson.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package im.zhaojun.common.config; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpRequest; | ||
import org.springframework.http.client.ClientHttpRequestExecution; | ||
import org.springframework.http.client.ClientHttpRequestInterceptor; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.util.Collections; | ||
|
||
public class ContentTypeTextToTextJson implements ClientHttpRequestInterceptor { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(ContentTypeTextToTextJson.class); | ||
|
||
@Override | ||
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) | ||
throws IOException { | ||
URI uri = request.getURI(); | ||
ClientHttpResponse response = execution.execute(request, body); | ||
HttpHeaders headers = response.getHeaders(); | ||
headers.put("Content-Type", Collections.singletonList("application/text")); | ||
return response; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.