Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jvm-cache/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[jvm.pdf](doc%2Fjvm.pdf)
Binary file added jvm-cache/doc/jvm.pdf
Binary file not shown.
Binary file added jvm-cache/jar/nasl-metadata-collector-0.8.0.jar
Binary file not shown.
1 change: 1 addition & 0 deletions jvm-cache/jar/nasl-metadata-maven-plugin-1.3.0/install.bat
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"
1 change: 1 addition & 0 deletions jvm-cache/jar/nasl-metadata-maven-plugin-1.3.0/install.sh
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 not shown.
74 changes: 74 additions & 0 deletions jvm-cache/jar/nasl-metadata-maven-plugin-1.3.0/pom.xml
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 added jvm-cache/jvm缓存.docx
Binary file not shown.
78 changes: 78 additions & 0 deletions jvm-cache/pom.xml
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>
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();
}
}
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 jvm-cache/src/main/java/com/netease/cache/jvm/api/JvmCacheUtil.java
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) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议对入参增加校验

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);
}
}
}
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 {
}
2 changes: 2 additions & 0 deletions jvm-cache/src/main/resource/META-INF/spring.factories
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