Skip to content

Commit 3a9bc3d

Browse files
committed
Merge branch 'wzy1935-issue/67'
2 parents de37cc1 + e3db28d commit 3a9bc3d

File tree

13 files changed

+733
-128
lines changed

13 files changed

+733
-128
lines changed

src/main/generated/io/vertx/httpproxy/cache/CacheOptionsConverter.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, CacheOp
1919
obj.setMaxSize(((Number)member.getValue()).intValue());
2020
}
2121
break;
22+
case "name":
23+
if (member.getValue() instanceof String) {
24+
obj.setName((String)member.getValue());
25+
}
26+
break;
27+
case "shared":
28+
if (member.getValue() instanceof Boolean) {
29+
obj.setShared((Boolean)member.getValue());
30+
}
31+
break;
2232
}
2333
}
2434
}
@@ -29,5 +39,9 @@ static void toJson(CacheOptions obj, JsonObject json) {
2939

3040
static void toJson(CacheOptions obj, java.util.Map<String, Object> json) {
3141
json.put("maxSize", obj.getMaxSize());
42+
if (obj.getName() != null) {
43+
json.put("name", obj.getName());
44+
}
45+
json.put("shared", obj.isShared());
3246
}
3347
}

src/main/java/io/vertx/httpproxy/ProxyContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ public interface ProxyContext {
3535
boolean isWebSocket();
3636

3737
/**
38-
* Attach a payload to the context
38+
* Attach a payload to the context.
3939
*
4040
* @param name the payload name
4141
* @param value any payload value
4242
*/
4343
void set(String name, Object value);
4444

4545
/**
46-
* Get a payload attached to this context
46+
* Get a payload attached to this context.
4747
*
4848
* @param name the payload name
4949
* @param type the expected payload type

src/main/java/io/vertx/httpproxy/ProxyRequest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static ProxyRequest reverseProxy(HttpServerRequest proxiedRequest) {
101101
ProxyRequest setBody(Body body);
102102

103103
/**
104-
* Set the request authority
104+
* Set the request authority.
105105
*
106106
* <ul>
107107
* <li>for HTTP/1 the {@literal Host} header</li>
@@ -128,7 +128,7 @@ static ProxyRequest reverseProxy(HttpServerRequest proxiedRequest) {
128128
MultiMap headers();
129129

130130
/**
131-
* Put an HTTP header
131+
* Put an HTTP header.
132132
*
133133
* @param name The header name
134134
* @param value The header value
@@ -157,7 +157,7 @@ default Future<Void> proxy(HttpClientRequest request) {
157157
Future<ProxyResponse> send(HttpClientRequest request);
158158

159159
/**
160-
* Release the proxy request and its associated resources
160+
* Release the proxy request and its associated resources.
161161
*
162162
* <p> The HTTP server request is resumed, no HTTP server response is sent.
163163
*

src/main/java/io/vertx/httpproxy/cache/CacheOptions.java

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1+
/*
2+
* Copyright (c) 2011-2025 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+
*/
11+
112
package io.vertx.httpproxy.cache;
213

314
import io.vertx.codegen.annotations.DataObject;
415
import io.vertx.codegen.json.annotations.JsonGen;
516
import io.vertx.core.json.JsonObject;
6-
import io.vertx.httpproxy.impl.CacheImpl;
7-
import io.vertx.httpproxy.spi.cache.Cache;
17+
18+
import java.util.Objects;
819

920
/**
1021
* Cache options.
@@ -13,19 +24,53 @@
1324
@JsonGen(publicConverter = false)
1425
public class CacheOptions {
1526

27+
/**
28+
* Default max size of the cache = 1000
29+
*/
1630
public static final int DEFAULT_MAX_SIZE = 1000;
1731

32+
/**
33+
* Actual name of anonymous shared cache = {@code __vertx.DEFAULT}
34+
*/
35+
public static final String DEFAULT_NAME = "__vertx.DEFAULT";
36+
37+
/**
38+
* Default shared cache = {@code false}
39+
*/
40+
public static final boolean DEFAULT_SHARED = false;
41+
1842
private int maxSize = DEFAULT_MAX_SIZE;
43+
private String name = DEFAULT_NAME;
44+
private boolean shared = DEFAULT_SHARED;
1945

46+
/**
47+
* Default constructor.
48+
*/
2049
public CacheOptions() {
2150
}
2251

52+
/**
53+
* Copy constructor.
54+
*
55+
* @param other the options to copy
56+
*/
57+
public CacheOptions(CacheOptions other) {
58+
this.maxSize = other.getMaxSize();
59+
this.name = other.getName();
60+
this.shared = other.isShared();
61+
}
62+
63+
/**
64+
* Constructor to create an options from JSON.
65+
*
66+
* @param json the JSON
67+
*/
2368
public CacheOptions(JsonObject json) {
2469
CacheOptionsConverter.fromJson(json, this);
2570
}
2671

2772
/**
28-
* @return the max number of entries the cache can hold
73+
* @return the max number of entries the cache can hold.
2974
*/
3075
public int getMaxSize() {
3176
return maxSize;
@@ -45,15 +90,55 @@ public CacheOptions setMaxSize(int maxSize) {
4590
return this;
4691
}
4792

48-
public <K, V> Cache<K, V> newCache() {
49-
return new CacheImpl<>(this);
93+
/**
94+
* @return the cache name used for sharing
95+
*/
96+
public String getName() {
97+
return this.name;
98+
}
99+
100+
/**
101+
* Set the cache name, used when the cache is shared, otherwise ignored.
102+
* @param name the new name
103+
* @return a reference to this, so the API can be used fluently
104+
*/
105+
public CacheOptions setName(String name) {
106+
Objects.requireNonNull(name, "Client name cannot be null");
107+
this.name = name;
108+
return this;
109+
}
110+
111+
/**
112+
* @return whether the cache is shared
113+
*/
114+
public boolean isShared() {
115+
return shared;
116+
}
117+
118+
/**
119+
* Set to {@code true} to share the cache.
120+
*
121+
* <p> There can be multiple shared caches distinguished by {@link #getName()}, when no specific
122+
* name is set, the {@link #DEFAULT_NAME} is used.
123+
*
124+
* @param shared {@code true} to use a shared client
125+
* @return a reference to this, so the API can be used fluently
126+
*/
127+
public CacheOptions setShared(boolean shared) {
128+
this.shared = shared;
129+
return this;
50130
}
51131

52132
@Override
53133
public String toString() {
54134
return toJson().toString();
55135
}
56136

137+
/**
138+
* Convert to JSON.
139+
*
140+
* @return the JSON
141+
*/
57142
public JsonObject toJson() {
58143
JsonObject json = new JsonObject();
59144
CacheOptionsConverter.toJson(this, json);
Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,47 @@
11
package io.vertx.httpproxy.impl;
22

3+
import io.vertx.core.Future;
4+
import io.vertx.core.Vertx;
35
import io.vertx.httpproxy.cache.CacheOptions;
46
import io.vertx.httpproxy.spi.cache.Cache;
7+
import io.vertx.httpproxy.spi.cache.Resource;
58

6-
import java.util.LinkedHashMap;
7-
import java.util.Map;
9+
import java.util.*;
810

911
/**
1012
* Simplistic implementation.
1113
*/
12-
public class CacheImpl<K, V> extends LinkedHashMap<K, V> implements Cache<K, V> {
14+
public class CacheImpl implements Cache {
1315

1416
private final int maxSize;
17+
private final Map<String, Resource> data;
1518

1619
public CacheImpl(CacheOptions options) {
1720
this.maxSize = options.getMaxSize();
21+
this.data = Collections.synchronizedMap(new LinkedHashMap<>() {
22+
@Override
23+
protected boolean removeEldestEntry(Map.Entry<String, Resource> eldest) {
24+
return size() > maxSize;
25+
}
26+
});
1827
}
1928

20-
protected boolean removeEldestEntry(Map.Entry eldest) {
21-
return size() > maxSize;
29+
30+
@Override
31+
public Future<Void> put(String key, Resource value) {
32+
data.put(key, value);
33+
return Future.succeededFuture();
34+
}
35+
36+
@Override
37+
public Future<Resource> get(String key) {
38+
return Future.succeededFuture(data.get(key));
2239
}
40+
41+
@Override
42+
public Future<Void> remove(String key) {
43+
data.remove(key);
44+
return Future.succeededFuture();
45+
}
46+
2347
}

0 commit comments

Comments
 (0)