66import com .codingapi .springboot .script .temp .TempGroovyScriptContext ;
77import lombok .Getter ;
88
9- import java .util .HashMap ;
10- import java .util .LinkedHashMap ;
119import java .util .List ;
12- import java .util .Map ;
1310
1411/**
1512 * 脚本数据缓存上下文管理对象
13+ * 缓存容器支持替换(如 Redis 共享缓存实现),默认为进程内 LRU 缓存
1614 */
1715public class GroovyScriptCacheContext {
1816
1917 @ Getter
2018 private final static GroovyScriptCacheContext instance = new GroovyScriptCacheContext ();
2119
22- private final Map <String , GroovyScript > cache ;
23-
24- private final static int MAX_CACHE_SIZE = 10 * 1024 ;
20+ private GroovyScriptCache cache = new DefaultGroovyScriptCache ();
2521
2622 private GroovyScriptCacheContext () {
27- this .cache = new LinkedHashMap <>(16 , 0.75f , true ) {
28- @ Override
29- protected boolean removeEldestEntry (Map .Entry <String , GroovyScript > eldest ) {
30- return size () > MAX_CACHE_SIZE ;
31- }
32- };
23+ }
24+
25+ /**
26+ * 替换缓存容器实现,入参为 null 时回退为默认 LRU 实现
27+ *
28+ * @param cache 缓存容器实现
29+ */
30+ public void setCache (GroovyScriptCache cache ) {
31+ this .cache = (cache != null ) ? cache : new DefaultGroovyScriptCache ();
3332 }
3433
3534 /**
@@ -38,9 +37,7 @@ protected boolean removeEldestEntry(Map.Entry<String, GroovyScript> eldest) {
3837 * @param script 脚本对象
3938 */
4039 public void save (GroovyScript script ) {
41- if (script != null ) {
42- this .cache .put (script .getKey (), script );
43- }
40+ this .cache .save (script );
4441 }
4542
4643
@@ -50,29 +47,24 @@ public void save(GroovyScript script) {
5047 * @param script 脚本对象
5148 */
5249 public void cache (GroovyScript script ) {
53- if (script != null ) {
54- this .cache .put (script .getKey (), script );
55- }
50+ this .cache .save (script );
5651 }
5752
5853
5954 /**
60- * 删除脚本
55+ * 移除脚本缓存(仅作用于本地缓存容器)
6156 *
6257 * @param key 脚本key
6358 */
6459 public void remove (String key ) {
65- GroovyScript groovyScript = this .getGroovyScript (key );
66- if (groovyScript != null ) {
67- this .cache .remove (key );
68- }
60+ this .cache .remove (key );
6961 }
7062
7163 /**
7264 * 获取脚本keys
7365 */
7466 public List <String > keys () {
75- return this .cache .keySet (). stream (). toList ();
67+ return this .cache .keys ();
7668 }
7769
7870 /**
@@ -81,7 +73,7 @@ public List<String> keys() {
8173 * @return 总数量
8274 */
8375 public int count () {
84- return this .cache .size ();
76+ return this .cache .count ();
8577 }
8678
8779 /**
@@ -98,7 +90,7 @@ public GroovyScript getGroovyScript(String key) {
9890 if (groovyScript == null ) {
9991 groovyScript = GroovyScriptRepositoryContext .getInstance ().get (key );
10092 if (groovyScript != null ) {
101- this .cache .put ( key , groovyScript );
93+ this .cache .save ( groovyScript );
10294 }
10395 }
10496 }
@@ -142,7 +134,7 @@ public void setBatchCache(List<GroovyScript> scriptList) {
142134 if (scriptList != null ) {
143135 for (GroovyScript script : scriptList ) {
144136 if (script != null ) {
145- this .cache .put ( script . getKey (), script );
137+ this .cache .save ( script );
146138 }
147139 }
148140 }
@@ -154,8 +146,11 @@ public void setBatchCache(List<GroovyScript> scriptList) {
154146 * @param cache 是否缓存
155147 */
156148 public void compileAll (boolean cache ) {
157- for (GroovyScript script : this .cache .values ()) {
158- script .compile (cache );
149+ for (String key : this .cache .keys ()) {
150+ GroovyScript groovyScript = this .cache .get (key );
151+ if (groovyScript != null ) {
152+ groovyScript .compile (cache );
153+ }
159154 }
160155 }
161156
0 commit comments