1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.hc.client5.http.impl.cache;
28
29 import org.apache.hc.core5.util.Args;
30 import org.apache.hc.core5.util.TimeValue;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 public class CacheConfig implements Cloneable {
100
101
102
103
104 public final static int DEFAULT_MAX_OBJECT_SIZE_BYTES = 8192;
105
106
107
108
109 public final static int DEFAULT_MAX_CACHE_ENTRIES = 1000;
110
111
112
113
114 public final static int DEFAULT_MAX_UPDATE_RETRIES = 1;
115
116
117
118 public final static boolean DEFAULT_303_CACHING_ENABLED = false;
119
120
121
122 public final static boolean DEFAULT_WEAK_ETAG_ON_PUTDELETE_ALLOWED = false;
123
124
125
126 public final static boolean DEFAULT_HEURISTIC_CACHING_ENABLED = false;
127
128
129
130
131 public final static float DEFAULT_HEURISTIC_COEFFICIENT = 0.1f;
132
133
134
135
136 public final static TimeValue DEFAULT_HEURISTIC_LIFETIME = TimeValue.ZERO_MILLISECONDS;
137
138
139
140
141 public static final int DEFAULT_ASYNCHRONOUS_WORKERS = 1;
142
143 public static final CacheConfig DEFAULT = new Builder().build();
144
145 private final long maxObjectSize;
146 private final int maxCacheEntries;
147 private final int maxUpdateRetries;
148 private final boolean allow303Caching;
149 private final boolean weakETagOnPutDeleteAllowed;
150 private final boolean heuristicCachingEnabled;
151 private final float heuristicCoefficient;
152 private final TimeValue heuristicDefaultLifetime;
153 private final boolean sharedCache;
154 private final boolean freshnessCheckEnabled;
155 private final int asynchronousWorkers;
156 private final boolean neverCacheHTTP10ResponsesWithQuery;
157
158 CacheConfig(
159 final long maxObjectSize,
160 final int maxCacheEntries,
161 final int maxUpdateRetries,
162 final boolean allow303Caching,
163 final boolean weakETagOnPutDeleteAllowed,
164 final boolean heuristicCachingEnabled,
165 final float heuristicCoefficient,
166 final TimeValue heuristicDefaultLifetime,
167 final boolean sharedCache,
168 final boolean freshnessCheckEnabled,
169 final int asynchronousWorkers,
170 final boolean neverCacheHTTP10ResponsesWithQuery) {
171 super();
172 this.maxObjectSize = maxObjectSize;
173 this.maxCacheEntries = maxCacheEntries;
174 this.maxUpdateRetries = maxUpdateRetries;
175 this.allow303Caching = allow303Caching;
176 this.weakETagOnPutDeleteAllowed = weakETagOnPutDeleteAllowed;
177 this.heuristicCachingEnabled = heuristicCachingEnabled;
178 this.heuristicCoefficient = heuristicCoefficient;
179 this.heuristicDefaultLifetime = heuristicDefaultLifetime;
180 this.sharedCache = sharedCache;
181 this.freshnessCheckEnabled = freshnessCheckEnabled;
182 this.asynchronousWorkers = asynchronousWorkers;
183 this.neverCacheHTTP10ResponsesWithQuery = neverCacheHTTP10ResponsesWithQuery;
184 }
185
186
187
188
189
190
191
192 public long getMaxObjectSize() {
193 return maxObjectSize;
194 }
195
196
197
198
199
200
201 public boolean isNeverCacheHTTP10ResponsesWithQuery() {
202 return neverCacheHTTP10ResponsesWithQuery;
203 }
204
205
206
207
208 public int getMaxCacheEntries() {
209 return maxCacheEntries;
210 }
211
212
213
214
215 public int getMaxUpdateRetries(){
216 return maxUpdateRetries;
217 }
218
219
220
221
222
223 public boolean is303CachingEnabled() {
224 return allow303Caching;
225 }
226
227
228
229
230
231 public boolean isWeakETagOnPutDeleteAllowed() {
232 return weakETagOnPutDeleteAllowed;
233 }
234
235
236
237
238
239 public boolean isHeuristicCachingEnabled() {
240 return heuristicCachingEnabled;
241 }
242
243
244
245
246 public float getHeuristicCoefficient() {
247 return heuristicCoefficient;
248 }
249
250
251
252
253
254 public TimeValue getHeuristicDefaultLifetime() {
255 return heuristicDefaultLifetime;
256 }
257
258
259
260
261
262
263 public boolean isSharedCache() {
264 return sharedCache;
265 }
266
267
268
269
270
271
272
273 public boolean isFreshnessCheckEnabled() {
274 return freshnessCheckEnabled;
275 }
276
277
278
279
280
281
282 public int getAsynchronousWorkers() {
283 return asynchronousWorkers;
284 }
285
286 @Override
287 protected CacheConfig clone() throws CloneNotSupportedException {
288 return (CacheConfig) super.clone();
289 }
290
291 public static Builder custom() {
292 return new Builder();
293 }
294
295 public static Builder copy(final CacheConfig config) {
296 Args.notNull(config, "Cache config");
297 return new Builder()
298 .setMaxObjectSize(config.getMaxObjectSize())
299 .setMaxCacheEntries(config.getMaxCacheEntries())
300 .setMaxUpdateRetries(config.getMaxUpdateRetries())
301 .setHeuristicCachingEnabled(config.isHeuristicCachingEnabled())
302 .setHeuristicCoefficient(config.getHeuristicCoefficient())
303 .setHeuristicDefaultLifetime(config.getHeuristicDefaultLifetime())
304 .setSharedCache(config.isSharedCache())
305 .setAsynchronousWorkers(config.getAsynchronousWorkers())
306 .setNeverCacheHTTP10ResponsesWithQueryString(config.isNeverCacheHTTP10ResponsesWithQuery());
307 }
308
309
310 public static class Builder {
311
312 private long maxObjectSize;
313 private int maxCacheEntries;
314 private int maxUpdateRetries;
315 private boolean allow303Caching;
316 private boolean weakETagOnPutDeleteAllowed;
317 private boolean heuristicCachingEnabled;
318 private float heuristicCoefficient;
319 private TimeValue heuristicDefaultLifetime;
320 private boolean sharedCache;
321 private boolean freshnessCheckEnabled;
322 private int asynchronousWorkers;
323 private boolean neverCacheHTTP10ResponsesWithQuery;
324
325 Builder() {
326 this.maxObjectSize = DEFAULT_MAX_OBJECT_SIZE_BYTES;
327 this.maxCacheEntries = DEFAULT_MAX_CACHE_ENTRIES;
328 this.maxUpdateRetries = DEFAULT_MAX_UPDATE_RETRIES;
329 this.allow303Caching = DEFAULT_303_CACHING_ENABLED;
330 this.weakETagOnPutDeleteAllowed = DEFAULT_WEAK_ETAG_ON_PUTDELETE_ALLOWED;
331 this.heuristicCachingEnabled = DEFAULT_HEURISTIC_CACHING_ENABLED;
332 this.heuristicCoefficient = DEFAULT_HEURISTIC_COEFFICIENT;
333 this.heuristicDefaultLifetime = DEFAULT_HEURISTIC_LIFETIME;
334 this.sharedCache = true;
335 this.freshnessCheckEnabled = true;
336 this.asynchronousWorkers = DEFAULT_ASYNCHRONOUS_WORKERS;
337 }
338
339
340
341
342
343 public Builder setMaxObjectSize(final long maxObjectSize) {
344 this.maxObjectSize = maxObjectSize;
345 return this;
346 }
347
348
349
350
351 public Builder setMaxCacheEntries(final int maxCacheEntries) {
352 this.maxCacheEntries = maxCacheEntries;
353 return this;
354 }
355
356
357
358
359 public Builder setMaxUpdateRetries(final int maxUpdateRetries) {
360 this.maxUpdateRetries = maxUpdateRetries;
361 return this;
362 }
363
364
365
366
367
368
369 public Builder setAllow303Caching(final boolean allow303Caching) {
370 this.allow303Caching = allow303Caching;
371 return this;
372 }
373
374
375
376
377
378
379 public Builder setWeakETagOnPutDeleteAllowed(final boolean weakETagOnPutDeleteAllowed) {
380 this.weakETagOnPutDeleteAllowed = weakETagOnPutDeleteAllowed;
381 return this;
382 }
383
384
385
386
387
388
389 public Builder setHeuristicCachingEnabled(final boolean heuristicCachingEnabled) {
390 this.heuristicCachingEnabled = heuristicCachingEnabled;
391 return this;
392 }
393
394
395
396
397
398
399
400
401
402 public Builder setHeuristicCoefficient(final float heuristicCoefficient) {
403 this.heuristicCoefficient = heuristicCoefficient;
404 return this;
405 }
406
407
408
409
410
411
412
413
414
415
416
417 public Builder setHeuristicDefaultLifetime(final TimeValue heuristicDefaultLifetime) {
418 this.heuristicDefaultLifetime = heuristicDefaultLifetime;
419 return this;
420 }
421
422
423
424
425
426
427
428 public Builder setSharedCache(final boolean sharedCache) {
429 this.sharedCache = sharedCache;
430 return this;
431 }
432
433
434
435
436
437
438
439 public Builder setAsynchronousWorkers(final int asynchronousWorkers) {
440 this.asynchronousWorkers = asynchronousWorkers;
441 return this;
442 }
443
444
445
446
447
448
449
450
451 public Builder setNeverCacheHTTP10ResponsesWithQueryString(
452 final boolean neverCacheHTTP10ResponsesWithQuery) {
453 this.neverCacheHTTP10ResponsesWithQuery = neverCacheHTTP10ResponsesWithQuery;
454 return this;
455 }
456
457 public Builder setFreshnessCheckEnabled(final boolean freshnessCheckEnabled) {
458 this.freshnessCheckEnabled = freshnessCheckEnabled;
459 return this;
460 }
461
462 public CacheConfig build() {
463 return new CacheConfig(
464 maxObjectSize,
465 maxCacheEntries,
466 maxUpdateRetries,
467 allow303Caching,
468 weakETagOnPutDeleteAllowed,
469 heuristicCachingEnabled,
470 heuristicCoefficient,
471 heuristicDefaultLifetime,
472 sharedCache,
473 freshnessCheckEnabled,
474 asynchronousWorkers,
475 neverCacheHTTP10ResponsesWithQuery);
476 }
477
478 }
479
480 @Override
481 public String toString() {
482 final StringBuilder builder = new StringBuilder();
483 builder.append("[maxObjectSize=").append(this.maxObjectSize)
484 .append(", maxCacheEntries=").append(this.maxCacheEntries)
485 .append(", maxUpdateRetries=").append(this.maxUpdateRetries)
486 .append(", 303CachingEnabled=").append(this.allow303Caching)
487 .append(", weakETagOnPutDeleteAllowed=").append(this.weakETagOnPutDeleteAllowed)
488 .append(", heuristicCachingEnabled=").append(this.heuristicCachingEnabled)
489 .append(", heuristicCoefficient=").append(this.heuristicCoefficient)
490 .append(", heuristicDefaultLifetime=").append(this.heuristicDefaultLifetime)
491 .append(", sharedCache=").append(this.sharedCache)
492 .append(", freshnessCheckEnabled=").append(this.freshnessCheckEnabled)
493 .append(", asynchronousWorkers=").append(this.asynchronousWorkers)
494 .append(", neverCacheHTTP10ResponsesWithQuery=").append(this.neverCacheHTTP10ResponsesWithQuery)
495 .append("]");
496 return builder.toString();
497 }
498
499 }