Skip to content

Commit

Permalink
Performance optimization & Release 0.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzhiguo committed Jun 24, 2024
1 parent 90c3dd0 commit b500672
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 33 deletions.
4 changes: 2 additions & 2 deletions cache-ihc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<description>In heap cache, controlled by JVM, affecting GC</description>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
* Abstract Synchronous Cache
*
Expand All @@ -33,6 +36,8 @@
public abstract class AbstractSynchronousCache<K, V, X, Y> extends AbstractLoadingCache<K, V>
implements SynchronousCache<K, V, X, Y> {

private final ReadWriteLock rwLock = new ReentrantReadWriteLock();

/**
* Get value with sync value
*
Expand All @@ -43,37 +48,38 @@ public abstract class AbstractSynchronousCache<K, V, X, Y> extends AbstractLoadi
*/
@Override
public V getValueWithSyncValue(K key, Y syncValue) throws Exception {
if (effectiveCheck(key, syncValue)) {
return getValue(key);
} else {
if (null == getSyncValueLocalCache()) {
if (log.isWarnEnabled()) {
log.warn("该同步型缓存未提供'SyncValueLocalCache'具体实现,无法提供自动同步功能!cacheName={}", getName());
}
return getValue(key);
}
synchronized (this) {
boolean needSync = false;
if (!effectiveCheck(key, syncValue)) {
needSync = true;
removeValue(key);
}
V value = null;
try {
value = getValue(key);
if (needSync) {
getSyncValueLocalCache().putValue(key, syncValue);
if (log.isInfoEnabled()) {
log.info("[缓存同步]数据同步Key不一致,已更新!Cache={}, Key={}, SyncValue={}",
getName(), key, syncValue);
}
}
} catch (Exception e) {
log.error("Error occurred when getValue, Cache={}, Key={}, SyncValue={}",
getName(), key, syncValue, e);
if (getSyncValueLocalCache() == null) {
log.warn("SyncValueLocalCache not provided, automatic sync unavailable. Cache={}", getName());
return getValueSafely(key);
}
boolean needSync = !effectiveCheck(key, syncValue);
if (!needSync) {
return getValueSafely(key);
}
rwLock.writeLock().lock();
try {
if (!effectiveCheck(key, syncValue)) {
removeValue(key);
V value = getValueSafely(key);
if (value != null) {
getSyncValueLocalCache().putValue(key, syncValue);
log.info("Cache synced due to inconsistent sync value. Cache={}, Key={}, SyncValue={}",
getName(), key, syncValue);
}
return value;
}
} finally {
rwLock.writeLock().unlock();
}
return getValueSafely(key);
}

private V getValueSafely(K key) {
try {
return getValue(key);
} catch (Exception e) {
log.error("Error retrieving value. Cache={}, Key={}", getName(), key, e);
return null;
}
}

Expand Down
19 changes: 16 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@
</modules>

<properties>
<revision>0.1.3-SNAPSHOT</revision>
<revision>0.1.3</revision>
<jdk.version>1.8</jdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<maven-source-plugin.version>3.0.1</maven-source-plugin.version>
<maven-jar-plugin.version>3.1.0</maven-jar-plugin.version>
<maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version>
<maven-resources-plugin.version>3.1.0</maven-resources-plugin.version>
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
<maven-compiler-plugin.version>3.13.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<maven-deploy-plugin.version>3.1.2</maven-deploy-plugin.version>
<maven-surefire-plugin.argLine>-Xms512m -Xmx512m</maven-surefire-plugin.argLine>
Expand All @@ -77,6 +77,7 @@
<guava.version>32.1.3-jre</guava.version>
<caffeine.version>2.9.3</caffeine.version>
<fastjson-version>2.0.31</fastjson-version>
<javax.annotation-api.version>1.3.2</javax.annotation-api.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -153,8 +154,20 @@
</dependency>
</dependencies>
</dependencyManagement>

<profiles>
<profile>
<id>jdk11+</id>
<activation>
<jdk>[11,)</jdk>
</activation>
<dependencies>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>${javax.annotation-api.version}</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>sonatype-oss-release</id>
<activation>
Expand Down

0 comments on commit b500672

Please sign in to comment.