Skip to content

Commit 81cee2d

Browse files
author
litongjava
committed
remove caffeine and add ehcache
1 parent 2bde432 commit 81cee2d

File tree

10 files changed

+154
-169
lines changed

10 files changed

+154
-169
lines changed

frameworks/Java/act/src/main/resources/conf/ebean_mysql/db.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mysql.host=localhost
33
app.batch_save=false
44

55
db.impl=act.db.ebean.EbeanPlugin
6-
db.url=jdbc:mysql://${mysql.host}:3306/hello_world?jdbcCompliantTruncation=false&elideSetAutoCommits=true&useLocalSessionState=true&PrepStmts=true&cacheCallableStmts=true&alwaysSendSetIsolation=false&prepStmtCacheSize=4096&cacheServerConfiguration=true&prepStmtCacheSqlLimit=2048&zeroDateTimeBehavior=convertToNull&traceProtocol=false&useUnbufferedInput=false&useReadAheadInput=false&maintainTimeStats=false&useServerPrepStmts=true&cacheRSMetadata=true&serverTimezone=UTC&useSSL=false
6+
db.url=jdbc:mysql://${mysql.host}:3306/hello_world?jdbcCompliantTruncation=false&elideSetAutoCommits=true&useLocalSessionState=true&cachePrepStmts=true&cacheCallableStmts=true&alwaysSendSetIsolation=false&prepStmtCacheSize=4096&cacheServerConfiguration=true&prepStmtCacheSqlLimit=2048&zeroDateTimeBehavior=convertToNull&traceProtocol=false&useUnbufferedInput=false&useReadAheadInput=false&maintainTimeStats=false&useServerPrepStmts=true&cacheRSMetadata=true&serverTimezone=UTC&useSSL=false
77

88
db.slave.maxConnections=48
99
db.slave.minConnections=48

frameworks/Java/tio-server/pom.xml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@
1515
<dependency>
1616
<groupId>com.litongjava</groupId>
1717
<artifactId>tio-http-server</artifactId>
18-
<version>3.7.3.v20231218-RELEASE</version>
18+
<version>3.7.3.v20231224-RELEASE</version>
1919
</dependency>
2020

2121

22+
<!-- https://mvnrepository.com/artifact/com.jfinal/activerecord -->
2223
<dependency>
23-
<groupId>com.litongjava</groupId>
24-
<artifactId>table-to-json</artifactId>
25-
<version>1.2.1</version>
24+
<groupId>com.jfinal</groupId>
25+
<artifactId>activerecord</artifactId>
26+
<version>5.1.2</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>net.sf.ehcache</groupId>
31+
<artifactId>ehcache-core</artifactId>
32+
<version>2.6.11</version>
2633
</dependency>
2734

2835

@@ -155,7 +162,7 @@
155162
</dependency>
156163
</dependencies>
157164
<build>
158-
<finalName>${final.name}</finalName>
165+
<finalName>${project.build.finalName}-native</finalName>
159166
<plugins>
160167
<plugin>
161168
<groupId>org.graalvm.nativeimage</groupId>
@@ -171,7 +178,7 @@
171178
</executions>
172179
<configuration>
173180
<skip>false</skip>
174-
<imageName>${project.build.finalName}</imageName>
181+
<imageName>${project.build.finalName}-native</imageName>
175182
<mainClass>${main.class}</mainClass>
176183
<buildArgs>
177184
-H:+RemoveSaturatedTypeFlows

frameworks/Java/tio-server/src/main/java/com/litongjava/tio/http/server/MainApp.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
package com.litongjava.tio.http.server;
22

3-
import java.io.IOException;
4-
53
import com.litongjava.tio.http.common.HttpConfig;
64
import com.litongjava.tio.http.common.handler.HttpRequestHandler;
7-
import com.litongjava.tio.http.server.config.CaffeineCacheConfig;
5+
import com.litongjava.tio.http.server.config.EhCachePluginConfig;
86
import com.litongjava.tio.http.server.config.EnjoyEngineConfig;
97
import com.litongjava.tio.http.server.config.MysqlDbConfig;
108
import com.litongjava.tio.http.server.controller.CacheController;
119
import com.litongjava.tio.http.server.controller.DbController;
1210
import com.litongjava.tio.http.server.controller.IndexController;
1311
import com.litongjava.tio.http.server.handler.HttpRoutes;
14-
import com.litongjava.tio.http.server.handler.SimpleHttpDispahterHanlder;
12+
import com.litongjava.tio.http.server.handler.SimpleHttpDispatcherHandler;
1513
import com.litongjava.tio.http.server.handler.SimpleHttpRoutes;
16-
import com.litongjava.tio.http.server.utils.EnviormentUtils;
1714
import com.litongjava.tio.server.ServerTioConfig;
15+
import com.litongjava.tio.utils.environment.EnvironmentUtils;
1816

1917
public class MainApp {
2018

2119
public static void main(String[] args) {
2220
long start = System.currentTimeMillis();
23-
EnviormentUtils.buildCmdArgsMap(args);
21+
EnvironmentUtils.buildCmdArgsMap(args);
2422
// add route
2523
IndexController controller = new IndexController();
2624
HttpRoutes simpleHttpRoutes = new SimpleHttpRoutes();
@@ -36,7 +34,6 @@ public static void main(String[] args) {
3634

3735
CacheController cacheController = new CacheController();
3836
simpleHttpRoutes.add("/cacheQuery", cacheController::cacheQuery);
39-
simpleHttpRoutes.add("/cacheList", cacheController::cacheList);
4037

4138
// config server
4239
HttpConfig httpConfig = new HttpConfig(8080, null, null, null);
@@ -45,7 +42,7 @@ public static void main(String[] args) {
4542
httpConfig.setCheckHost(false);
4643
httpConfig.setCompatible1_0(false);
4744

48-
HttpRequestHandler requestHandler = new SimpleHttpDispahterHanlder(httpConfig, simpleHttpRoutes);
45+
HttpRequestHandler requestHandler = new SimpleHttpDispatcherHandler(httpConfig, simpleHttpRoutes);
4946
HttpServerStarter httpServerStarter = new HttpServerStarter(httpConfig, requestHandler);
5047
ServerTioConfig serverTioConfig = httpServerStarter.getServerTioConfig();
5148
// close Heartbeat
@@ -56,7 +53,7 @@ public static void main(String[] args) {
5653
httpServerStarter.start();
5754
new MysqlDbConfig().init();
5855
new EnjoyEngineConfig().engine();
59-
new CaffeineCacheConfig().register();
56+
new EhCachePluginConfig().ehCachePlugin();
6057
long end = System.currentTimeMillis();
6158
System.out.println((end - start) + "ms");
6259
} catch (Exception e) {

frameworks/Java/tio-server/src/main/java/com/litongjava/tio/http/server/config/CaffeineCacheConfig.java

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.litongjava.tio.http.server.config;
2+
3+
4+
import com.jfinal.plugin.ehcache.EhCachePlugin;
5+
6+
public class EhCachePluginConfig {
7+
8+
public EhCachePlugin ehCachePlugin() {
9+
EhCachePlugin ehCachePlugin = new EhCachePlugin();
10+
ehCachePlugin.start();
11+
return ehCachePlugin;
12+
}
13+
}

frameworks/Java/tio-server/src/main/java/com/litongjava/tio/http/server/config/MysqlDbConfig.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
44
import com.jfinal.plugin.activerecord.OrderedFieldContainerFactory;
55
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
6-
import com.litongjava.tio.http.server.utils.EnviormentUtils;
6+
import com.litongjava.tio.utils.environment.EnvironmentUtils;
77

88
public class MysqlDbConfig {
99

1010
public void init() {
1111
// start active recored
12-
String jdbcUrl = EnviormentUtils.get("JDBC_URL");
12+
13+
String jdbcUrl = EnvironmentUtils.get("JDBC_URL");
1314
// String jdbcUrl = "jdbc:mysql://192.168.3.9/hello_world";
1415

15-
String jdbcUser = EnviormentUtils.get("JDBC_USER");
16+
String jdbcUser = EnvironmentUtils.get("JDBC_USER");
1617
// String jdbcUser = "root";
1718

18-
String jdbcPswd = EnviormentUtils.get("JDBC_PSWD");
19+
String jdbcPswd = EnvironmentUtils.get("JDBC_PSWD");
1920
// String jdbcPswd = "robot_123456#";
2021
HikariCpPlugin hikariCpPlugin = new HikariCpPlugin(jdbcUrl, jdbcUser, jdbcPswd);
2122

frameworks/Java/tio-server/src/main/java/com/litongjava/tio/http/server/controller/CacheController.java

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public class CacheController {
2525
public HttpResponse cacheQuery(HttpRequest request) {
2626
String queries = request.getParam("queries");
2727
List<Map<String, Object>> recordMaps = RandomUtils.randomWorldNumbers()
28-
// limit
29-
.limit(RandomUtils.parseQueryCount(queries)) // 限制查询数量
30-
.mapToObj(id -> findByIdWithCache("world", id)) // 使用 mapToObj 将 int 映射为对象
31-
.filter(Objects::nonNull) // 过滤掉 null 值
32-
.map(Record::toMap) // 将每个 Record 对象转换为 Map
33-
.collect(Collectors.toList()); // 收集到 List
28+
// limit
29+
.limit(RandomUtils.parseQueryCount(queries)) // 限制查询数量
30+
.mapToObj(id -> findByIdWithCache("world", id)) // 使用 mapToObj 将 int 映射为对象
31+
.filter(Objects::nonNull) // 过滤掉 null 值
32+
.map(Record::toMap) // 将每个 Record 对象转换为 Map
33+
.collect(Collectors.toList()); // 收集到 List
3434

3535
HttpResponse httpResponse = new HttpResponse(request);
3636
httpResponse.addHeader(HeaderName.Content_Type, HeaderValue.Content_Type.TEXT_PLAIN_JSON);
@@ -40,30 +40,7 @@ public HttpResponse cacheQuery(HttpRequest request) {
4040
}
4141

4242
private Record findByIdWithCache(String tableName, int id) {
43-
44-
ICache cache = CaffeineCache.getCache("world");
45-
46-
// firsthandCreater用户查询数据库
47-
FirsthandCreater<Record> firsthandCreater = new FirsthandCreater<Record>() {
48-
@Override
49-
public Record create() {
50-
// log.info("select from db:{},id", tableName, id);
51-
return Db.findById(tableName, id);
52-
}
53-
};
54-
55-
String key = id + "";
56-
boolean putTempToCacheIfNull = false;
57-
Record value = CacheUtils.get(cache, key, putTempToCacheIfNull, firsthandCreater);
58-
return value;
59-
}
60-
61-
public HttpResponse cacheList(HttpRequest request) {
62-
Collection<String> keys = CaffeineCache.getCache("world").keys();
63-
64-
HttpResponse httpResponse = new HttpResponse(request);
65-
httpResponse.addHeader(HeaderName.Content_Type, HeaderValue.Content_Type.TEXT_PLAIN_JSON);
66-
httpResponse.setBody(JSON.toJSONString(keys).getBytes());
67-
return httpResponse;
43+
String sql = "SELECT id, randomNumber FROM world WHERE id = ?";
44+
return Db.findFirstByCache(tableName, id, sql, id);
6845
}
6946
}

frameworks/Java/tio-server/src/main/java/com/litongjava/tio/http/server/controller/DbController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.alibaba.fastjson2.JSON;
1212
import com.jfinal.plugin.activerecord.Db;
1313
import com.jfinal.plugin.activerecord.Record;
14+
import com.jfinal.plugin.ehcache.CacheKit;
1415
import com.jfinal.template.Engine;
1516
import com.jfinal.template.Template;
1617
import com.litongjava.tio.http.common.HeaderName;
@@ -71,8 +72,7 @@ public HttpResponse queries(HttpRequest request) {
7172
public HttpResponse updates(HttpRequest request) {
7273
String queries = request.getParam("queries");
7374

74-
ICache cache = CaffeineCache.getCache("world");
75-
cache.clear();
75+
CacheKit.removeAll("world");
7676

7777
List<Map<String, Object>> updatedRecords = RandomUtils.randomWorldNumbers()// random numbers
7878
// limit

0 commit comments

Comments
 (0)