Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cache SPI and resource serialization #92

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, CacheOp
obj.setMaxSize(((Number)member.getValue()).intValue());
}
break;
case "name":
if (member.getValue() instanceof String) {
obj.setName((String)member.getValue());
}
break;
case "shared":
if (member.getValue() instanceof Boolean) {
obj.setShared((Boolean)member.getValue());
}
break;
}
}
}
Expand All @@ -33,5 +43,9 @@ static void toJson(CacheOptions obj, JsonObject json) {

static void toJson(CacheOptions obj, java.util.Map<String, Object> json) {
json.put("maxSize", obj.getMaxSize());
if (obj.getName() != null) {
json.put("name", obj.getName());
}
json.put("shared", obj.getShared());
}
}
4 changes: 2 additions & 2 deletions src/main/java/io/vertx/httpproxy/ProxyContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public interface ProxyContext {
boolean isWebSocket();

/**
* Attach a payload to the context
* Attach a payload to the context.
*
* @param name the payload name
* @param value any payload value
*/
void set(String name, Object value);

/**
* Get a payload attached to this context
* Get a payload attached to this context.
*
* @param name the payload name
* @param type the expected payload type
Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/vertx/httpproxy/ProxyInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ default Future<Void> handleProxyResponse(ProxyContext context) {
/**
* Used to set whether to apply the interceptor to the WebSocket
* handshake packet. The default value is false.
*
* @return the boolean value
*/
default boolean allowApplyToWebSocket() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/vertx/httpproxy/ProxyRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static ProxyRequest reverseProxy(HttpServerRequest proxiedRequest) {
ProxyRequest setBody(Body body);

/**
* Set the request authority
* Set the request authority.
*
* <ul>
* <li>for HTTP/1 the {@literal Host} header</li>
Expand All @@ -128,7 +128,7 @@ static ProxyRequest reverseProxy(HttpServerRequest proxiedRequest) {
MultiMap headers();

/**
* Put an HTTP header
* Put an HTTP header.
*
* @param name The header name
* @param value The header value
Expand Down Expand Up @@ -157,7 +157,7 @@ default Future<Void> proxy(HttpClientRequest request) {
Future<ProxyResponse> send(HttpClientRequest request);

/**
* Release the proxy request and its associated resources
* Release the proxy request and its associated resources.
*
* <p> The HTTP server request is resumed, no HTTP server response is sent.
*
Expand Down
84 changes: 79 additions & 5 deletions src/main/java/io/vertx/httpproxy/cache/CacheOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import io.vertx.codegen.annotations.DataObject;
import io.vertx.codegen.json.annotations.JsonGen;
import io.vertx.core.json.JsonObject;
import io.vertx.httpproxy.impl.CacheImpl;
import io.vertx.httpproxy.spi.cache.Cache;

import java.util.Objects;

/**
* Cache options.
Expand All @@ -13,19 +13,53 @@
@JsonGen(publicConverter = false)
public class CacheOptions {

/**
* Default max size of the cache = 1000
*/
public static final int DEFAULT_MAX_SIZE = 1000;

/**
* Actual name of anonymous shared cache = {@code __vertx.DEFAULT}
*/
public static final String DEFAULT_NAME = "__vertx.DEFAULT";
wzy1935 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Default shared cache = {@code false}
*/
public static final boolean DEFAULT_SHARED = false;

private int maxSize = DEFAULT_MAX_SIZE;
private String name = DEFAULT_NAME;
private boolean shared = DEFAULT_SHARED;

/**
* Default constructor.
*/
public CacheOptions() {
}

/**
* Copy constructor.
*
* @param other the options to copy
*/
public CacheOptions(CacheOptions other) {
this.maxSize = other.getMaxSize();
this.name = other.getName();
this.shared = other.getShared();
}

/**
* Constructor to create an options from JSON.
*
* @param json the JSON
*/
public CacheOptions(JsonObject json) {
CacheOptionsConverter.fromJson(json, this);
}

/**
* @return the max number of entries the cache can hold
* @return the max number of entries the cache can hold.
*/
public int getMaxSize() {
return maxSize;
Expand All @@ -45,15 +79,55 @@ public CacheOptions setMaxSize(int maxSize) {
return this;
}

public <K, V> Cache<K, V> newCache() {
return new CacheImpl<>(this);
/**
* @return the cache name used for sharing
*/
public String getName() {
return this.name;
}

/**
* Set the cache name, used when the cache is shared, otherwise ignored.
* @param name the new name
* @return a reference to this, so the API can be used fluently
*/
public CacheOptions setName(String name) {
Objects.requireNonNull(name, "Client name cannot be null");
this.name = name;
return this;
}

/**
* @return whether the cache is shared
*/
public boolean getShared() {
return shared;
}

/**
* Set to {@code true} to share the cache.
*
* <p> There can be multiple shared caches distinguished by {@link #getName()}, when no specific
* name is set, the {@link #DEFAULT_NAME} is used.
*
* @param shared {@code true} to use a shared client
* @return a reference to this, so the API can be used fluently
*/
public CacheOptions setShared(boolean shared) {
this.shared = shared;
return this;
}

@Override
public String toString() {
return toJson().toString();
}

/**
* Convert to JSON.
*
* @return the JSON
*/
public JsonObject toJson() {
JsonObject json = new JsonObject();
CacheOptionsConverter.toJson(this, json);
Expand Down
34 changes: 29 additions & 5 deletions src/main/java/io/vertx/httpproxy/impl/CacheImpl.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
package io.vertx.httpproxy.impl;

import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.httpproxy.cache.CacheOptions;
import io.vertx.httpproxy.spi.cache.Cache;
import io.vertx.httpproxy.spi.cache.Resource;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.*;

/**
* Simplistic implementation.
*/
public class CacheImpl<K, V> extends LinkedHashMap<K, V> implements Cache<K, V> {
public class CacheImpl implements Cache {

private final int maxSize;
private final Map<String, Resource> data;

public CacheImpl(CacheOptions options) {
this.maxSize = options.getMaxSize();
this.data = Collections.synchronizedMap(new LinkedHashMap<>() {
@Override
protected boolean removeEldestEntry(Map.Entry<String, Resource> eldest) {
return size() > maxSize;
}
});
}

protected boolean removeEldestEntry(Map.Entry eldest) {
return size() > maxSize;

@Override
public Future<Void> put(String key, Resource value) {
data.put(key, value);
return Future.succeededFuture();
}

@Override
public Future<Resource> get(String key) {
return Future.succeededFuture(data.get(key));
}

@Override
public Future<Void> remove(String key) {
data.remove(key);
return Future.succeededFuture();
}

}
Loading
Loading