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 public class CacheConfig implements Cloneable {
76
77
78
79
80 public final static int DEFAULT_MAX_OBJECT_SIZE_BYTES = 8192;
81
82
83
84
85 public final static int DEFAULT_MAX_CACHE_ENTRIES = 1000;
86
87
88
89
90 public final static int DEFAULT_MAX_UPDATE_RETRIES = 1;
91
92
93
94
95 @Deprecated
96 public final static boolean DEFAULT_303_CACHING_ENABLED = false;
97
98
99
100
101 @Deprecated
102 public final static boolean DEFAULT_WEAK_ETAG_ON_PUTDELETE_ALLOWED = false;
103
104
105
106 public final static boolean DEFAULT_HEURISTIC_CACHING_ENABLED = false;
107
108
109
110
111 public final static float DEFAULT_HEURISTIC_COEFFICIENT = 0.1f;
112
113
114
115
116 public final static TimeValue DEFAULT_HEURISTIC_LIFETIME = TimeValue.ZERO_MILLISECONDS;
117
118
119
120
121 public static final int DEFAULT_ASYNCHRONOUS_WORKERS = 1;
122
123 public static final CacheConfig DEFAULT = new Builder().build();
124
125 private final long maxObjectSize;
126 private final int maxCacheEntries;
127 private final int maxUpdateRetries;
128 private final boolean heuristicCachingEnabled;
129 private final float heuristicCoefficient;
130 private final TimeValue heuristicDefaultLifetime;
131 private final boolean sharedCache;
132 private final boolean freshnessCheckEnabled;
133 private final int asynchronousWorkers;
134 private final boolean neverCacheHTTP10ResponsesWithQuery;
135 private final boolean staleIfErrorEnabled;
136
137
138
139
140
141
142 private final boolean neverCacheHTTP11ResponsesWithQuery;
143
144 CacheConfig(
145 final long maxObjectSize,
146 final int maxCacheEntries,
147 final int maxUpdateRetries,
148 final boolean heuristicCachingEnabled,
149 final float heuristicCoefficient,
150 final TimeValue heuristicDefaultLifetime,
151 final boolean sharedCache,
152 final boolean freshnessCheckEnabled,
153 final int asynchronousWorkers,
154 final boolean neverCacheHTTP10ResponsesWithQuery,
155 final boolean neverCacheHTTP11ResponsesWithQuery,
156 final boolean staleIfErrorEnabled) {
157 super();
158 this.maxObjectSize = maxObjectSize;
159 this.maxCacheEntries = maxCacheEntries;
160 this.maxUpdateRetries = maxUpdateRetries;
161 this.heuristicCachingEnabled = heuristicCachingEnabled;
162 this.heuristicCoefficient = heuristicCoefficient;
163 this.heuristicDefaultLifetime = heuristicDefaultLifetime;
164 this.sharedCache = sharedCache;
165 this.freshnessCheckEnabled = freshnessCheckEnabled;
166 this.asynchronousWorkers = asynchronousWorkers;
167 this.neverCacheHTTP10ResponsesWithQuery = neverCacheHTTP10ResponsesWithQuery;
168 this.neverCacheHTTP11ResponsesWithQuery = neverCacheHTTP11ResponsesWithQuery;
169 this.staleIfErrorEnabled = staleIfErrorEnabled;
170 }
171
172
173
174
175
176
177
178 public long getMaxObjectSize() {
179 return maxObjectSize;
180 }
181
182
183
184
185
186
187 public boolean isNeverCacheHTTP10ResponsesWithQuery() {
188 return neverCacheHTTP10ResponsesWithQuery;
189 }
190
191
192
193
194
195
196
197
198
199
200
201
202
203 public boolean isNeverCacheHTTP11ResponsesWithQuery() {
204 return neverCacheHTTP11ResponsesWithQuery;
205 }
206
207
208
209
210
211
212
213
214
215
216 public boolean isStaleIfErrorEnabled() {
217 return this.staleIfErrorEnabled;
218 }
219
220
221
222
223 public int getMaxCacheEntries() {
224 return maxCacheEntries;
225 }
226
227
228
229
230 public int getMaxUpdateRetries(){
231 return maxUpdateRetries;
232 }
233
234
235
236
237 @Deprecated
238 public boolean is303CachingEnabled() {
239 return true;
240 }
241
242
243
244
245
246
247
248 @Deprecated
249 public boolean isWeakETagOnPutDeleteAllowed() {
250 return true;
251 }
252
253
254
255
256
257 public boolean isHeuristicCachingEnabled() {
258 return heuristicCachingEnabled;
259 }
260
261
262
263
264 public float getHeuristicCoefficient() {
265 return heuristicCoefficient;
266 }
267
268
269
270
271
272 public TimeValue getHeuristicDefaultLifetime() {
273 return heuristicDefaultLifetime;
274 }
275
276
277
278
279
280
281 public boolean isSharedCache() {
282 return sharedCache;
283 }
284
285
286
287
288
289
290
291 public boolean isFreshnessCheckEnabled() {
292 return freshnessCheckEnabled;
293 }
294
295
296
297
298
299
300 public int getAsynchronousWorkers() {
301 return asynchronousWorkers;
302 }
303
304 @Override
305 protected CacheConfig clone() throws CloneNotSupportedException {
306 return (CacheConfig) super.clone();
307 }
308
309 public static Builder custom() {
310 return new Builder();
311 }
312
313 public static Builder copy(final CacheConfig config) {
314 Args.notNull(config, "Cache config");
315 return new Builder()
316 .setMaxObjectSize(config.getMaxObjectSize())
317 .setMaxCacheEntries(config.getMaxCacheEntries())
318 .setMaxUpdateRetries(config.getMaxUpdateRetries())
319 .setHeuristicCachingEnabled(config.isHeuristicCachingEnabled())
320 .setHeuristicCoefficient(config.getHeuristicCoefficient())
321 .setHeuristicDefaultLifetime(config.getHeuristicDefaultLifetime())
322 .setSharedCache(config.isSharedCache())
323 .setAsynchronousWorkers(config.getAsynchronousWorkers())
324 .setNeverCacheHTTP10ResponsesWithQueryString(config.isNeverCacheHTTP10ResponsesWithQuery())
325 .setNeverCacheHTTP11ResponsesWithQueryString(config.isNeverCacheHTTP11ResponsesWithQuery())
326 .setStaleIfErrorEnabled(config.isStaleIfErrorEnabled());
327 }
328
329 public static class Builder {
330
331 private long maxObjectSize;
332 private int maxCacheEntries;
333 private int maxUpdateRetries;
334 private boolean heuristicCachingEnabled;
335 private float heuristicCoefficient;
336 private TimeValue heuristicDefaultLifetime;
337 private boolean sharedCache;
338 private boolean freshnessCheckEnabled;
339 private int asynchronousWorkers;
340 private boolean neverCacheHTTP10ResponsesWithQuery;
341 private boolean neverCacheHTTP11ResponsesWithQuery;
342 private boolean staleIfErrorEnabled;
343
344 Builder() {
345 this.maxObjectSize = DEFAULT_MAX_OBJECT_SIZE_BYTES;
346 this.maxCacheEntries = DEFAULT_MAX_CACHE_ENTRIES;
347 this.maxUpdateRetries = DEFAULT_MAX_UPDATE_RETRIES;
348 this.heuristicCachingEnabled = DEFAULT_HEURISTIC_CACHING_ENABLED;
349 this.heuristicCoefficient = DEFAULT_HEURISTIC_COEFFICIENT;
350 this.heuristicDefaultLifetime = DEFAULT_HEURISTIC_LIFETIME;
351 this.sharedCache = true;
352 this.freshnessCheckEnabled = true;
353 this.asynchronousWorkers = DEFAULT_ASYNCHRONOUS_WORKERS;
354 this.staleIfErrorEnabled = false;
355 }
356
357
358
359
360
361
362 public Builder setMaxObjectSize(final long maxObjectSize) {
363 this.maxObjectSize = maxObjectSize;
364 return this;
365 }
366
367
368
369
370
371
372 public Builder setMaxCacheEntries(final int maxCacheEntries) {
373 this.maxCacheEntries = maxCacheEntries;
374 return this;
375 }
376
377
378
379
380
381
382 public Builder setMaxUpdateRetries(final int maxUpdateRetries) {
383 this.maxUpdateRetries = maxUpdateRetries;
384 return this;
385 }
386
387
388
389
390
391 @Deprecated
392 public Builder setAllow303Caching(final boolean allow303Caching) {
393 return this;
394 }
395
396
397
398
399
400 @Deprecated
401 public Builder setWeakETagOnPutDeleteAllowed(final boolean weakETagOnPutDeleteAllowed) {
402 return this;
403 }
404
405
406
407
408
409
410
411 public Builder setHeuristicCachingEnabled(final boolean heuristicCachingEnabled) {
412 this.heuristicCachingEnabled = heuristicCachingEnabled;
413 return this;
414 }
415
416
417
418
419
420
421
422
423
424
425 public Builder setHeuristicCoefficient(final float heuristicCoefficient) {
426 this.heuristicCoefficient = heuristicCoefficient;
427 return this;
428 }
429
430
431
432
433
434
435
436
437
438
439
440
441 public Builder setHeuristicDefaultLifetime(final TimeValue heuristicDefaultLifetime) {
442 this.heuristicDefaultLifetime = heuristicDefaultLifetime;
443 return this;
444 }
445
446
447
448
449
450
451
452
453
454 public Builder setSharedCache(final boolean sharedCache) {
455 this.sharedCache = sharedCache;
456 return this;
457 }
458
459
460
461
462
463
464
465
466 public Builder setAsynchronousWorkers(final int asynchronousWorkers) {
467 this.asynchronousWorkers = asynchronousWorkers;
468 return this;
469 }
470
471
472
473
474
475
476
477
478
479 public Builder setNeverCacheHTTP10ResponsesWithQueryString(
480 final boolean neverCacheHTTP10ResponsesWithQuery) {
481 this.neverCacheHTTP10ResponsesWithQuery = neverCacheHTTP10ResponsesWithQuery;
482 return this;
483 }
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499 public Builder setStaleIfErrorEnabled(final boolean enabled) {
500 this.staleIfErrorEnabled = enabled;
501 return this;
502 }
503
504 public Builder setFreshnessCheckEnabled(final boolean freshnessCheckEnabled) {
505 this.freshnessCheckEnabled = freshnessCheckEnabled;
506 return this;
507 }
508
509
510
511
512
513
514
515 public Builder setNeverCacheHTTP11ResponsesWithQueryString(
516 final boolean neverCacheHTTP11ResponsesWithQuery) {
517 this.neverCacheHTTP11ResponsesWithQuery = neverCacheHTTP11ResponsesWithQuery;
518 return this;
519 }
520
521 public CacheConfig build() {
522 return new CacheConfig(
523 maxObjectSize,
524 maxCacheEntries,
525 maxUpdateRetries,
526 heuristicCachingEnabled,
527 heuristicCoefficient,
528 heuristicDefaultLifetime,
529 sharedCache,
530 freshnessCheckEnabled,
531 asynchronousWorkers,
532 neverCacheHTTP10ResponsesWithQuery,
533 neverCacheHTTP11ResponsesWithQuery,
534 staleIfErrorEnabled);
535 }
536
537 }
538
539 @Override
540 public String toString() {
541 final StringBuilder builder = new StringBuilder();
542 builder.append("[maxObjectSize=").append(this.maxObjectSize)
543 .append(", maxCacheEntries=").append(this.maxCacheEntries)
544 .append(", maxUpdateRetries=").append(this.maxUpdateRetries)
545 .append(", heuristicCachingEnabled=").append(this.heuristicCachingEnabled)
546 .append(", heuristicCoefficient=").append(this.heuristicCoefficient)
547 .append(", heuristicDefaultLifetime=").append(this.heuristicDefaultLifetime)
548 .append(", sharedCache=").append(this.sharedCache)
549 .append(", freshnessCheckEnabled=").append(this.freshnessCheckEnabled)
550 .append(", asynchronousWorkers=").append(this.asynchronousWorkers)
551 .append(", neverCacheHTTP10ResponsesWithQuery=").append(this.neverCacheHTTP10ResponsesWithQuery)
552 .append(", neverCacheHTTP11ResponsesWithQuery=").append(this.neverCacheHTTP11ResponsesWithQuery)
553 .append(", staleIfErrorEnabled=").append(this.staleIfErrorEnabled)
554 .append("]");
555 return builder.toString();
556 }
557
558 }