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.http.impl.client.cache;
28
29 import org.apache.http.util.Args;
30
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
100
101
102
103 public class CacheConfig implements Cloneable {
104
105
106
107
108 public final static int DEFAULT_MAX_OBJECT_SIZE_BYTES = 8192;
109
110
111
112
113 public final static int DEFAULT_MAX_CACHE_ENTRIES = 1000;
114
115
116
117
118 public final static int DEFAULT_MAX_UPDATE_RETRIES = 1;
119
120
121
122 public final static boolean DEFAULT_303_CACHING_ENABLED = false;
123
124
125
126 public final static boolean DEFAULT_WEAK_ETAG_ON_PUTDELETE_ALLOWED = false;
127
128
129
130 public final static boolean DEFAULT_HEURISTIC_CACHING_ENABLED = false;
131
132
133
134
135 public final static float DEFAULT_HEURISTIC_COEFFICIENT = 0.1f;
136
137
138
139
140 public final static long DEFAULT_HEURISTIC_LIFETIME = 0;
141
142
143
144
145 public static final int DEFAULT_ASYNCHRONOUS_WORKERS_MAX = 1;
146
147
148
149
150 public static final int DEFAULT_ASYNCHRONOUS_WORKERS_CORE = 1;
151
152
153
154
155 public static final int DEFAULT_ASYNCHRONOUS_WORKER_IDLE_LIFETIME_SECS = 60;
156
157
158
159 public static final int DEFAULT_REVALIDATION_QUEUE_SIZE = 100;
160
161 public static final CacheConfig DEFAULT = new Builder().build();
162
163
164 private long maxObjectSize;
165 private int maxCacheEntries;
166 private int maxUpdateRetries;
167 private final boolean allow303Caching;
168 private final boolean weakETagOnPutDeleteAllowed;
169 private boolean heuristicCachingEnabled;
170 private float heuristicCoefficient;
171 private long heuristicDefaultLifetime;
172 private boolean isSharedCache;
173 private int asynchronousWorkersMax;
174 private int asynchronousWorkersCore;
175 private int asynchronousWorkerIdleLifetimeSecs;
176 private int revalidationQueueSize;
177 private boolean neverCacheHTTP10ResponsesWithQuery;
178
179
180
181
182 @Deprecated
183 public CacheConfig() {
184 super();
185 this.maxObjectSize = DEFAULT_MAX_OBJECT_SIZE_BYTES;
186 this.maxCacheEntries = DEFAULT_MAX_CACHE_ENTRIES;
187 this.maxUpdateRetries = DEFAULT_MAX_UPDATE_RETRIES;
188 this.allow303Caching = DEFAULT_303_CACHING_ENABLED;
189 this.weakETagOnPutDeleteAllowed = DEFAULT_WEAK_ETAG_ON_PUTDELETE_ALLOWED;
190 this.heuristicCachingEnabled = DEFAULT_HEURISTIC_CACHING_ENABLED;
191 this.heuristicCoefficient = DEFAULT_HEURISTIC_COEFFICIENT;
192 this.heuristicDefaultLifetime = DEFAULT_HEURISTIC_LIFETIME;
193 this.isSharedCache = true;
194 this.asynchronousWorkersMax = DEFAULT_ASYNCHRONOUS_WORKERS_MAX;
195 this.asynchronousWorkersCore = DEFAULT_ASYNCHRONOUS_WORKERS_CORE;
196 this.asynchronousWorkerIdleLifetimeSecs = DEFAULT_ASYNCHRONOUS_WORKER_IDLE_LIFETIME_SECS;
197 this.revalidationQueueSize = DEFAULT_REVALIDATION_QUEUE_SIZE;
198 }
199
200 CacheConfig(
201 final long maxObjectSize,
202 final int maxCacheEntries,
203 final int maxUpdateRetries,
204 final boolean allow303Caching,
205 final boolean weakETagOnPutDeleteAllowed,
206 final boolean heuristicCachingEnabled,
207 final float heuristicCoefficient,
208 final long heuristicDefaultLifetime,
209 final boolean isSharedCache,
210 final int asynchronousWorkersMax,
211 final int asynchronousWorkersCore,
212 final int asynchronousWorkerIdleLifetimeSecs,
213 final int revalidationQueueSize,
214 final boolean neverCacheHTTP10ResponsesWithQuery) {
215 super();
216 this.maxObjectSize = maxObjectSize;
217 this.maxCacheEntries = maxCacheEntries;
218 this.maxUpdateRetries = maxUpdateRetries;
219 this.allow303Caching = allow303Caching;
220 this.weakETagOnPutDeleteAllowed = weakETagOnPutDeleteAllowed;
221 this.heuristicCachingEnabled = heuristicCachingEnabled;
222 this.heuristicCoefficient = heuristicCoefficient;
223 this.heuristicDefaultLifetime = heuristicDefaultLifetime;
224 this.isSharedCache = isSharedCache;
225 this.asynchronousWorkersMax = asynchronousWorkersMax;
226 this.asynchronousWorkersCore = asynchronousWorkersCore;
227 this.asynchronousWorkerIdleLifetimeSecs = asynchronousWorkerIdleLifetimeSecs;
228 this.revalidationQueueSize = revalidationQueueSize;
229 }
230
231
232
233
234
235
236
237 @Deprecated
238 public int getMaxObjectSizeBytes() {
239 return maxObjectSize > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) maxObjectSize;
240 }
241
242
243
244
245
246
247
248 @Deprecated
249 public void setMaxObjectSizeBytes(final int maxObjectSizeBytes) {
250 if (maxObjectSizeBytes > Integer.MAX_VALUE) {
251 this.maxObjectSize = Integer.MAX_VALUE;
252 } else {
253 this.maxObjectSize = maxObjectSizeBytes;
254 }
255 }
256
257
258
259
260
261
262
263 public long getMaxObjectSize() {
264 return maxObjectSize;
265 }
266
267
268
269
270
271
272
273
274
275 @Deprecated
276 public void setMaxObjectSize(final long maxObjectSize) {
277 this.maxObjectSize = maxObjectSize;
278 }
279
280
281
282
283
284
285 public boolean isNeverCacheHTTP10ResponsesWithQuery() {
286 return neverCacheHTTP10ResponsesWithQuery;
287 }
288
289
290
291
292 public int getMaxCacheEntries() {
293 return maxCacheEntries;
294 }
295
296
297
298
299
300
301 @Deprecated
302 public void setMaxCacheEntries(final int maxCacheEntries) {
303 this.maxCacheEntries = maxCacheEntries;
304 }
305
306
307
308
309 public int getMaxUpdateRetries(){
310 return maxUpdateRetries;
311 }
312
313
314
315
316
317
318 @Deprecated
319 public void setMaxUpdateRetries(final int maxUpdateRetries){
320 this.maxUpdateRetries = maxUpdateRetries;
321 }
322
323
324
325
326
327 public boolean is303CachingEnabled() {
328 return allow303Caching;
329 }
330
331
332
333
334
335 public boolean isWeakETagOnPutDeleteAllowed() {
336 return weakETagOnPutDeleteAllowed;
337 }
338
339
340
341
342
343 public boolean isHeuristicCachingEnabled() {
344 return heuristicCachingEnabled;
345 }
346
347
348
349
350
351
352
353
354 @Deprecated
355 public void setHeuristicCachingEnabled(final boolean heuristicCachingEnabled) {
356 this.heuristicCachingEnabled = heuristicCachingEnabled;
357 }
358
359
360
361
362 public float getHeuristicCoefficient() {
363 return heuristicCoefficient;
364 }
365
366
367
368
369
370
371
372
373
374
375
376 @Deprecated
377 public void setHeuristicCoefficient(final float heuristicCoefficient) {
378 this.heuristicCoefficient = heuristicCoefficient;
379 }
380
381
382
383
384
385 public long getHeuristicDefaultLifetime() {
386 return heuristicDefaultLifetime;
387 }
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402 @Deprecated
403 public void setHeuristicDefaultLifetime(final long heuristicDefaultLifetimeSecs) {
404 this.heuristicDefaultLifetime = heuristicDefaultLifetimeSecs;
405 }
406
407
408
409
410
411
412 public boolean isSharedCache() {
413 return isSharedCache;
414 }
415
416
417
418
419
420
421
422
423
424 @Deprecated
425 public void setSharedCache(final boolean isSharedCache) {
426 this.isSharedCache = isSharedCache;
427 }
428
429
430
431
432
433
434 public int getAsynchronousWorkersMax() {
435 return asynchronousWorkersMax;
436 }
437
438
439
440
441
442
443
444
445
446 @Deprecated
447 public void setAsynchronousWorkersMax(final int max) {
448 this.asynchronousWorkersMax = max;
449 }
450
451
452
453
454
455 public int getAsynchronousWorkersCore() {
456 return asynchronousWorkersCore;
457 }
458
459
460
461
462
463
464
465
466
467 @Deprecated
468 public void setAsynchronousWorkersCore(final int min) {
469 this.asynchronousWorkersCore = min;
470 }
471
472
473
474
475
476
477
478 public int getAsynchronousWorkerIdleLifetimeSecs() {
479 return asynchronousWorkerIdleLifetimeSecs;
480 }
481
482
483
484
485
486
487
488
489
490
491 @Deprecated
492 public void setAsynchronousWorkerIdleLifetimeSecs(final int secs) {
493 this.asynchronousWorkerIdleLifetimeSecs = secs;
494 }
495
496
497
498
499 public int getRevalidationQueueSize() {
500 return revalidationQueueSize;
501 }
502
503
504
505
506
507
508 @Deprecated
509 public void setRevalidationQueueSize(final int size) {
510 this.revalidationQueueSize = size;
511 }
512
513 @Override
514 protected CacheConfig clone() throws CloneNotSupportedException {
515 return (CacheConfig) super.clone();
516 }
517
518 public static Builder custom() {
519 return new Builder();
520 }
521
522 public static Builder copy(final CacheConfig config) {
523 Args.notNull(config, "Cache config");
524 return new Builder()
525 .setMaxObjectSize(config.getMaxObjectSize())
526 .setMaxCacheEntries(config.getMaxCacheEntries())
527 .setMaxUpdateRetries(config.getMaxUpdateRetries())
528 .setHeuristicCachingEnabled(config.isHeuristicCachingEnabled())
529 .setHeuristicCoefficient(config.getHeuristicCoefficient())
530 .setHeuristicDefaultLifetime(config.getHeuristicDefaultLifetime())
531 .setSharedCache(config.isSharedCache())
532 .setAsynchronousWorkersMax(config.getAsynchronousWorkersMax())
533 .setAsynchronousWorkersCore(config.getAsynchronousWorkersCore())
534 .setAsynchronousWorkerIdleLifetimeSecs(config.getAsynchronousWorkerIdleLifetimeSecs())
535 .setRevalidationQueueSize(config.getRevalidationQueueSize())
536 .setNeverCacheHTTP10ResponsesWithQueryString(config.isNeverCacheHTTP10ResponsesWithQuery());
537 }
538
539
540 public static class Builder {
541
542 private long maxObjectSize;
543 private int maxCacheEntries;
544 private int maxUpdateRetries;
545 private boolean allow303Caching;
546 private boolean weakETagOnPutDeleteAllowed;
547 private boolean heuristicCachingEnabled;
548 private float heuristicCoefficient;
549 private long heuristicDefaultLifetime;
550 private boolean isSharedCache;
551 private int asynchronousWorkersMax;
552 private int asynchronousWorkersCore;
553 private int asynchronousWorkerIdleLifetimeSecs;
554 private int revalidationQueueSize;
555 private boolean neverCacheHTTP10ResponsesWithQuery;
556
557 Builder() {
558 this.maxObjectSize = DEFAULT_MAX_OBJECT_SIZE_BYTES;
559 this.maxCacheEntries = DEFAULT_MAX_CACHE_ENTRIES;
560 this.maxUpdateRetries = DEFAULT_MAX_UPDATE_RETRIES;
561 this.allow303Caching = DEFAULT_303_CACHING_ENABLED;
562 this.weakETagOnPutDeleteAllowed = DEFAULT_WEAK_ETAG_ON_PUTDELETE_ALLOWED;
563 this.heuristicCachingEnabled = false;
564 this.heuristicCoefficient = DEFAULT_HEURISTIC_COEFFICIENT;
565 this.heuristicDefaultLifetime = DEFAULT_HEURISTIC_LIFETIME;
566 this.isSharedCache = true;
567 this.asynchronousWorkersMax = DEFAULT_ASYNCHRONOUS_WORKERS_MAX;
568 this.asynchronousWorkersCore = DEFAULT_ASYNCHRONOUS_WORKERS_CORE;
569 this.asynchronousWorkerIdleLifetimeSecs = DEFAULT_ASYNCHRONOUS_WORKER_IDLE_LIFETIME_SECS;
570 this.revalidationQueueSize = DEFAULT_REVALIDATION_QUEUE_SIZE;
571 }
572
573
574
575
576
577 public Builder setMaxObjectSize(final long maxObjectSize) {
578 this.maxObjectSize = maxObjectSize;
579 return this;
580 }
581
582
583
584
585 public Builder setMaxCacheEntries(final int maxCacheEntries) {
586 this.maxCacheEntries = maxCacheEntries;
587 return this;
588 }
589
590
591
592
593 public Builder setMaxUpdateRetries(final int maxUpdateRetries) {
594 this.maxUpdateRetries = maxUpdateRetries;
595 return this;
596 }
597
598
599
600
601
602
603 public Builder setAllow303Caching(final boolean allow303Caching) {
604 this.allow303Caching = allow303Caching;
605 return this;
606 }
607
608
609
610
611
612
613 public Builder setWeakETagOnPutDeleteAllowed(final boolean weakETagOnPutDeleteAllowed) {
614 this.weakETagOnPutDeleteAllowed = weakETagOnPutDeleteAllowed;
615 return this;
616 }
617
618
619
620
621
622
623 public Builder setHeuristicCachingEnabled(final boolean heuristicCachingEnabled) {
624 this.heuristicCachingEnabled = heuristicCachingEnabled;
625 return this;
626 }
627
628
629
630
631
632
633
634
635
636 public Builder setHeuristicCoefficient(final float heuristicCoefficient) {
637 this.heuristicCoefficient = heuristicCoefficient;
638 return this;
639 }
640
641
642
643
644
645
646
647
648
649
650
651
652 public Builder setHeuristicDefaultLifetime(final long heuristicDefaultLifetime) {
653 this.heuristicDefaultLifetime = heuristicDefaultLifetime;
654 return this;
655 }
656
657
658
659
660
661
662
663 public Builder setSharedCache(final boolean isSharedCache) {
664 this.isSharedCache = isSharedCache;
665 return this;
666 }
667
668
669
670
671
672
673
674 public Builder setAsynchronousWorkersMax(final int asynchronousWorkersMax) {
675 this.asynchronousWorkersMax = asynchronousWorkersMax;
676 return this;
677 }
678
679
680
681
682
683
684
685 public Builder setAsynchronousWorkersCore(final int asynchronousWorkersCore) {
686 this.asynchronousWorkersCore = asynchronousWorkersCore;
687 return this;
688 }
689
690
691
692
693
694
695
696
697 public Builder setAsynchronousWorkerIdleLifetimeSecs(final int asynchronousWorkerIdleLifetimeSecs) {
698 this.asynchronousWorkerIdleLifetimeSecs = asynchronousWorkerIdleLifetimeSecs;
699 return this;
700 }
701
702
703
704
705 public Builder setRevalidationQueueSize(final int revalidationQueueSize) {
706 this.revalidationQueueSize = revalidationQueueSize;
707 return this;
708 }
709
710
711
712
713
714
715
716
717 public Builder setNeverCacheHTTP10ResponsesWithQueryString(
718 final boolean neverCacheHTTP10ResponsesWithQuery) {
719 this.neverCacheHTTP10ResponsesWithQuery = neverCacheHTTP10ResponsesWithQuery;
720 return this;
721 }
722
723 public CacheConfig build() {
724 return new CacheConfig(
725 maxObjectSize,
726 maxCacheEntries,
727 maxUpdateRetries,
728 allow303Caching,
729 weakETagOnPutDeleteAllowed,
730 heuristicCachingEnabled,
731 heuristicCoefficient,
732 heuristicDefaultLifetime,
733 isSharedCache,
734 asynchronousWorkersMax,
735 asynchronousWorkersCore,
736 asynchronousWorkerIdleLifetimeSecs,
737 revalidationQueueSize,
738 neverCacheHTTP10ResponsesWithQuery);
739 }
740
741 }
742
743 @Override
744 public String toString() {
745 final StringBuilder builder = new StringBuilder();
746 builder.append("[maxObjectSize=").append(this.maxObjectSize)
747 .append(", maxCacheEntries=").append(this.maxCacheEntries)
748 .append(", maxUpdateRetries=").append(this.maxUpdateRetries)
749 .append(", 303CachingEnabled=").append(this.allow303Caching)
750 .append(", weakETagOnPutDeleteAllowed=").append(this.weakETagOnPutDeleteAllowed)
751 .append(", heuristicCachingEnabled=").append(this.heuristicCachingEnabled)
752 .append(", heuristicCoefficient=").append(this.heuristicCoefficient)
753 .append(", heuristicDefaultLifetime=").append(this.heuristicDefaultLifetime)
754 .append(", isSharedCache=").append(this.isSharedCache)
755 .append(", asynchronousWorkersMax=").append(this.asynchronousWorkersMax)
756 .append(", asynchronousWorkersCore=").append(this.asynchronousWorkersCore)
757 .append(", asynchronousWorkerIdleLifetimeSecs=").append(this.asynchronousWorkerIdleLifetimeSecs)
758 .append(", revalidationQueueSize=").append(this.revalidationQueueSize)
759 .append(", neverCacheHTTP10ResponsesWithQuery=").append(this.neverCacheHTTP10ResponsesWithQuery)
760 .append("]");
761 return builder.toString();
762 }
763
764 }