187 lines
6.3 KiB
Java
187 lines
6.3 KiB
Java
/* */ package kong.unirest.cosmetics;
|
|
/* */
|
|
/* */ import java.net.http.WebSocket;
|
|
/* */ import java.time.Instant;
|
|
/* */ import java.time.temporal.ChronoUnit;
|
|
/* */ import java.util.LinkedHashMap;
|
|
/* */ import java.util.Map;
|
|
/* */ import java.util.Objects;
|
|
/* */ import java.util.concurrent.CompletableFuture;
|
|
/* */ import java.util.function.Function;
|
|
/* */ import java.util.function.Supplier;
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */ class CacheManager
|
|
/* */ {
|
|
/* 40 */ private final CacheWrapper wrapper = new CacheWrapper();
|
|
/* 41 */ private final CacheWrapper asyncWrapper = new CacheWrapper();
|
|
/* */
|
|
/* */ private final Cache backingCache;
|
|
/* */ private final Cache.KeyGenerator keyGen;
|
|
/* */ private Client originalClient;
|
|
/* */
|
|
/* */ public CacheManager() {
|
|
/* 48 */ this(100, 0L, HashKey::new);
|
|
/* */ }
|
|
/* */
|
|
/* */ public CacheManager(int depth, long ttl, Cache.KeyGenerator keyGenerator) {
|
|
/* 52 */ this(new CacheMap(depth, ttl), keyGenerator);
|
|
/* */ }
|
|
/* */
|
|
/* */ public CacheManager(Cache backing, Cache.KeyGenerator keyGenerator) {
|
|
/* 56 */ this.backingCache = backing;
|
|
/* 57 */ if (keyGenerator != null) {
|
|
/* 58 */ this.keyGen = keyGenerator;
|
|
/* */ } else {
|
|
/* 60 */ this.keyGen = HashKey::new;
|
|
/* */ }
|
|
/* */ }
|
|
/* */
|
|
/* */ Client wrap(Client client) {
|
|
/* 65 */ this.originalClient = client;
|
|
/* 66 */ return this.wrapper;
|
|
/* */ }
|
|
/* */
|
|
/* */ Client wrapAsync(Client client) {
|
|
/* 70 */ this.originalClient = client;
|
|
/* 71 */ return this.asyncWrapper;
|
|
/* */ }
|
|
/* */
|
|
/* */ private <T> Cache.Key getHash(HttpRequest request, Boolean isAsync, Class<?> responseType) {
|
|
/* 75 */ return this.keyGen.apply(request, isAsync, responseType);
|
|
/* */ }
|
|
/* */
|
|
/* */ private static class HashKey implements Cache.Key {
|
|
/* */ private final int hash;
|
|
/* */ private final Instant time;
|
|
/* */
|
|
/* */ HashKey(HttpRequest request, Boolean isAsync, Class<?> responseType) {
|
|
/* 83 */ this(Objects.hash(new Object[] { Integer.valueOf(request.hashCode()), isAsync, responseType }, ), request
|
|
/* 84 */ .getCreationTime());
|
|
/* */ }
|
|
/* */
|
|
/* */ HashKey(int hash, Instant time) {
|
|
/* 88 */ this.hash = hash;
|
|
/* 89 */ this.time = time;
|
|
/* */ }
|
|
/* */
|
|
/* */
|
|
/* */ public boolean equals(Object o) {
|
|
/* 94 */ if (this == o) {
|
|
/* 95 */ return true;
|
|
/* */ }
|
|
/* 97 */ if (o == null || getClass() != o.getClass()) {
|
|
/* 98 */ return false;
|
|
/* */ }
|
|
/* 100 */ HashKey key = (HashKey)o;
|
|
/* 101 */ return (this.hash == key.hash);
|
|
/* */ }
|
|
/* */
|
|
/* */
|
|
/* */ public int hashCode() {
|
|
/* 106 */ return this.hash;
|
|
/* */ }
|
|
/* */
|
|
/* */
|
|
/* */ public Instant getTime() {
|
|
/* 111 */ return this.time;
|
|
/* */ }
|
|
/* */ }
|
|
/* */
|
|
/* */ private class CacheWrapper
|
|
/* */ implements Client
|
|
/* */ {
|
|
/* */ public Object getClient() {
|
|
/* 119 */ return CacheManager.this.originalClient.getClient();
|
|
/* */ }
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */ public <T> HttpResponse<T> request(HttpRequest request, Function<RawResponse, HttpResponse<T>> transformer, Class<?> responseType) {
|
|
/* 127 */ Cache.Key hash = CacheManager.this.getHash(request, Boolean.valueOf(false), responseType);
|
|
/* 128 */ return CacheManager.this.backingCache.get(hash, () -> CacheManager.this.originalClient.request(request, transformer, responseType));
|
|
/* */ }
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */ public <T> CompletableFuture<HttpResponse<T>> request(HttpRequest request, Function<RawResponse, HttpResponse<T>> transformer, CompletableFuture<HttpResponse<T>> callback, Class<?> responseType) {
|
|
/* 137 */ Cache.Key key = CacheManager.this.getHash(request, Boolean.valueOf(true), responseType);
|
|
/* 138 */ return CacheManager.this.backingCache.getAsync(key, () -> CacheManager.this.originalClient.request(request, transformer, callback, responseType));
|
|
/* */ }
|
|
/* */
|
|
/* */
|
|
/* */
|
|
/* */ public WebSocketResponse websocket(WebSocketRequest request, WebSocket.Listener listener) {
|
|
/* 144 */ return CacheManager.this.originalClient.websocket(request, listener);
|
|
/* */ }
|
|
/* */ }
|
|
/* */
|
|
/* */ private static class CacheMap extends LinkedHashMap<Cache.Key, Object> implements Cache {
|
|
/* */ private final int maxSize;
|
|
/* */ private long ttl;
|
|
/* */
|
|
/* */ CacheMap(int maxSize, long ttl) {
|
|
/* 153 */ this.maxSize = maxSize;
|
|
/* 154 */ this.ttl = ttl;
|
|
/* */ }
|
|
/* */
|
|
/* */
|
|
/* */ public <T> HttpResponse<T> get(Cache.Key key, Supplier<HttpResponse<T>> fetcher) {
|
|
/* 159 */ clearOld();
|
|
/* 160 */ return (HttpResponse<T>)computeIfAbsent((K)key, k -> fetcher.get());
|
|
/* */ }
|
|
/* */
|
|
/* */
|
|
/* */ public <T> CompletableFuture getAsync(Cache.Key key, Supplier<CompletableFuture<HttpResponse<T>>> fetcher) {
|
|
/* 165 */ clearOld();
|
|
/* 166 */ return (CompletableFuture)computeIfAbsent((K)key, k -> fetcher.get());
|
|
/* */ }
|
|
/* */
|
|
/* */ private void clearOld() {
|
|
/* 170 */ if (this.ttl > 0L) {
|
|
/* 171 */ Instant now = Util.now();
|
|
/* 172 */ keySet().removeIf(k -> (ChronoUnit.MILLIS.between(k.getTime(), now) > this.ttl));
|
|
/* */ }
|
|
/* */ }
|
|
/* */
|
|
/* */
|
|
/* */ protected boolean removeEldestEntry(Map.Entry<Cache.Key, Object> eldest) {
|
|
/* 178 */ return (size() > this.maxSize);
|
|
/* */ }
|
|
/* */ }
|
|
/* */ }
|
|
|
|
|
|
/* Location: C:\Users\Xiao\Downloads\MinehutCosmetics.jar!\kon\\unirest\cosmetics\CacheManager.class
|
|
* Java compiler version: 11 (55.0)
|
|
* JD-Core Version: 1.1.3
|
|
*/ |