Skip to content

Commit

Permalink
ehcache 缓存key常量化
Browse files Browse the repository at this point in the history
  • Loading branch information
chickengone committed Sep 9, 2022
1 parent 97aa453 commit db41b92
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 42 deletions.
2 changes: 1 addition & 1 deletion dbapi-assembly/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.gitee.freakchicken.dbapi</groupId>
<artifactId>dbapi</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion dbapi-cluster-apiServer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>dbapi</artifactId>
<groupId>com.gitee.freakchicken.dbapi</groupId>
<version>3.1.1</version>
<version>3.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion dbapi-cluster-gateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>dbapi</artifactId>
<groupId>com.gitee.freakchicken.dbapi</groupId>
<version>3.1.1</version>
<version>3.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion dbapi-cluster-manager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>dbapi</artifactId>
<groupId>com.gitee.freakchicken.dbapi</groupId>
<version>3.1.1</version>
<version>3.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion dbapi-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.gitee.freakchicken.dbapi</groupId>
<artifactId>dbapi</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dbapi-common</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion dbapi-controller/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>dbapi</artifactId>
<groupId>com.gitee.freakchicken.dbapi</groupId>
<version>3.1.1</version>
<version>3.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion dbapi-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.gitee.freakchicken.dbapi</groupId>
<artifactId>dbapi</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion dbapi-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>dbapi</artifactId>
<groupId>com.gitee.freakchicken.dbapi</groupId>
<version>3.1.1</version>
<version>3.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
} else {
String appId = tokenService.verifyToken(tokenStr);
if (appId == null) {
log.error("token[{}] matched no appid", tokenStr);
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.getWriter().append(JSON.toJSONString(ResponseDto.fail("Token Invalid!")));
return;
} else {
List<String> authGroups = appService.getAuthGroups(appId);
if (!authGroups.contains(config.getGroupId())) {
log.error("token[{}] matched appid[{}], but appid not authorized", tokenStr, appId);
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.getWriter().append(JSON.toJSONString(ResponseDto.fail("Token Invalid!")));
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import com.gitee.freakchicken.dbapi.basic.dao.AppInfoMapper;
import com.gitee.freakchicken.dbapi.basic.domain.ApiAuth;
import com.gitee.freakchicken.dbapi.basic.domain.AppInfo;
import com.gitee.freakchicken.dbapi.basic.util.Constants;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
Expand All @@ -26,6 +28,9 @@ public class AppService {
@Autowired
private MetaDataCacheManager metaDataCacheManager;

@Autowired
CacheManager cacheManager;

@Transactional
public AppInfo add(AppInfo app) {
app.setId(RandomStringUtils.random(16, true, true));
Expand Down Expand Up @@ -55,11 +60,12 @@ public List<AppInfo> getAll() {
@Transactional
public void delete(String appid) {
appInfoMapper.deleteById(appid);
cacheManager.getCache(Constants.EHCACHE_APP_AUTH_GROUPS).evict(appid);
}

@Transactional
@CacheEvict(value = "app_AuthGroups", key = "#appId")
public void auth(String appId, String groupIds) {
cacheManager.getCache(Constants.EHCACHE_APP_AUTH_GROUPS).evictIfPresent(appId);
apiAuthMapper.deleteByAppId(appId);
if (StringUtils.isNoneBlank(groupIds)) {
String[] split = groupIds.split(",");
Expand All @@ -73,9 +79,9 @@ public void auth(String appId, String groupIds) {
metaDataCacheManager.cleanTokenAuthMetaCacheIfCluster(appId);
}

@Cacheable(value = "app_AuthGroups", key = "#appId", unless = "#result == null")
public List<String> getAuthGroups(String appId) {
List<String> list = apiAuthMapper.selectByAppId(appId);
cacheManager.getCache(Constants.EHCACHE_APP_AUTH_GROUPS).putIfAbsent(appId, list);
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import com.gitee.freakchicken.dbapi.basic.dao.AppInfoMapper;
import com.gitee.freakchicken.dbapi.basic.domain.AppInfo;
import com.gitee.freakchicken.dbapi.basic.domain.AppToken;
import com.gitee.freakchicken.dbapi.basic.util.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AppTokenService {
@Autowired
private AppInfoMapper appInfoMapper;
Expand Down Expand Up @@ -43,16 +46,17 @@ else if (appInfo.getExpireTime() > 0) {
appToken.setToken(token);
appToken.setAppId(appId);


//最新token存入缓存
cacheManager.getCache("token_app").putIfAbsent(token, appToken);
cacheManager.getCache(Constants.EHCACHE_TOKEN_APP).putIfAbsent(token, appToken);

//clean old token
String oldToken = cacheManager.getCache("app_token").get(appId, String.class);
String oldToken = cacheManager.getCache(Constants.EHCACHE_APP_TOKEN).get(appId, String.class);
if (oldToken != null)
cacheManager.getCache("token_app").evict(oldToken);
cacheManager.getCache(Constants.EHCACHE_TOKEN_APP).evict(oldToken);

//appid和最新token关系记录下来
cacheManager.getCache("app_token").putIfAbsent(appId, token);
cacheManager.getCache(Constants.EHCACHE_APP_TOKEN).putIfAbsent(appId, token);
return appToken;
}
}
Expand All @@ -64,14 +68,14 @@ else if (appInfo.getExpireTime() > 0) {
* @return
*/
public String verifyToken(String token) {
AppToken appToken = cacheManager.getCache("token_app").get(token, AppToken.class);
AppToken appToken = cacheManager.getCache(Constants.EHCACHE_TOKEN_APP).get(token, AppToken.class);
if (appToken == null) {
return null;
} else {
Long expireTime = appToken.getExpireTime();
// 单次失效
if (expireTime == 0) {
cacheManager.getCache("token_app").evict(token);
cacheManager.getCache(Constants.EHCACHE_TOKEN_APP).evict(token);
return appToken.getAppId();
}
// 永久有效
Expand All @@ -84,7 +88,8 @@ else if (expireTime > 0) {
return appToken.getAppId();
} else {
// token已经过期就清除
cacheManager.getCache("token_app").evict(token);
cacheManager.getCache(Constants.EHCACHE_TOKEN_APP).evict(token);
log.error("token [{}] expired!", token);
throw new RuntimeException("token expired!");
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.gitee.freakchicken.dbapi.basic.service.*;

import com.gitee.freakchicken.dbapi.basic.domain.DataSource;
import com.gitee.freakchicken.dbapi.basic.service.ApiConfigService;
import com.gitee.freakchicken.dbapi.basic.service.ApiService;
import com.gitee.freakchicken.dbapi.basic.service.DataSourceService;
import com.gitee.freakchicken.dbapi.basic.service.IPService;
import com.gitee.freakchicken.dbapi.basic.util.JdbcUtil;
import com.gitee.freakchicken.dbapi.basic.util.PoolManager;
import com.gitee.freakchicken.dbapi.basic.util.SqlEngineUtil;
import com.gitee.freakchicken.dbapi.basic.util.ThreadUtils;
import com.gitee.freakchicken.dbapi.common.ApiConfig;
import com.gitee.freakchicken.dbapi.common.ApiSql;
import com.gitee.freakchicken.dbapi.common.ResponseDto;
import com.gitee.freakchicken.dbapi.basic.domain.DataSource;
import com.gitee.freakchicken.dbapi.plugin.AlarmPlugin;
import com.gitee.freakchicken.dbapi.plugin.CachePlugin;
import com.gitee.freakchicken.dbapi.plugin.PluginManager;
import com.gitee.freakchicken.dbapi.plugin.TransformPlugin;

import com.gitee.freakchicken.dbapi.basic.util.JdbcUtil;
import com.gitee.freakchicken.dbapi.basic.util.SqlEngineUtil;
import com.github.freakchick.orange.SqlMeta;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.entity.ContentType;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
Expand All @@ -39,10 +38,9 @@
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
Expand All @@ -57,8 +55,6 @@ public class APIServlet extends HttpServlet {

@Autowired
IPService ipService;
// @Autowired
// MailService mailService;

@Value("${dbapi.api.context}")
String apiContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.gitee.freakchicken.dbapi.basic.util;

public class Constants {

public static String EHCACHE_APP_TOKEN = "app_token";
public static String EHCACHE_TOKEN_APP = "token_app";
public static String EHCACHE_APP_AUTH_GROUPS = "app_AuthGroups";

}
2 changes: 1 addition & 1 deletion dbapi-service/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=3.1.1
version=3.1.2

server.servlet.encoding.force=true
server.servlet.encoding.charset=UTF-8
Expand Down
14 changes: 7 additions & 7 deletions dbapi-service/src/main/resources/ehcache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="3600"
memoryStoreEvictionPolicy="LRU"/>

<!--token缓存1小时-->
<cache name="token_app" eternal="false" maxElementsInMemory="10000"
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="3600"
<!--token_app永久缓存-->
<cache name="token_app" eternal="true" maxElementsInMemory="10000"
overflowToDisk="false" diskPersistent="false"
memoryStoreEvictionPolicy="LRU"/>

<!--token缓存1小时-->
<cache name="app_token" eternal="false" maxElementsInMemory="10000"
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="3600"
<!--app_token永久缓存-->
<cache name="app_token" eternal="true" maxElementsInMemory="10000"
overflowToDisk="false" diskPersistent="false"
memoryStoreEvictionPolicy="LRU"/>

<!--token_AuthGroups缓存1小时-->
<!--app_AuthGroups缓存1小时-->
<cache name="app_AuthGroups" eternal="false" maxElementsInMemory="10000"
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="3600"
memoryStoreEvictionPolicy="LRU"/>
Expand Down
2 changes: 1 addition & 1 deletion dbapi-standalone/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.gitee.freakchicken.dbapi</groupId>
<artifactId>dbapi</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion dbapi-ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.gitee.freakchicken.dbapi</groupId>
<artifactId>dbapi</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.gitee.freakchicken.dbapi</groupId>
<artifactId>dbapi</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>

<name>DBApi</name>
<description>build http api from sql without other code</description>
Expand Down

0 comments on commit db41b92

Please sign in to comment.