Commit 7336975b authored by 张辰's avatar 张辰

local cache

parent 87da0782
...@@ -65,6 +65,16 @@ ...@@ -65,6 +65,16 @@
<artifactId>spring-cloud-starter-zipkin</artifactId> <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
<!-- mybatis-plus自动生成所需包--> <!-- mybatis-plus自动生成所需包-->
<dependency> <dependency>
<groupId>org.apache.velocity</groupId> <groupId>org.apache.velocity</groupId>
......
...@@ -3,10 +3,13 @@ package com.tanpu.community.config; ...@@ -3,10 +3,13 @@ package com.tanpu.community.config;
import com.tanpu.community.cache.RedisCache; import com.tanpu.community.cache.RedisCache;
import com.tanpu.community.util.SpringUtils; import com.tanpu.community.util.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Arrays;
@Configuration @Configuration
public class CacheConfig { public class CacheConfig {
...@@ -16,6 +19,14 @@ public class CacheConfig { ...@@ -16,6 +19,14 @@ public class CacheConfig {
@Bean @Bean
public RedisCache redisCache() { public RedisCache redisCache() {
return new RedisCache.Builder().cacheName("community2").build(); return new RedisCache.Builder().cacheName("redis").build();
}
@Bean
public CacheManager cacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
cacheManager.setCacheNames(Arrays.asList("local"));
cacheManager.setCacheSpecification("maximumSize=10,expireAfterWrite=10s");
return cacheManager;
} }
} }
...@@ -21,4 +21,16 @@ public class FileController { ...@@ -21,4 +21,16 @@ public class FileController {
public CommonResp<FileUploadResp> uploadToRemote(@RequestParam(value = "file") MultipartFile file) { public CommonResp<FileUploadResp> uploadToRemote(@RequestParam(value = "file") MultipartFile file) {
return CommonResp.success(fileManager.uploadFile(file)); return CommonResp.success(fileManager.uploadFile(file));
} }
@GetMapping("/test")
public String test() {
for (int i = 0; i < 30; i++) {
System.out.println(fileManager.getId("" + i / 2));
}
for (int i = 30; i > 0; i--) {
System.out.println(fileManager.getId("" + i / 2));
}
return "";
}
} }
...@@ -8,6 +8,7 @@ import com.tanpu.community.service.OSSFileService; ...@@ -8,6 +8,7 @@ import com.tanpu.community.service.OSSFileService;
import com.tanpu.community.util.ConvertUtil; import com.tanpu.community.util.ConvertUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
...@@ -49,4 +50,10 @@ public class FileManager { ...@@ -49,4 +50,10 @@ public class FileManager {
return ossFileService.queryByIds(fileIds).stream() return ossFileService.queryByIds(fileIds).stream()
.collect(Collectors.toMap(FileRecordEntity::getFileId, FileRecordEntity::getUrl)); .collect(Collectors.toMap(FileRecordEntity::getFileId, FileRecordEntity::getUrl));
} }
@Cacheable(value = "local", key = "#id")
public String getId(String id) {
System.out.println("cache " + id);
return "haha " + id;
}
} }
...@@ -128,6 +128,18 @@ ...@@ -128,6 +128,18 @@
<version>2.4.5</version> <version>2.4.5</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.7.0</version>
</dependency>
<dependency> <dependency>
<groupId>com.baomidou</groupId> <groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId> <artifactId>mybatis-plus-boot-starter</artifactId>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment