Update Upstream

b9f0416 Use a Guava Cache instead of a ThreadLocal (1862)
Dieser Commit ist enthalten in:
NotMyFault 2021-08-02 22:10:21 +02:00
Ursprung a5e84dcfea
Commit 45a8030aeb
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 158F5701A6AAD00C

Datei anzeigen

@ -19,6 +19,9 @@
package com.sk89q.worldedit.session.request; package com.sk89q.worldedit.session.request;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extension.platform.Actor;
@ -27,19 +30,15 @@ import com.sk89q.worldedit.world.World;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Collection; import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/** /**
* Describes the current request using a {@link ThreadLocal}. * Describes the current request
*/ */
public final class Request { public final class Request {
private static final ThreadLocal<Request> threadLocal = ThreadLocal.withInitial(Request::new); private static final LoadingCache<Thread, Request> THREAD_TO_REQUEST = CacheBuilder.newBuilder()
//FAWE start .weakKeys()
// TODO any better way to deal with this? .build(CacheLoader.from(Request::new));
private static final Map<Thread, Request> requests = new ConcurrentHashMap<>();
//FAWE end
@Nullable @Nullable
private World world; private World world;
@ -55,13 +54,12 @@ public final class Request {
private Extent extent; private Extent extent;
//FAWE end //FAWE end
//FAWE start
private Request() { private Request() {
requests.put(Thread.currentThread(), this);
} }
//FAWE start
public static Collection<Request> getAll() { public static Collection<Request> getAll() {
return requests.values(); return THREAD_TO_REQUEST.asMap().values();
} }
//FAWE end //FAWE end
@ -160,7 +158,7 @@ public final class Request {
* @return the current request * @return the current request
*/ */
public static Request request() { public static Request request() {
return threadLocal.get(); return THREAD_TO_REQUEST.getUnchecked(Thread.currentThread());
} }
/** /**
@ -168,10 +166,7 @@ public final class Request {
*/ */
public static void reset() { public static void reset() {
request().invalidate(); request().invalidate();
threadLocal.remove(); THREAD_TO_REQUEST.invalidate(Thread.currentThread());
//FAWE start
requests.remove(Thread.currentThread());
//FAWE end
} }
/** /**