1 package com.atlassian.cache;
2
3 import java.util.concurrent.TimeUnit;
4 import com.atlassian.annotations.PublicApi;
5 import com.atlassian.util.concurrent.NotNull;
6
7 /**
8 * A builder for creating {@link CacheSettings} instances.
9 * @since 2.0
10 */
11 @PublicApi
12 public class CacheSettingsBuilder
13 {
14 private Long expireAfterAccess;
15 private Long expireAfterWrite;
16 private Boolean flushable;
17 private Boolean local;
18 private Integer maxEntries;
19 private Boolean replicateAsynchronously;
20 private Boolean replicateViaCopy;
21
22 /**
23 * @return a new {@link CacheSettings} instance with
24 */
25 public CacheSettings build()
26 {
27 return new DefaultCacheSettings(expireAfterAccess, expireAfterWrite, flushable,
28 local, maxEntries, replicateAsynchronously, replicateViaCopy);
29 }
30
31 /**
32 * Set a hint for the cache regarding how long entries should be held in the cache.
33 * @param expireAfter Time to retain entries for since their last access.
34 * @param timeUnit The {@link TimeUnit} for the time
35 * @return this builder
36 */
37 public CacheSettingsBuilder expireAfterAccess(long expireAfter, @NotNull TimeUnit timeUnit)
38 {
39 this.expireAfterAccess = timeUnit.toMillis(expireAfter);
40 return this;
41 }
42
43 /**
44 * Set a hint for the cache regarding how long entries should be held in the cache.
45 * @param expireAfter Time to retain entries from when they were created.
46 * @param timeUnit The {@link TimeUnit} for the time
47 * @return this builder
48 */
49 public CacheSettingsBuilder expireAfterWrite(long expireAfter, @NotNull TimeUnit timeUnit)
50 {
51 this.expireAfterWrite = timeUnit.toMillis(expireAfter);
52 return this;
53 }
54
55 /**
56 * Indicates that this cache can be flushed by the cache manager when required or desired.
57 * @return this builder
58 */
59 public CacheSettingsBuilder flushable()
60 {
61 this.flushable = true;
62 return this;
63 }
64
65 /**
66 * Indicates that this cache cannot be flushed by the cache manager when required or desired.
67 * @return this builder
68 */
69 public CacheSettingsBuilder unflushable()
70 {
71 this.flushable = false;
72 return this;
73 }
74
75 /**
76 * Indicates that this cache can have a maximum number of entries.
77 * @param maxEntries The maximum number of entries. Must be greater than zero.
78 * @return this builder
79 */
80 public CacheSettingsBuilder maxEntries(int maxEntries)
81 {
82 if (0 >= maxEntries)
83 {
84 throw new IllegalArgumentException("maxEntries must be greater than zero, passed: " + maxEntries);
85 }
86 this.maxEntries = maxEntries;
87 return this;
88 }
89
90 /**
91 * Indicates that in a clustered environment with replicated caches, this
92 * cache should replicate asynchronously. By default, replication is
93 * synchronous, i.e. cache mutations block on the originating node until
94 * all nodes in the cluster have replicated the mutation. Asynchronous
95 * replication provides greater responsiveness at the expense of
96 * consistency.
97 *
98 * @return this builder
99 */
100 public CacheSettingsBuilder replicateAsynchronously()
101 {
102 this.replicateAsynchronously = true;
103 return this;
104 }
105
106 /**
107 * Indicates that in a clustered environment with replicated caches, this
108 * cache should replicate put and update operations by copying the relevant
109 * key and value across the wire (requiring both of them to be {@link
110 * java.io.Serializable}). By default, put and update operations are
111 * replicated by sending only the key across the wire, for invalidation by
112 * the other nodes in the cluster; this requires only the key to be {@link
113 * java.io.Serializable}.
114 *
115 * @return this builder
116 */
117 public CacheSettingsBuilder replicateViaCopy()
118 {
119 this.replicateViaCopy = true;
120 return this;
121 }
122
123 /**
124 * Indicates that this cache should be local to the node (JVM) where the cache is created.
125 * By default, caches will be clustered in a clustered deployment.
126 * @return this builder
127 */
128 public CacheSettingsBuilder local()
129 {
130 this.local = true;
131 return this;
132 }
133
134 /**
135 * Indicates that this cache should be clustered in a clustered deployment.
136 * @return this builder
137 */
138 public CacheSettingsBuilder remote()
139 {
140 this.local = false;
141 return this;
142 }
143 }