Skip to content

Commit 8d48923

Browse files
author
litongjava
committed
add table-to-json
1 parent 06c11a2 commit 8d48923

File tree

14 files changed

+3114
-1766
lines changed

14 files changed

+3114
-1766
lines changed

docs/.vuepress/config/sidebar-zh.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@
133133
"15_table-to-json/03.md",
134134
"15_table-to-json/04.md",
135135
"15_table-to-json/05.md",
136-
"15_table-to-json/06.md"
136+
"15_table-to-json/06.md",
137+
"15_table-to-json/07.md",
138+
"15_table-to-json/08.md"
137139
]
138140
},
139141
{
@@ -164,7 +166,13 @@
164166
{
165167
"title": "22_tio-utils",
166168
"collapsable": false,
167-
"children": ["22_tio-utils/01.md", "22_tio-utils/02.md", "22_tio-utils/03.md", "22_tio-utils/04.md"]
169+
"children": [
170+
"22_tio-utils/01.md",
171+
"22_tio-utils/02.md",
172+
"22_tio-utils/03.md",
173+
"22_tio-utils/04.md",
174+
"22_tio-utils/05.md"
175+
]
168176
},
169177
{
170178
"title": "25_性能测试",

docs/zh/14_jfinal-plugins/01.md

+2-207
Original file line numberDiff line numberDiff line change
@@ -1,208 +1,3 @@
1-
# tio-boot jfinal-plugins 整合 ehcache
1+
# 概述
22

3-
Tio-boot 是一个基于 Java 的网络编程框架,用于快速开发高性能的网络应用程序。
4-
Ehcache 是一个广泛使用的开源 Java 缓存,它可以提高应用程序的性能和扩展性。
5-
6-
整合 ecache 需要用到 jfinal-plugins
7-
https://central.sonatype.com/artifact/com.litongjava/jfinal-plugins
8-
9-
### 添加依赖
10-
11-
```
12-
<dependency>
13-
<groupId>com.litongjava</groupId>
14-
<artifactId>jfinal-plugins</artifactId>
15-
<version>1.0.6</version>
16-
</dependency>
17-
<dependency>
18-
<groupId>net.sf.ehcache</groupId>
19-
<artifactId>ehcache-core</artifactId>
20-
<version>2.6.11</version>
21-
</dependency>
22-
23-
```
24-
25-
### 添加配置文件 ehcache.xml
26-
27-
`ehcache.xml` 是 Ehcache 缓存的配置文件。EcachePlugin 启动时会自动加载这个配置,它定义了缓存的基本属性和行为。以下是文件中每个部分的详细解释:
28-
29-
1. **`<diskStore>`**: 指定磁盘存储的路径,用于溢出或持久化缓存数据到磁盘。
30-
31-
2. **`<defaultCache>`**: 设置默认缓存的属性。这些属性将应用于未单独配置的所有缓存。
32-
- **`eternal`**: 设置为 `false` 表示缓存不是永久的,可以过期。
33-
- **`maxElementsInMemory`**: 内存中可以存储的最大元素数量。
34-
- **`overflowToDisk`**: 当内存中的元素数量超过最大值时,是否溢出到磁盘。
35-
- **`diskPersistent`**: 是否在 JVM 重启之间持久化到磁盘。
36-
- **`timeToIdleSeconds`**: 元素最后一次被访问后多久会变成空闲状态。
37-
- **`timeToLiveSeconds`**: 元素从创建或最后一次更新后多久会过期。
38-
- **`memoryStoreEvictionPolicy`**: 当内存达到最大值时,移除元素的策略(例如,LRU 表示最近最少使用)。
39-
40-
ehcache.xml 配置文件内容如下
41-
42-
```
43-
<?xml version="1.0" encoding="UTF-8"?>
44-
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
45-
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
46-
47-
<diskStore path="java.io.tmpdir/EhCache" />
48-
49-
<defaultCache eternal="false" maxElementsInMemory="10000" overflowToDisk="false" diskPersistent="false"
50-
timeToIdleSeconds="1800" timeToLiveSeconds="259200" memoryStoreEvictionPolicy="LRU" />
51-
</ehcache>
52-
```
53-
54-
### EhCachePluginConfig 配置类
55-
56-
这个类是一个配置类,用于初始化和配置 EhCache 插件。它通过 @AConfiguration 注解标记为配置类。类中的方法 ehCachePlugin 通过 @ABean 注解标记为 Bean 方法,框架启动时会执行该方法并将返回值放到 bean 容器中。在这个方法中,创建了一个 Plugin 实例并启动它。destroyMethod 指定在服务关闭时将会调用该方法,关闭该插件
57-
58-
```
59-
package com.enoleap.manglang.pen.api.server.config;
60-
61-
import com.litongjava.jfinal.aop.annotation.ABean;
62-
import com.litongjava.jfinal.aop.annotation.AConfiguration;
63-
import com.litongjava.jfinal.plugin.ehcache.EhCachePlugin;
64-
65-
@AConfiguration
66-
public class EhCachePluginConfig {
67-
68-
@ABean(destroyMethod = "stop")
69-
public EhCachePlugin ehCachePlugin() {
70-
EhCachePlugin ehCachePlugin = new EhCachePlugin();
71-
ehCachePlugin.start();
72-
return ehCachePlugin;
73-
}
74-
}
75-
```
76-
77-
如果不想将 EhCachePlugin 放入 Aop 容器,你可以使用下面的配置类
78-
79-
```
80-
package com.enoleap.manglang.pen.api.server.config;
81-
82-
import com.litongjava.jfinal.aop.annotation.AConfiguration;
83-
import com.litongjava.jfinal.aop.annotation.AInitialization;
84-
import com.litongjava.jfinal.plugin.ehcache.EhCachePlugin;
85-
import com.litongjava.tio.boot.server.TioBootServer;
86-
87-
@AConfiguration
88-
public class EhCachePluginConfig {
89-
90-
@AInitialization
91-
public EhCachePlugin ehCachePlugin() {
92-
EhCachePlugin ehCachePlugin = new EhCachePlugin();
93-
ehCachePlugin.start();
94-
TioBootServer.addDestroyMethod(ehCachePlugin::stop);
95-
return ehCachePlugin;
96-
}
97-
}
98-
```
99-
100-
### 控制器
101-
102-
1. **EhCacheTestController**:
103-
104-
- 这个控制器包含一个方法 `test01`,用于测试将数据添加到 EhCache 缓存中并从中检索数据。
105-
- 在这个方法中,首先尝试从缓存中获取一个键值。如果不存在,它将计算一个新值并将其存储在缓存中。
106-
- 这个控制器演示了如何使用 Ehcache 存储和检索简单的键值对。
107-
108-
2. **EhCacheController**:
109-
- 这个控制器包含多个方法,用于与 Ehcache 进行更复杂的交互。
110-
- 方法如 `getCacheNames``getAllCacheValue` 用于检索缓存中的信息,例如缓存名称或所有缓存的值。
111-
- 其他方法允许按名称检索特定缓存的值,或者根据缓存名称和键检索特定的值。
112-
- 这个控制器提供了更深入的视图,展示了如何管理和检查 Ehcache 中的数据。
113-
114-
```
115-
package com.litongjava.tio.web.hello.AController;
116-
117-
import com.litongjava.jfinal.plugin.ehcache.CacheKit;
118-
import com.litongjava.tio.http.server.annotation.RequestPath;
119-
120-
import lombok.extern.slf4j.Slf4j;
121-
122-
@Slf4j
123-
@RequestPath("/ecache/test")
124-
public class EhCacheTestController {
125-
126-
public String test01() {
127-
String cacheName = "student";
128-
String cacheKey = "litong";
129-
130-
String cacheData = CacheKit.get(cacheName, cacheKey);
131-
132-
if (cacheData == null) {
133-
String result = "001";
134-
log.info("计算新的值");
135-
CacheKit.put(cacheName, cacheKey, result);
136-
}
137-
138-
return cacheData;
139-
}
140-
141-
}
142-
143-
```
144-
145-
访问测试 http://localhost/ecache/test/test01
146-
147-
```
148-
package com.litongjava.tio.web.hello.AController;
149-
150-
import java.util.HashMap;
151-
import java.util.List;
152-
import java.util.Map;
153-
154-
import com.litongjava.jfinal.plugin.ehcache.CacheKit;
155-
import com.litongjava.tio.http.server.annotation.RequestPath;
156-
157-
import net.sf.ehcache.Cache;
158-
import net.sf.ehcache.CacheManager;
159-
import net.sf.ehcache.Element;
160-
161-
@RequestPath("/ecache")
162-
public class EhCacheController {
163-
public String[] getCacheNames() {
164-
String[] cacheNames = CacheKit.getCacheManager().getCacheNames();
165-
return cacheNames;
166-
}
167-
168-
public Map<String, Map<String, Object>> getAllCacheValue() {
169-
CacheManager cacheManager = CacheKit.getCacheManager();
170-
String[] cacheNames = cacheManager.getCacheNames();
171-
Map<String, Map<String, Object>> retval = new HashMap<>(cacheNames.length);
172-
for (String name : cacheNames) {
173-
Map<String, Object> map = cacheToMap(cacheManager, name);
174-
retval.put(name, map);
175-
}
176-
return retval;
177-
178-
}
179-
180-
public Map<String, Object> getCacheValueByCacheName(String cacheName) {
181-
CacheManager cacheManager = CacheKit.getCacheManager();
182-
Map<String, Object> retval = cacheToMap(cacheManager, cacheName);
183-
return retval;
184-
}
185-
186-
public Object getCacheValueByCacheNameAndCacheKey(String cacheName, String key) {
187-
Object object = CacheKit.get(cacheName, key);
188-
return object;
189-
}
190-
191-
private Map<String, Object> cacheToMap(CacheManager cacheManager, String name) {
192-
Cache cache = cacheManager.getCache(name);
193-
@SuppressWarnings("unchecked")
194-
List<String> keys = cache.getKeys();
195-
Map<String, Object> map = new HashMap<>(keys.size());
196-
for (String key : keys) {
197-
Element element = cache.get(key);
198-
Object value = element.getObjectValue();
199-
map.put(key, value);
200-
}
201-
return map;
202-
}
203-
}
204-
```
205-
206-
访问测试
207-
http://localhost/ecache/getCacheNames
208-
http://localhost/ecache/getAllCacheValue
3+
jfinal-plugins 是笔者为 tio-boot 开发的插件集合

0 commit comments

Comments
 (0)