Skip to content

Commit

Permalink
🏗️ 缓存架构调整, 改为自实现缓存, 移除第三方依赖.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaojun1998 committed Feb 26, 2020
1 parent 3e61d7d commit 399e961
Show file tree
Hide file tree
Showing 15 changed files with 329 additions and 205 deletions.
24 changes: 7 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -106,23 +110,9 @@
</dependency>

<dependency>
<groupId>com.alicp.jetcache</groupId>
<artifactId>jetcache-starter-redis</artifactId>
<version>2.5.14</version>
<exclusions>
<exclusion>
<groupId>com.alicp.jetcache</groupId>
<artifactId>jetcache-redis</artifactId>
</exclusion>
<exclusion>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</exclusion>
<exclusion>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
</exclusion>
</exclusions>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.61</version>
</dependency>

</dependencies>
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/im/zhaojun/ZfileApplication.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package im.zhaojun;

import com.alicp.jetcache.anno.config.EnableCreateCacheAnnotation;
import com.alicp.jetcache.anno.config.EnableMethodCache;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
Expand All @@ -12,8 +10,6 @@
*/
@EnableAsync
@SpringBootApplication
@EnableMethodCache(basePackages = "im.zhaojun", proxyTargetClass = true)
@EnableCreateCacheAnnotation
@EnableAspectJAutoProxy(exposeProxy = true)
public class ZfileApplication {

Expand Down
55 changes: 55 additions & 0 deletions src/main/java/im/zhaojun/common/aop/FileListCacheAspect.java
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;
}
}
115 changes: 115 additions & 0 deletions src/main/java/im/zhaojun/common/cache/ZFileCache.java
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;
}
}
21 changes: 21 additions & 0 deletions src/main/java/im/zhaojun/common/config/CacheConfig.java
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<>();
}
}
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;
}

}
17 changes: 9 additions & 8 deletions src/main/java/im/zhaojun/common/config/GlobalScheduleTask.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package im.zhaojun.common.config;

import im.zhaojun.common.cache.ZFileCache;
import im.zhaojun.common.model.enums.StorageTypeEnum;
import im.zhaojun.common.service.AbstractFileService;
import im.zhaojun.common.service.StorageConfigService;
Expand All @@ -23,6 +24,9 @@
@Slf4j
public class GlobalScheduleTask {

@Resource
private ZFileCache zFileCache;

@Resource
private StorageConfigService storageConfigService;

Expand All @@ -38,7 +42,7 @@ public class GlobalScheduleTask {
/**
* 项目启动 30 秒后, 每 15 分钟执行一次刷新 OneDrive Token 的定时任务.
*/
@Scheduled(fixedRate = 1000 * 60 * 15, initialDelay = 1000 * 30)
@Scheduled(fixedRate = 1000 * 60 * 10, initialDelay = 1000 * 30)
public void autoRefreshOneDriveToken() {

AbstractFileService currentFileService = systemConfigService.getCurrentFileService();
Expand All @@ -54,16 +58,12 @@ public void autoRefreshOneDriveToken() {
return;
}

try {
refreshOneDriveToken(StorageTypeEnum.ONE_DRIVE);
} catch (Exception e) {
log.debug("刷新 OneDrive Token 失败.", e);
}
StorageTypeEnum currentStorageTypeEnum = currentFileService.getStorageTypeEnum();

try {
refreshOneDriveToken(StorageTypeEnum.ONE_DRIVE_CHINA);
refreshOneDriveToken(currentStorageTypeEnum);
} catch (Exception e) {
log.debug("刷新 OneDrive 世纪互联 Token 失败.", e);
log.debug("刷新 " + currentStorageTypeEnum.getDescription() + " Token 失败.", e);
}
}

Expand All @@ -78,4 +78,5 @@ public void refreshOneDriveToken(StorageTypeEnum storageType) {
}
log.info("刷新 {} key 时间: {}", storageType.getDescription(), LocalDateTime.now());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.Collections;

/**
* @author zhaojun
Expand All @@ -17,6 +18,7 @@ public class ZFileConfiguration {
public RestTemplate restTemplate(){
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
restTemplate.setInterceptors(Collections.singletonList(new ContentTypeTextToTextJson()));
return restTemplate;
}

Expand Down
14 changes: 8 additions & 6 deletions src/main/java/im/zhaojun/common/controller/CacheController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package im.zhaojun.common.controller;

import im.zhaojun.common.cache.ZFileCache;
import im.zhaojun.common.model.dto.CacheConfigDTO;
import im.zhaojun.common.model.dto.ResultBean;
import im.zhaojun.common.service.AbstractFileService;
Expand Down Expand Up @@ -30,6 +31,9 @@ public class CacheController {
@Resource
private FileCacheService fileCacheService;

@Resource
private ZFileCache zFileCache;

@PostMapping("/enable")
public ResultBean enableCache() throws Exception {
fileCacheService.enableCache();
Expand All @@ -43,16 +47,14 @@ public ResultBean disableCache() throws Exception {
}

@GetMapping("/config")
public ResultBean cacheConfig() throws Exception {
public ResultBean cacheConfig() {
AbstractFileService fileService = systemConfigService.getCurrentFileService();
Set<String> cacheKeys = fileService.getCacheKeys();

CacheConfigDTO cacheConfigDTO = new CacheConfigDTO();
cacheConfigDTO.setEnableCache(systemConfigService.getEnableCache());
cacheConfigDTO.setCacheFinish(fileAsyncCacheService.isCacheFinish());
cacheConfigDTO.setCacheKeys(cacheKeys);
cacheConfigDTO.setCacheDirectoryCount(fileAsyncCacheService.getCacheDirectoryCount());
cacheConfigDTO.setCacheFileCount(fileAsyncCacheService.getCacheFileCount());
cacheConfigDTO.setCacheKeys(zFileCache.keySet());
cacheConfigDTO.setCacheDirectoryCount(zFileCache.getCacheDirectorCount());
cacheConfigDTO.setCacheFileCount(zFileCache.getCacheFileCount());
return ResultBean.success(cacheConfigDTO);
}

Expand Down
Loading

0 comments on commit 399e961

Please sign in to comment.