Преглед на файлове

新增 jsqlparser 缓存 Caffeine 缓存实现抽象类

hubin преди 1 година
родител
ревизия
c7bd198d82

+ 98 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/parser/cache/AbstractCaffeineJsqlParseCache.java

@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2011-2023, baomidou (jobob@qq.com).
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.baomidou.mybatisplus.extension.parser.cache;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import lombok.Setter;
+import net.sf.jsqlparser.statement.Statement;
+import net.sf.jsqlparser.statement.Statements;
+import org.apache.ibatis.logging.Log;
+import org.apache.ibatis.logging.LogFactory;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Executor;
+import java.util.function.Consumer;
+
+/**
+ * jsqlparser 缓存 Caffeine 缓存实现抽象类
+ *
+ * @author miemie hubin
+ * @since 2023-08-08
+ */
+public abstract class AbstractCaffeineJsqlParseCache implements JsqlParseCache {
+    protected final Log logger = LogFactory.getLog(this.getClass());
+    protected final Cache<String, byte[]> cache;
+    @Setter
+    protected boolean async = false;
+    @Setter
+    protected Executor executor;
+
+    public AbstractCaffeineJsqlParseCache(Cache<String, byte[]> cache) {
+        this.cache = cache;
+    }
+
+    public AbstractCaffeineJsqlParseCache(Consumer<Caffeine<Object, Object>> consumer) {
+        Caffeine<Object, Object> caffeine = Caffeine.newBuilder();
+        consumer.accept(caffeine);
+        this.cache = caffeine.build();
+    }
+
+    @Override
+    public void putStatement(String sql, Statement value) {
+        put(sql, value);
+    }
+
+    @Override
+    public void putStatements(String sql, Statements value) {
+        put(sql, value);
+    }
+
+    @Override
+    public Statement getStatement(String sql) {
+        byte[] bytes = cache.getIfPresent(sql);
+        return null == bytes ? null : (Statement) deserialize(sql, bytes);
+    }
+
+    @Override
+    public Statements getStatements(String sql) {
+        byte[] bytes = cache.getIfPresent(sql);
+        return null == bytes ? null : (Statements) deserialize(sql, bytes);
+    }
+
+    protected void put(String sql, Object value) {
+        if (async) {
+            if (executor != null) {
+                CompletableFuture.runAsync(() -> cache.put(sql, serialize(value)), executor);
+            } else {
+                CompletableFuture.runAsync(() -> cache.put(sql, serialize(value)));
+            }
+        } else {
+            cache.put(sql, serialize(value));
+        }
+    }
+
+    /**
+     * 序列化
+     */
+    public abstract byte[] serialize(Object obj);
+
+    /**
+     * 反序列化
+     */
+    public abstract Object deserialize(String sql, byte[] bytes);
+
+}

+ 2 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/parser/cache/FstFactory.java

@@ -18,6 +18,8 @@ package com.baomidou.mybatisplus.extension.parser.cache;
 import org.nustaq.serialization.FSTConfiguration;
 
 /**
+ * Fst Factory
+ *
  * @author miemie
  * @since 2023-08-06
  */

+ 9 - 57
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/parser/cache/FstSerialCaffeineJsqlParseCache.java

@@ -17,81 +17,33 @@ package com.baomidou.mybatisplus.extension.parser.cache;
 
 import com.github.benmanes.caffeine.cache.Cache;
 import com.github.benmanes.caffeine.cache.Caffeine;
-import lombok.Setter;
-import net.sf.jsqlparser.statement.Statement;
-import net.sf.jsqlparser.statement.Statements;
-import org.apache.ibatis.logging.Log;
-import org.apache.ibatis.logging.LogFactory;
 
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.Executor;
 import java.util.function.Consumer;
 
 /**
+ * jsqlparser 缓存 Fst 序列化 Caffeine 缓存实现
+ *
  * @author miemie
  * @since 2023-08-05
  */
-public class FstSerialCaffeineJsqlParseCache implements JsqlParseCache {
-    protected final Log logger = LogFactory.getLog(this.getClass());
-    private final Cache<String, byte[]> cache;
-    @Setter
-    private boolean async = false;
-    @Setter
-    private Executor executor;
+public class FstSerialCaffeineJsqlParseCache extends AbstractCaffeineJsqlParseCache {
+
 
     public FstSerialCaffeineJsqlParseCache(Cache<String, byte[]> cache) {
-        this.cache = cache;
+        super(cache);
     }
 
     public FstSerialCaffeineJsqlParseCache(Consumer<Caffeine<Object, Object>> consumer) {
-        Caffeine<Object, Object> caffeine = Caffeine.newBuilder();
-        consumer.accept(caffeine);
-        this.cache = caffeine.build();
-    }
-
-    @Override
-    public void putStatement(String sql, Statement value) {
-        put(sql, value);
+        super(consumer);
     }
 
     @Override
-    public void putStatements(String sql, Statements value) {
-        put(sql, value);
-    }
-
-    protected void put(String sql, Object value) {
-        if (async) {
-            if (executor != null) {
-                CompletableFuture.runAsync(() -> cache.put(sql, serialize(value)), executor);
-            } else {
-                CompletableFuture.runAsync(() -> cache.put(sql, serialize(value)));
-            }
-        } else {
-            cache.put(sql, serialize(value));
-        }
-    }
-
-    @Override
-    public Statement getStatement(String sql) {
-        byte[] bytes = cache.getIfPresent(sql);
-        if (bytes != null)
-            return (Statement) deserialize(sql, bytes);
-        return null;
-    }
-
-    @Override
-    public Statements getStatements(String sql) {
-        byte[] bytes = cache.getIfPresent(sql);
-        if (bytes != null)
-            return (Statements) deserialize(sql, bytes);
-        return null;
-    }
-
-    protected byte[] serialize(Object obj) {
+    public byte[] serialize(Object obj) {
         return FstFactory.getDefaultFactory().asByteArray(obj);
     }
 
-    protected Object deserialize(String sql, byte[] bytes) {
+    @Override
+    public Object deserialize(String sql, byte[] bytes) {
         try {
             return FstFactory.getDefaultFactory().asObject(bytes);
         } catch (Exception e) {

+ 9 - 50
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/parser/cache/JdkSerialCaffeineJsqlParseCache.java

@@ -29,70 +29,29 @@ import java.util.concurrent.Executor;
 import java.util.function.Consumer;
 
 /**
+ * jsqlparser 缓存 jdk 序列化 Caffeine 缓存实现
+ *
  * @author miemie
  * @since 2023-08-05
  */
-public class JdkSerialCaffeineJsqlParseCache implements JsqlParseCache {
-    protected final Log logger = LogFactory.getLog(this.getClass());
-    private final Cache<String, byte[]> cache;
-    @Setter
-    private boolean async = false;
-    @Setter
-    private Executor executor;
+public class JdkSerialCaffeineJsqlParseCache extends AbstractCaffeineJsqlParseCache {
+
 
     public JdkSerialCaffeineJsqlParseCache(Cache<String, byte[]> cache) {
-        this.cache = cache;
+        super(cache);
     }
 
     public JdkSerialCaffeineJsqlParseCache(Consumer<Caffeine<Object, Object>> consumer) {
-        Caffeine<Object, Object> caffeine = Caffeine.newBuilder();
-        consumer.accept(caffeine);
-        this.cache = caffeine.build();
-    }
-
-    @Override
-    public void putStatement(String sql, Statement value) {
-        put(sql, value);
+        super(consumer);
     }
 
     @Override
-    public void putStatements(String sql, Statements value) {
-        put(sql, value);
-    }
-
-    protected void put(String sql, Object value) {
-        if (async) {
-            if (executor != null) {
-                CompletableFuture.runAsync(() -> cache.put(sql, serialize(value)), executor);
-            } else {
-                CompletableFuture.runAsync(() -> cache.put(sql, serialize(value)));
-            }
-        } else {
-            cache.put(sql, serialize(value));
-        }
-    }
-
-    @Override
-    public Statement getStatement(String sql) {
-        byte[] bytes = cache.getIfPresent(sql);
-        if (bytes != null)
-            return (Statement) deserialize(sql, bytes);
-        return null;
-    }
-
-    @Override
-    public Statements getStatements(String sql) {
-        byte[] bytes = cache.getIfPresent(sql);
-        if (bytes != null)
-            return (Statements) deserialize(sql, bytes);
-        return null;
-    }
-
-    protected byte[] serialize(Object obj) {
+    public byte[] serialize(Object obj) {
         return SerializationUtils.serialize(obj);
     }
 
-    protected Object deserialize(String sql, byte[] bytes) {
+    @Override
+    public Object deserialize(String sql, byte[] bytes) {
         try {
             return SerializationUtils.deserialize(bytes);
         } catch (Exception e) {

+ 2 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/parser/cache/JsqlParseCache.java

@@ -19,6 +19,8 @@ import net.sf.jsqlparser.statement.Statement;
 import net.sf.jsqlparser.statement.Statements;
 
 /**
+ * jsqlparser 缓存接口
+ *
  * @author miemie
  * @since 2023-08-05
  */