-
Notifications
You must be signed in to change notification settings - Fork 47
jvm缓存 #229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xiaoxude
wants to merge
14
commits into
netease-lcap:main
Choose a base branch
from
xiaoxude:feat/sort-map
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
jvm缓存 #229
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
66f87c7
feat:新增有序map依赖库
b27d983
UPDATE
ab28009
update
0801c7d
update
96f094a
新增jvm缓存
fe7db14
添加jvm缓存
75d89d6
1
0975414
1
a6a3c6b
Merge branch 'main' into feat/sort-map
xiaoxude 0fd626c
jvm 缓存
017018c
Merge remote-tracking branch 'origin/feat/sort-map' into feat/sort-map
b830651
添加jvm缓存文档
4e2ce49
update
3c7949a
update:添加参数校验
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
| [jvm.pdf](doc%2Fjvm.pdf) |
Binary file not shown.
Binary file not shown.
This file contains hidden or 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 @@ | ||
| mvn install:install-file -Dfile="nasl-metadata-maven-plugin-1.3.0.jar" -DpomFile="pom.xml" |
This file contains hidden or 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 @@ | ||
| mvn install:install-file -Dfile="nasl-metadata-maven-plugin-1.3.0.jar" -DpomFile="pom.xml" |
Binary file added
BIN
+24.1 KB
jvm-cache/jar/nasl-metadata-maven-plugin-1.3.0/nasl-metadata-maven-plugin-1.3.0.jar
Binary file not shown.
This file contains hidden or 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,74 @@ | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>com.netease.lowcode</groupId> | ||
| <artifactId>nasl-metadata-maven-plugin</artifactId> | ||
| <version>1.3.0</version> | ||
| <packaging>maven-plugin</packaging> | ||
|
|
||
| <name>Nasl Metadata Maven Plugin</name> | ||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.apache.maven</groupId> | ||
| <artifactId>maven-plugin-api</artifactId> | ||
| <version>2.0</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.maven</groupId> | ||
| <artifactId>maven-project</artifactId> | ||
| <version>2.0.10</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.maven.plugin-tools</groupId> | ||
| <artifactId>maven-plugin-annotations</artifactId> | ||
| <version>3.2</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.codehaus.plexus</groupId> | ||
| <artifactId>plexus-compiler-manager</artifactId> | ||
| <version>2.8.4</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.codehaus.plexus</groupId> | ||
| <artifactId>plexus-utils</artifactId> | ||
| <version>3.0.8</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.maven.shared</groupId> | ||
| <artifactId>maven-common-artifact-filters</artifactId> | ||
| <version>3.0.1</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.apache.maven</groupId> | ||
| <artifactId>maven-aether-provider</artifactId> | ||
| <version>3.3.9</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.eclipse.aether</groupId> | ||
| <artifactId>aether-api</artifactId> | ||
| <version>1.1.0</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-plugin-plugin</artifactId> | ||
| <version>3.2</version> | ||
| <configuration> | ||
| <goalPrefix>nasl-metadata-maven-plugin</goalPrefix> | ||
| <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
Binary file not shown.
This file contains hidden or 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,78 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-parent</artifactId> | ||
| <version>2.2.9.RELEASE</version><!--与当前制品应用默认版本统一--> | ||
| <relativePath/> <!-- lookup parent from repository --> | ||
| </parent> | ||
|
|
||
| <groupId>com.netease.cache</groupId> | ||
| <artifactId>jvm-cache</artifactId> | ||
| <version>1.0.2</version> | ||
|
|
||
| <properties> | ||
| <maven.compiler.source>8</maven.compiler.source> | ||
| <maven.compiler.target>8</maven.compiler.target> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <nasl.ide.version>3.3</nasl.ide.version> | ||
| </properties> | ||
| <dependencies> | ||
| <!--本案例是本地系统引入nasl-metadata-collector-0.8.0.jar的方式。 | ||
| 若把nasl-metadata-collector-0.8.0.jar安装到自己的maven仓库, | ||
| 注意修改artifactId和groupId的情况下,不要使用<scope>system</scope>,会在发布时造成依赖中断。 | ||
| 不修改artifactId和groupId的情况下,nasl-metadata-maven-plugin会做特殊处理--> | ||
| <dependency> | ||
| <artifactId>nasl-metadata-collector</artifactId> | ||
| <groupId>com.netease.lowcode</groupId> | ||
| <version>0.8.0</version> | ||
| <optional>true</optional> | ||
| <scope>system</scope> | ||
| <systemPath>${project.basedir}/jar/nasl-metadata-collector-0.8.0.jar</systemPath> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.guava</groupId> | ||
| <artifactId>guava</artifactId> | ||
| <version>32.0.1-jre</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.fasterxml.jackson.core</groupId> | ||
| <artifactId>jackson-databind</artifactId> | ||
| <version>${jackson.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.slf4j</groupId> | ||
| <artifactId>slf4j-api</artifactId> | ||
| <scope>provided</scope> | ||
| <version>1.7.30</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.springframework</groupId> | ||
| <artifactId>spring-context</artifactId> | ||
| <version>5.2.8.RELEASE</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>com.netease.lowcode</groupId> | ||
| <artifactId>nasl-metadata-maven-plugin</artifactId> | ||
| <version>1.3.0</version> | ||
| <configuration> | ||
| <jarWithDependencies>false</jarWithDependencies> | ||
| </configuration> | ||
| <executions> | ||
| <execution> | ||
| <goals> | ||
| <goal>archive</goal> | ||
| </goals> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
68 changes: 68 additions & 0 deletions
68
jvm-cache/src/main/java/com/netease/cache/jvm/api/FlexibleExpirationCache.java
This file contains hidden or 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,68 @@ | ||
| package com.netease.cache.jvm.api; | ||
|
|
||
| import com.google.common.cache.Cache; | ||
| import com.google.common.cache.CacheBuilder; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.Callable; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| public class FlexibleExpirationCache<K, V> { | ||
| private final Cache<K, V> cache; | ||
| private final ConcurrentHashMap<K, Long> expirationMap = new ConcurrentHashMap<>(); | ||
|
|
||
| public FlexibleExpirationCache(Long maxSize) { | ||
| this.cache = CacheBuilder.newBuilder() | ||
| .removalListener(notification -> { | ||
| if (notification.getKey() != null) { | ||
| expirationMap.remove(notification.getKey()); | ||
| } | ||
| }) | ||
| .maximumSize(maxSize) | ||
| .build(); | ||
| } | ||
|
|
||
| public void put(K key, V value, Long expireAfterWriteSeconds) { | ||
| cache.put(key, value); | ||
| expirationMap.put(key, System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(expireAfterWriteSeconds)); | ||
| } | ||
|
|
||
| public V get(K key) { | ||
| if (isExpired(key)) { | ||
| invalidate(key); | ||
| return null; | ||
| } | ||
| return cache.getIfPresent(key); | ||
| } | ||
|
|
||
| public List<K> getAllKeys() { | ||
| return new ArrayList<>(cache.asMap().keySet()); | ||
| } | ||
|
|
||
| public V getValue(K key, Callable<V> loader) { | ||
| try { | ||
| return cache.get(key, loader); | ||
| } catch (ExecutionException e) { | ||
| // 处理加载异常 | ||
| throw new RuntimeException(e.getCause()); | ||
| } | ||
| } | ||
|
|
||
| public boolean isExpired(K key) { | ||
| Long expiryTime = expirationMap.get(key); | ||
| return expiryTime != null && System.currentTimeMillis() > expiryTime; | ||
| } | ||
|
|
||
| public void invalidate(K key) { | ||
| cache.invalidate(key); | ||
| expirationMap.remove(key); | ||
| } | ||
|
|
||
| public void invalidateAll() { | ||
| cache.invalidateAll(); | ||
| expirationMap.clear(); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
...cache/src/main/java/com/netease/cache/jvm/api/JvmCacheSpringEnvironmentConfiguration.java
This file contains hidden or 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,13 @@ | ||
| package com.netease.cache.jvm.api; | ||
|
|
||
| import org.springframework.context.annotation.ComponentScan; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
|
|
||
| /** | ||
| * @author system | ||
| */ | ||
| @Configuration | ||
| @ComponentScan(basePackageClasses = LibraryAutoScan.class) | ||
| public class JvmCacheSpringEnvironmentConfiguration { | ||
| } |
130 changes: 130 additions & 0 deletions
130
jvm-cache/src/main/java/com/netease/cache/jvm/api/JvmCacheUtil.java
This file contains hidden or 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,130 @@ | ||
| package com.netease.cache.jvm.api; | ||
|
|
||
| import com.netease.lowcode.core.EnvironmentType; | ||
| import com.netease.lowcode.core.annotation.Environment; | ||
| import com.netease.lowcode.core.annotation.NaslConfiguration; | ||
| import com.netease.lowcode.core.annotation.NaslLogic; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import javax.annotation.PostConstruct; | ||
| import java.util.List; | ||
| import java.util.function.Function; | ||
|
|
||
| @Component | ||
| public class JvmCacheUtil { | ||
|
|
||
| private static FlexibleExpirationCache<String, String> cache; | ||
|
|
||
| /** | ||
| * 缓存空间大小 | ||
| * 类型为Long | ||
| */ | ||
| @Value("${cacheMaximumSize}") | ||
| @NaslConfiguration(defaultValue = {@Environment(type = EnvironmentType.DEV, value = "100"), | ||
| @Environment(type = EnvironmentType.ONLINE, value = "100")}) | ||
| public Long cacheMaximumSize; | ||
|
|
||
|
|
||
| @PostConstruct | ||
| public void init() { | ||
| cache = new FlexibleExpirationCache<>(cacheMaximumSize); | ||
| } | ||
|
|
||
| /** | ||
| * 添加缓存 | ||
| * | ||
| * @param key 键 | ||
| * @param value 值 | ||
| * @param expireAfterWriteSeconds 过期时间(秒) | ||
| */ | ||
| @NaslLogic | ||
| public String setCache(String key, String value, Long expireAfterWriteSeconds) { | ||
| assertNotNull(key, value, expireAfterWriteSeconds); | ||
| cache.put(key, value, expireAfterWriteSeconds); | ||
| return value; | ||
| } | ||
|
|
||
| /** | ||
| * 获取缓存 | ||
| * | ||
| * @param key 键 | ||
| * @return 值 | ||
| */ | ||
| @NaslLogic | ||
| public String getCache(String key) { | ||
| assertNotNull(key); | ||
| return cache.get(key); | ||
| } | ||
|
|
||
| /** | ||
| * 获取缓存,缓存不存在则执行function, 并将查询的值放到缓存中 | ||
| */ | ||
| @NaslLogic | ||
| public String getOrComputeAndSet(String key, Long expireAfterWriteSeconds, Function<String, String> functionStr) { | ||
| assertNotNull(key, expireAfterWriteSeconds, functionStr); | ||
| String value = getCache(key); | ||
| if (value != null) { | ||
| return value; | ||
| } | ||
|
|
||
| String str = functionStr.apply(null); | ||
| if (str != null) { | ||
| setCache(key, str, expireAfterWriteSeconds); | ||
| } | ||
| return str; | ||
| } | ||
|
|
||
| /** | ||
| * 是否过期 | ||
| * | ||
| * @param key 键 | ||
| * @return true/false | ||
| */ | ||
| @NaslLogic | ||
| public Boolean isExpired(String key) { | ||
| assertNotNull(key); | ||
| return cache.isExpired(key); | ||
| } | ||
|
|
||
| /** | ||
| * 清除缓存 | ||
| * | ||
| * @param key 键 | ||
| */ | ||
| @NaslLogic | ||
| public Boolean invalidate(String key) { | ||
| assertNotNull(key); | ||
| cache.invalidate(key); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * 清除所有缓存 | ||
| */ | ||
| @NaslLogic | ||
| public Boolean invalidateAll() { | ||
| cache.invalidateAll(); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * 查询缓存中所有的key | ||
| */ | ||
| @NaslLogic | ||
| public List<String> keys() { | ||
| return cache.getAllKeys(); | ||
| } | ||
|
|
||
| private void assertNotNull(Object object) { | ||
| if (null == object) { | ||
| throw new RuntimeException("INPUT_INVALID"); | ||
| } | ||
| } | ||
|
|
||
| private void assertNotNull(Object... objects){ | ||
| for (Object object : objects) { | ||
| assertNotNull(object); | ||
| } | ||
| } | ||
| } | ||
8 changes: 8 additions & 0 deletions
8
jvm-cache/src/main/java/com/netease/cache/jvm/api/LibraryAutoScan.java
This file contains hidden or 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,8 @@ | ||
| package com.netease.cache.jvm.api; | ||
|
|
||
| /** | ||
| * 依赖库自动扫描类 | ||
| * @author system | ||
| */ | ||
| public class LibraryAutoScan { | ||
| } |
This file contains hidden or 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,2 @@ | ||
| org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ | ||
| com.netease.cache.jvm.api.JvmCacheSpringEnvironmentConfiguration |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议对入参增加校验