33import io .vertx .codegen .annotations .DataObject ;
44import io .vertx .codegen .json .annotations .JsonGen ;
55import io .vertx .core .json .JsonObject ;
6- import io . vertx . httpproxy . impl . CacheImpl ;
7- import io . vertx . httpproxy . spi . cache . Cache ;
6+
7+ import java . util . Objects ;
88
99/**
1010 * Cache options.
1313@ JsonGen (publicConverter = false )
1414public class CacheOptions {
1515
16+ /**
17+ * Default max size of the cache = 1000
18+ */
1619 public static final int DEFAULT_MAX_SIZE = 1000 ;
1720
21+ /**
22+ * Actual name of anonymous shared cache = {@code __vertx.DEFAULT}
23+ */
24+ public static final String DEFAULT_NAME = "__vertx.DEFAULT" ;
25+
26+ /**
27+ * Default shared cache = {@code false}
28+ */
29+ public static final boolean DEFAULT_SHARED = false ;
30+
1831 private int maxSize = DEFAULT_MAX_SIZE ;
32+ private String name = DEFAULT_NAME ;
33+ private boolean shared = DEFAULT_SHARED ;
1934
35+ /**
36+ * Default constructor
37+ */
2038 public CacheOptions () {
2139 }
2240
41+ /**
42+ * Copy constructor
43+ *
44+ * @param other the options to copy
45+ */
46+ public CacheOptions (CacheOptions other ) {
47+ this .maxSize = other .getMaxSize ();
48+ this .name = other .getName ();
49+ this .shared = other .getShared ();
50+ }
51+
52+ /**
53+ * Constructor to create an options from JSON
54+ *
55+ * @param json the JSON
56+ */
2357 public CacheOptions (JsonObject json ) {
2458 CacheOptionsConverter .fromJson (json , this );
2559 }
2660
2761 /**
28- * @return the max number of entries the cache can hold
62+ * @return the max number of entries the cache can hold.
2963 */
3064 public int getMaxSize () {
3165 return maxSize ;
@@ -45,15 +79,55 @@ public CacheOptions setMaxSize(int maxSize) {
4579 return this ;
4680 }
4781
48- public Cache newCache () {
49- return new CacheImpl (this );
82+ /**
83+ * @return the cache name used for sharing
84+ */
85+ public String getName () {
86+ return this .name ;
87+ }
88+
89+ /**
90+ * Set the cache name, used when the cache is shared, otherwise ignored.
91+ * @param name the new name
92+ * @return a reference to this, so the API can be used fluently
93+ */
94+ public CacheOptions setName (String name ) {
95+ Objects .requireNonNull (name , "Client name cannot be null" );
96+ this .name = name ;
97+ return this ;
98+ }
99+
100+ /**
101+ * @return whether the cache is shared
102+ */
103+ public boolean getShared () {
104+ return shared ;
105+ }
106+
107+ /**
108+ * Set to {@code true} to share the cache.
109+ *
110+ * <p> There can be multiple shared caches distinguished by {@link #getName()}, when no specific
111+ * name is set, the {@link #DEFAULT_NAME} is used.
112+ *
113+ * @param shared {@code true} to use a shared client
114+ * @return a reference to this, so the API can be used fluently
115+ */
116+ public CacheOptions setShared (boolean shared ) {
117+ this .shared = shared ;
118+ return this ;
50119 }
51120
52121 @ Override
53122 public String toString () {
54123 return toJson ().toString ();
55124 }
56125
126+ /**
127+ * Convert to JSON
128+ *
129+ * @return the JSON
130+ */
57131 public JsonObject toJson () {
58132 JsonObject json = new JsonObject ();
59133 CacheOptionsConverter .toJson (this , json );
0 commit comments