|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Institute of Software Chinese Academy of Sciences (ISCAS) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package xyz.eulix.platform.services.cache; |
| 18 | + |
| 19 | +import io.quarkus.redis.client.RedisClient; |
| 20 | +import io.vertx.redis.client.Response; |
| 21 | +import org.jboss.logging.Logger; |
| 22 | +import xyz.eulix.platform.common.support.CommonUtils; |
| 23 | +import xyz.eulix.platform.common.support.log.Logged; |
| 24 | +import xyz.eulix.platform.common.support.serialization.OperationUtils; |
| 25 | +import xyz.eulix.platform.services.config.ApplicationProperties; |
| 26 | + |
| 27 | +import javax.enterprise.context.ApplicationScoped; |
| 28 | +import javax.inject.Inject; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.List; |
| 32 | +import java.util.stream.Collectors; |
| 33 | + |
| 34 | +/** |
| 35 | + * Network Server Route 缓存客户端(Redis) |
| 36 | + * |
| 37 | + * For more information: |
| 38 | + * <a href="https://quarkus.io/guides/redis">https://quarkus.io/guides/redis</a>. |
| 39 | + * |
| 40 | + */ |
| 41 | +@ApplicationScoped |
| 42 | +public class GTRClient { |
| 43 | + private static final Logger LOG = Logger.getLogger("app.log"); |
| 44 | + |
| 45 | + public static final String SEPARATOR = ":"; |
| 46 | + |
| 47 | + public static final String GTR_PREV = "GTR:"; |
| 48 | + |
| 49 | + public static final String GTR_PREV_CLIENTS = "SpaceUUIDs:"; |
| 50 | + |
| 51 | + public static final String GTR_PREV_APP_TOKENS = "AppDomains:"; |
| 52 | + |
| 53 | + @Inject |
| 54 | + RedisClient redisClient; |
| 55 | + |
| 56 | + @Inject |
| 57 | + OperationUtils operationUtils; |
| 58 | + |
| 59 | + @Inject |
| 60 | + ApplicationProperties properties; |
| 61 | + |
| 62 | + public GTRouteBasic getGTRouteBasic(String subdomain) { |
| 63 | + String key = GTR_PREV + subdomain; |
| 64 | + Response response = redisClient.get(key); |
| 65 | + GTRouteBasic gtRouteBasic = new GTRouteBasic(subdomain, response != null ? getNetworkBasic(response.toString()) : null); |
| 66 | + LOG.debugv("get GTRouteBasic success, key:{0}, value:{1}", key, gtRouteBasic.getNetworkBasic()); |
| 67 | + return gtRouteBasic; |
| 68 | + } |
| 69 | + |
| 70 | + public NetworkBasic getNetworkBasic(String networkBasicStr) { |
| 71 | + return operationUtils.jsonToObject(networkBasicStr, NetworkBasic.class); |
| 72 | + } |
| 73 | + |
| 74 | + public void setGTRouteBasic(GTRouteBasic gtRouteBasic) { |
| 75 | + String key = GTR_PREV + gtRouteBasic.getSubdomain(); |
| 76 | + String value = operationUtils.objectToJson(gtRouteBasic.getNetworkBasic()); |
| 77 | + redisClient.set(Arrays.asList(key, value)); |
| 78 | + LOG.debugv("set GTRouteBasic success, key:{0}, value:{1}", key, value); |
| 79 | + } |
| 80 | + |
| 81 | + public void setGTRouteClients(GTRouteClients gtRouteClients) { |
| 82 | + String key = GTR_PREV_CLIENTS + gtRouteClients.getBoxUUID() + SEPARATOR + gtRouteClients.getUserId(); |
| 83 | + if (CommonUtils.isNullOrEmpty(gtRouteClients.getClientUUIDs())) { |
| 84 | + LOG.warnv("set GTRouteClients fail due to clientUUIDs is empty, key:{0}", key); |
| 85 | + return; |
| 86 | + } |
| 87 | + List<String> lists = new ArrayList<>(); |
| 88 | + lists.add(key); |
| 89 | + lists.addAll(gtRouteClients.getClientUUIDs()); |
| 90 | + redisClient.sadd(lists); |
| 91 | + LOG.debugv("set GTRouteClients success, key:{0}, value:{1}", key, gtRouteClients.getClientUUIDs()); |
| 92 | + } |
| 93 | + |
| 94 | + @Logged |
| 95 | + public void setRedirect(String subdomain, String serverAddr, String clientId, String newUserDomain, NSRRedirectStateEnum redirectState, |
| 96 | + String boxUUID, String userId) { |
| 97 | + NetworkBasic networkBasic = new NetworkBasic(serverAddr, clientId, newUserDomain, redirectState.getState(), boxUUID, userId); |
| 98 | + GTRouteBasic gtRouteBasic = new GTRouteBasic(subdomain, networkBasic); |
| 99 | + setGTRouteBasic(gtRouteBasic); |
| 100 | + } |
| 101 | + |
| 102 | + public void addClientUUID(String boxUUID, String userId, String clientUUID) { |
| 103 | + String key = GTR_PREV_CLIENTS + boxUUID + SEPARATOR + userId; |
| 104 | + List<String> lists = new ArrayList<>(); |
| 105 | + lists.add(key); |
| 106 | + lists.add(clientUUID); |
| 107 | + redisClient.sadd(lists); |
| 108 | + redisClient.persist(key); |
| 109 | + LOG.debugv("add GTRouteClient success, key:{0}, value:{1}", key, clientUUID); |
| 110 | + } |
| 111 | + |
| 112 | + public void removeClientUUID(String boxUUID, String userId, String clientUUID){ |
| 113 | + String key = GTR_PREV_CLIENTS + boxUUID + SEPARATOR + userId; |
| 114 | + List<String> lists = new ArrayList<>(); |
| 115 | + lists.add(key); |
| 116 | + lists.add(clientUUID); |
| 117 | + redisClient.srem(lists); |
| 118 | + redisClient.persist(key); |
| 119 | + LOG.debugv("remove GTRouteClient success, key:{0}, value:{1}", key, clientUUID); |
| 120 | + } |
| 121 | + |
| 122 | + public void addAppToken(String boxUUID, String appToken) { |
| 123 | + String key = GTR_PREV_APP_TOKENS + boxUUID; |
| 124 | + List<String> lists = new ArrayList<>(); |
| 125 | + lists.add(key); |
| 126 | + lists.add(appToken); |
| 127 | + redisClient.sadd(lists); |
| 128 | + redisClient.persist(key); |
| 129 | + LOG.debugv("add GTRouteAppToken success, key:{0}, value:{1}", key, appToken); |
| 130 | + } |
| 131 | + |
| 132 | + public void removeAppToken(String boxUUID, String appToken){ |
| 133 | + String key = GTR_PREV_APP_TOKENS + boxUUID; |
| 134 | + List<String> lists = new ArrayList<>(); |
| 135 | + lists.add(key); |
| 136 | + lists.add(appToken); |
| 137 | + redisClient.srem(lists); |
| 138 | + redisClient.persist(key); |
| 139 | + LOG.debugv("remove GTRouteAppToken success, key:{0}, value:{1}", key, appToken); |
| 140 | + } |
| 141 | + |
| 142 | + public void expireGTRouteBasic(String subdomain, String expireSeconds) { |
| 143 | + String key = GTR_PREV + subdomain; |
| 144 | + redisClient.expire(key, expireSeconds); |
| 145 | + LOG.debugv("expire GTRouteBasic success, key:{0}, expire time:{1}s", key, expireSeconds); |
| 146 | + } |
| 147 | + |
| 148 | + public void expireGTRouteClients(String boxUUID, String userId, String expireSeconds) { |
| 149 | + String key = GTR_PREV_CLIENTS + boxUUID + SEPARATOR + userId; |
| 150 | + redisClient.expire(key, expireSeconds); |
| 151 | + LOG.debugv("expire GTRouteClients success, key:{0}, expire time:{1}s", key, expireSeconds); |
| 152 | + } |
| 153 | + |
| 154 | + public void expireGTRouteAppTokens(String boxUUID, String expireSeconds) { |
| 155 | + String key = GTR_PREV_APP_TOKENS + boxUUID; |
| 156 | + redisClient.expire(key, expireSeconds); |
| 157 | + LOG.debugv("expire GTRouteAppTokens success, key:{0}, expire time:{1}s", key, expireSeconds); |
| 158 | + } |
| 159 | + |
| 160 | + public void clearGTRouteBasic(List<String> subdomains) { |
| 161 | + if (subdomains.isEmpty()) { |
| 162 | + LOG.debugv("subdomains is empty"); |
| 163 | + return; |
| 164 | + } |
| 165 | + List<String> keys = subdomains.stream().map(subdomain -> GTR_PREV + subdomain).collect(Collectors.toList()); |
| 166 | + Integer count = redisClient.del(keys).toInteger(); |
| 167 | + LOG.debugv("clear NSRouteBasic success, keys:{0}, count:{1}", keys, count); |
| 168 | + } |
| 169 | + |
| 170 | + public void clearGTRouteClients(String boxUUID, String userId) { |
| 171 | + String key = GTR_PREV_CLIENTS + boxUUID + SEPARATOR + userId; |
| 172 | + Integer count = redisClient.del(Arrays.asList(key)).toInteger(); |
| 173 | + LOG.debugv("clear NSRouteClients success, key:{0}, count:{1}", key, count); |
| 174 | + } |
| 175 | + |
| 176 | + public void clearNSRouteAppTokens(List<String> boxUUIDs) { |
| 177 | + if (boxUUIDs.isEmpty()) { |
| 178 | + LOG.debugv("boxUUIDs is empty"); |
| 179 | + return; |
| 180 | + } |
| 181 | + List<String> keys = boxUUIDs.stream().map(boxUUID -> GTR_PREV_APP_TOKENS + boxUUID).collect(Collectors.toList()); |
| 182 | + Integer count = redisClient.del(keys).toInteger(); |
| 183 | + LOG.debugv("clear NSRouteAppTokens success, keys:{0}, count:{1}", keys, count); |
| 184 | + } |
| 185 | +} |
0 commit comments