Skip to content

Commit

Permalink
系统缓存统一改为Caffeine,移除Guava
Browse files Browse the repository at this point in the history
  • Loading branch information
interestinglife41 committed Aug 19, 2021
1 parent 41ebeb9 commit afcf987
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Roadmap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
系统添加缓存支持:
ok 服务层API内部引入缓存特性;
实现postProcessGet,加载共享属性值;
系统缓存统一改为Caffeine,移除Guava以减少程序包大小;
ok 系统缓存统一改为Caffeine,移除Guava以减少程序包大小;
添加更多内置图表插件:
嵌套饼图
地图热力图
Expand Down
5 changes: 2 additions & 3 deletions datagear-analysis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import org.datagear.util.DateNumberFormat;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

import freemarker.cache.TemplateLoader;
import freemarker.core.OutputFormat;
Expand Down Expand Up @@ -175,7 +174,7 @@ public NameTemplateLoader(int cacheCapacity, int cacheExpireSeconds)
{
super();

this.nameTemplateCache = CacheBuilder.newBuilder().maximumSize(cacheCapacity)
this.nameTemplateCache = Caffeine.newBuilder().maximumSize(cacheCapacity)
.expireAfterAccess(cacheExpireSeconds, TimeUnit.SECONDS).build();
}

Expand All @@ -197,21 +196,14 @@ public void closeTemplateSource(Object templateSource) throws IOException
@Override
public Object findTemplateSource(String name) throws IOException
{
try
return this.nameTemplateCache.get(name, new Function<String, NameTemplateSource>()
{
return this.nameTemplateCache.get(name, new Callable<NameTemplateSource>()
@Override
public NameTemplateSource apply(String name)
{
@Override
public NameTemplateSource call() throws Exception
{
return new NameTemplateSource(name, System.currentTimeMillis());
}
});
}
catch (ExecutionException e)
{
throw new IOException("find template source in cache exception", e);
}
return new NameTemplateSource(name, System.currentTimeMillis());
}
});
}

@Override
Expand Down
5 changes: 2 additions & 3 deletions datagear-connection/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
<artifactId>commons-dbcp2</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,28 @@
import java.util.Comparator;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.ConnectionFactory;
import org.apache.commons.dbcp2.DriverConnectionFactory;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.datagear.util.JDBCCompatiblity;
import org.datagear.util.JdbcUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalCause;
import com.github.benmanes.caffeine.cache.RemovalListener;

/**
* 默认{@linkplain ConnectionSource}实现。
Expand Down Expand Up @@ -71,7 +73,7 @@ public DefaultConnectionSource(DriverEntityManager driverEntityManager)
{
super();
this.driverEntityManager = driverEntityManager;
this.internalDataSourceCache = CacheBuilder.newBuilder().maximumSize(50)
this.internalDataSourceCache = Caffeine.newBuilder().maximumSize(50)
.expireAfterAccess(60 * 24, TimeUnit.MINUTES)
.removalListener(new DriverBasicDataSourceRemovalListener()).build();
}
Expand Down Expand Up @@ -397,10 +399,10 @@ protected Connection getConnection(Driver driver, String url, Properties propert
try
{
dataSourceHolder = this.internalDataSourceCache.get(connectionIdentity,
new Callable<InternalDataSourceHolder>()
new Function<ConnectionIdentity, InternalDataSourceHolder>()
{
@Override
public InternalDataSourceHolder call() throws Exception
public InternalDataSourceHolder apply(ConnectionIdentity key)
{
DataSource dataSource = createInternalDataSource(driver, url, properties);
InternalDataSourceHolder holder = new InternalDataSourceHolder();
Expand Down Expand Up @@ -661,14 +663,13 @@ protected static class DriverBasicDataSourceRemovalListener
implements RemovalListener<ConnectionIdentity, InternalDataSourceHolder>
{
@Override
public void onRemoval(RemovalNotification<ConnectionIdentity, InternalDataSourceHolder> notification)
public void onRemoval(@Nullable ConnectionIdentity key, @Nullable InternalDataSourceHolder value,
@NonNull RemovalCause cause)
{
InternalDataSourceHolder holder = notification.getValue();

if (!holder.hasDataSource())
if (!value.hasDataSource())
return;

DataSource dataSource = holder.getDataSource();
DataSource dataSource = value.getDataSource();

if (!(dataSource instanceof DriverBasicDataSource))
throw new UnsupportedOperationException(
Expand All @@ -680,7 +681,7 @@ public void onRemoval(RemovalNotification<ConnectionIdentity, InternalDataSource
((DriverBasicDataSource) dataSource).close();

if (LOGGER.isDebugEnabled())
LOGGER.debug("Close internal data source for {}", notification.getKey());
LOGGER.debug("Close internal data source for {}", key);
}
catch (SQLException e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;

/**
* 消息通道。
Expand All @@ -32,15 +31,15 @@ public class MessageChannel

public MessageChannel()
{
this(60 * 10);
this(60 * 60);
}

public MessageChannel(int channelExpireSeconds)
{
super();

// 消息通道只允许超时,不允许被其他情况移除
this._cache = CacheBuilder.newBuilder().maximumSize(Integer.MAX_VALUE)
this._cache = Caffeine.newBuilder().maximumSize(Integer.MAX_VALUE)
.expireAfterAccess(channelExpireSeconds, TimeUnit.SECONDS)
.build(new CacheLoader<String, LinkedBlockingQueue<Object>>()
{
Expand Down Expand Up @@ -113,7 +112,7 @@ protected LinkedBlockingQueue<Object> getChannelQueueNonNull(String channel)
{
return this._cache.get(channel);
}
catch(ExecutionException e)
catch (Throwable e)
{
throw new MessageChannelException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import org.datagear.management.domain.Schema;
import org.datagear.meta.Table;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

/**
* {@linkplain Table}缓存。
Expand Down Expand Up @@ -65,8 +65,8 @@ public void setExpireAfterAccessMinutes(int expireAfterAccessMinutes)
*/
public void init()
{
this._cache = CacheBuilder.newBuilder().maximumSize(this.maximumSize)
.expireAfterAccess(this.expireAfterAccessMinutes * 60, TimeUnit.SECONDS).build();
this._cache = Caffeine.newBuilder().maximumSize(this.maximumSize)
.expireAfterAccess(this.expireAfterAccessMinutes, TimeUnit.MINUTES).build();
}

/**
Expand Down
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<java.version>1.8</java.version>
<springboot.version>2.4.6</springboot.version>
<javax.json.version>1.0.4</javax.json.version>
<guava.version>28.2-jre</guava.version>
<commons-csv.version>1.4</commons-csv.version>
<poi.version>3.17</poi.version>
<poi-ooxml.version>3.17</poi-ooxml.version>
Expand Down

0 comments on commit afcf987

Please sign in to comment.