Unverified Commit 7b3f8e27 authored by gaoyan's avatar gaoyan Committed by GitHub

[Optimization] Remove datasource cache with Paimon (#3376)

parent cae01d3b
...@@ -249,7 +249,11 @@ ...@@ -249,7 +249,11 @@
<artifactId>jsqlparser</artifactId> <artifactId>jsqlparser</artifactId>
<version>4.4</version> <version>4.4</version>
</dependency> </dependency>
<!-- for cache-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- test dependencies --> <!-- test dependencies -->
<dependency> <dependency>
<groupId>org.junit.vintage</groupId> <groupId>org.junit.vintage</groupId>
......
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.dinky.configure.cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CacheConfiguration {
@Bean
public CacheManager cacheManager() {
return new PaimonCacheManager();
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.dinky.configure.cache;
import org.dinky.data.constant.PaimonTableConstant;
import org.dinky.data.paimon.CacheData;
import org.dinky.shaded.paimon.data.BinaryString;
import org.dinky.utils.PaimonUtil;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import org.springframework.cache.support.AbstractValueAdaptingCache;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
public class PaimonCache extends AbstractValueAdaptingCache {
private static final Class<CacheData> clazz = CacheData.class;
private final String cacheName;
private static final String TABLE_NAME = PaimonTableConstant.DINKY_CACHE;
/**
* TIMEOUT CACHE
*/
private final cn.hutool.cache.Cache<Object, Object> cache = new TimedCache<>(1000 * 60);
public PaimonCache(String cacheName) {
super(true);
this.cacheName = cacheName;
}
@Override
public String getName() {
return cacheName;
}
@Override
public Object getNativeCache() {
return this;
}
@Override
public <T> T get(Object key, Class<T> type) {
return (T) get(key).get();
}
@Override
protected Object lookup(Object key) {
String strKey = Convert.toStr(key);
Object o = cache.get(strKey);
if (o == null) {
PaimonUtil.createOrGetTable(TABLE_NAME, clazz);
List<CacheData> cacheData = PaimonUtil.batchReadTable(
TABLE_NAME,
clazz,
x -> Arrays.asList(
x.equal(x.indexOf("cache_name"), BinaryString.fromString(cacheName)),
x.equal(x.indexOf("key"), BinaryString.fromString(strKey))));
if (cacheData.isEmpty()) {
return null;
}
return deserialize(cacheData.get(0).getData());
}
return o;
}
@Override
public <T> T get(Object key, Callable<T> valueLoader) {
return (T) get(key).get();
}
@Override
public void put(Object key, Object value) {
String strKey = Convert.toStr(key);
cache.put(strKey, value);
PaimonUtil.createOrGetTable(TABLE_NAME, clazz);
CacheData cacheData = CacheData.builder()
.cacheTime(DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm"))
.cacheName(cacheName)
.key(strKey)
.data(serialize(value))
.build();
PaimonUtil.write(TABLE_NAME, Collections.singletonList(cacheData), clazz);
}
@Override
public void evict(Object key) {
String strKey = Convert.toStr(key);
cache.remove(strKey);
CacheData cacheData =
CacheData.builder().cacheName(cacheName).key(strKey).data("").build();
PaimonUtil.write(TABLE_NAME, Collections.singletonList(cacheData), clazz);
}
@Override
public void clear() {
cache.clear();
PaimonUtil.dropTable(TABLE_NAME);
}
public String serialize(Object object) {
return JSON.toJSONString(object, JSONWriter.Feature.WriteClassName);
}
public Object deserialize(String json) {
return JSON.parseObject(json, Object.class, JSONReader.Feature.SupportAutoType);
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.dinky.configure.cache;
import java.util.Collection;
import java.util.LinkedHashSet;
import org.springframework.cache.Cache;
import org.springframework.cache.support.AbstractCacheManager;
public class PaimonCacheManager extends AbstractCacheManager {
@Override
protected Collection<? extends Cache> loadCaches() {
Collection<Cache> caches = new LinkedHashSet<>();
for (String cacheName : this.getCacheNames()) {
Cache cache = this.getCache(cacheName);
caches.add(cache);
}
return caches;
}
@Override
protected Cache getMissingCache(String name) {
return new PaimonCache(name);
}
}
...@@ -39,7 +39,34 @@ spring: ...@@ -39,7 +39,34 @@ spring:
# circular references allowed # circular references allowed
main: main:
allow-circular-references: true allow-circular-references: true
# 默认使用内存缓存元数据信息,
# dinky支持redis缓存,如有需要请把simple改为redis,并打开下面的redis连接配置
# 子配置项可以按需要打开或自定义配置
cache:
type: simple
# # 如果type配置为redis,则该项可按需配置
# redis:
## 是否缓存空值,保存默认即可
# cache-null-values: false
## 缓存过期时间,24小时
# time-to-live: 86400
# redis:
# host: 119.3.184.208
# port: 6379
# password: woai1998
# database: 10
# jedis:
# pool:
# # 连接池最大连接数(使用负值表示没有限制)
# max-active: 50
# # 连接池最大阻塞等待时间(使用负值表示没有限制)
# max-wait: 3000
# # 连接池中的最大空闲连接数
# max-idle: 20
# # 连接池中的最小空闲连接数
# min-idle: 5
# # 连接超时时间(毫秒)
# timeout: 5000
# file upload config of servlet , the default is 500MB # file upload config of servlet , the default is 500MB
servlet: servlet:
multipart: multipart:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment