|
2 | 2 |
|
3 | 3 | import io.vertx.codegen.annotations.DataObject; |
4 | 4 | import io.vertx.codegen.json.annotations.JsonGen; |
| 5 | +import io.vertx.core.Vertx; |
| 6 | +import io.vertx.core.http.HttpClientAgent; |
| 7 | +import io.vertx.core.http.HttpClientOptions; |
| 8 | +import io.vertx.core.internal.CloseFuture; |
| 9 | +import io.vertx.core.internal.VertxInternal; |
5 | 10 | import io.vertx.core.json.JsonObject; |
6 | 11 | import io.vertx.httpproxy.impl.CacheImpl; |
7 | 12 | import io.vertx.httpproxy.spi.cache.Cache; |
8 | 13 |
|
| 14 | +import java.util.Objects; |
| 15 | + |
9 | 16 | /** |
10 | 17 | * Cache options. |
11 | 18 | */ |
|
14 | 21 | public class CacheOptions { |
15 | 22 |
|
16 | 23 | public static final int DEFAULT_MAX_SIZE = 1000; |
| 24 | + public static final String DEFAULT_NAME = "__vertx.DEFAULT"; |
| 25 | + public static final boolean DEFAULT_SHARED = false; |
17 | 26 |
|
18 | 27 | private int maxSize = DEFAULT_MAX_SIZE; |
| 28 | + private String name = DEFAULT_NAME; |
| 29 | + private boolean shared = DEFAULT_SHARED; |
19 | 30 |
|
20 | 31 | public CacheOptions() { |
21 | 32 | } |
@@ -45,7 +56,36 @@ public CacheOptions setMaxSize(int maxSize) { |
45 | 56 | return this; |
46 | 57 | } |
47 | 58 |
|
48 | | - public Cache newCache() { |
| 59 | + public String getName() { |
| 60 | + return this.name; |
| 61 | + } |
| 62 | + |
| 63 | + public CacheOptions setName(String name) { |
| 64 | + Objects.requireNonNull(name, "Client name cannot be null"); |
| 65 | + this.name = name; |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + public boolean getShared() { |
| 70 | + return shared; |
| 71 | + } |
| 72 | + |
| 73 | + public CacheOptions setShared(boolean shared) { |
| 74 | + this.shared = shared; |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + public Cache newCache(Vertx vertx) { |
| 79 | + if (shared) { |
| 80 | + CloseFuture closeFuture = new CloseFuture(); |
| 81 | + return ((VertxInternal) vertx).createSharedResource("__vertx.shared.proxyCache", name, closeFuture, (cf_) -> { |
| 82 | + Cache cache = new CacheImpl(this); |
| 83 | + cf_.add(completion -> { |
| 84 | + cache.close().onComplete(completion); |
| 85 | + }); |
| 86 | + return cache; |
| 87 | + }); |
| 88 | + } |
49 | 89 | return new CacheImpl(this); |
50 | 90 | } |
51 | 91 |
|
|
0 commit comments