View Javadoc
1   /*
2    *    Copyright 2012-2026 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.caches.memcached;
17  
18  import java.net.InetSocketAddress;
19  import java.util.List;
20  import java.util.concurrent.TimeUnit;
21  
22  import net.spy.memcached.ConnectionFactory;
23  
24  /**
25   * The Memcached client configuration.
26   *
27   * @author Simone Tripodi
28   */
29  final class MemcachedConfiguration {
30  
31    /**
32     * The key prefix.
33     */
34    private String keyPrefix;
35  
36    /**
37     * The Connection Factory used to establish the connection to Memcached server(s).
38     */
39    private ConnectionFactory connectionFactory;
40  
41    /**
42     * The Memcached servers.
43     */
44    private List<InetSocketAddress> addresses;
45  
46    /**
47     * The flag to switch from sync to async Memcached get.
48     */
49    private boolean usingAsyncGet;
50  
51    /**
52     * Compression enabled flag.
53     */
54    private boolean compressionEnabled;
55  
56    /**
57     * The Memcached entries expiration time.
58     */
59    private int expiration;
60  
61    /**
62     * The Memcached connection timeout when using async get.
63     */
64    private int timeout;
65  
66    /**
67     * The Memcached timeout unit when using async get.
68     */
69    private TimeUnit timeUnit;
70  
71    /**
72     * The flag to enable SASL Connection
73     */
74    private boolean usingSASL;
75  
76    /**
77     * The Memcached SASL username
78     */
79    private String username;
80  
81    /**
82     * The Memcached SASL password
83     */
84    private String password;
85  
86    /**
87     * @return the keyPrefix
88     */
89    public String getKeyPrefix() {
90      return keyPrefix;
91    }
92  
93    /**
94     * @param keyPrefix
95     *          the keyPrefix to set
96     */
97    public void setKeyPrefix(String keyPrefix) {
98      this.keyPrefix = keyPrefix;
99    }
100 
101   /**
102    * @return the connectionFactory
103    */
104   public ConnectionFactory getConnectionFactory() {
105     return connectionFactory;
106   }
107 
108   /**
109    * @param connectionFactory
110    *          the connectionFactory to set
111    */
112   public void setConnectionFactory(ConnectionFactory connectionFactory) {
113     this.connectionFactory = connectionFactory;
114   }
115 
116   /**
117    * @return the addresses
118    */
119   public List<InetSocketAddress> getAddresses() {
120     return addresses;
121   }
122 
123   /**
124    * @param addresses
125    *          the addresses to set
126    */
127   public void setAddresses(List<InetSocketAddress> addresses) {
128     this.addresses = addresses;
129   }
130 
131   /**
132    * @return the usingAsyncGet
133    */
134   public boolean isUsingAsyncGet() {
135     return usingAsyncGet;
136   }
137 
138   /**
139    * @param usingAsyncGet
140    *          the usingAsyncGet to set
141    */
142   public void setUsingAsyncGet(boolean usingAsyncGet) {
143     this.usingAsyncGet = usingAsyncGet;
144   }
145 
146   /**
147    * @return the compressionEnabled
148    */
149   public boolean isCompressionEnabled() {
150     return compressionEnabled;
151   }
152 
153   /**
154    * @param compressionEnabled
155    *          the compressionEnabled to set
156    */
157   public void setCompressionEnabled(boolean compressionEnabled) {
158     this.compressionEnabled = compressionEnabled;
159   }
160 
161   /**
162    * @return the expiration
163    */
164   public int getExpiration() {
165     return expiration;
166   }
167 
168   /**
169    * @param expiration
170    *          the expiration to set
171    */
172   public void setExpiration(int expiration) {
173     this.expiration = expiration;
174   }
175 
176   /**
177    * @return the timeout
178    */
179   public int getTimeout() {
180     return timeout;
181   }
182 
183   /**
184    * @param timeout
185    *          the timeout to set
186    */
187   public void setTimeout(int timeout) {
188     this.timeout = timeout;
189   }
190 
191   /**
192    * @return the timeUnit
193    */
194   public TimeUnit getTimeUnit() {
195     return timeUnit;
196   }
197 
198   /**
199    * @param timeUnit
200    *          the timeUnit to set
201    */
202   public void setTimeUnit(TimeUnit timeUnit) {
203     this.timeUnit = timeUnit;
204   }
205 
206   /**
207    * @return the usingSASL
208    */
209   public boolean isUsingSASL() {
210     return usingSASL;
211   }
212 
213   /**
214    * @param usingSASL
215    *          the usingSASL to set
216    */
217   public void setUsingSASL(boolean usingSASL) {
218     this.usingSASL = usingSASL;
219   }
220 
221   /**
222    * @return the username
223    */
224   public String getUsername() {
225     return username;
226   }
227 
228   /**
229    * @param username
230    *          the username to set
231    */
232   public void setUsername(String username) {
233     this.username = username;
234   }
235 
236   /**
237    * @return the password
238    */
239   public String getPassword() {
240     return password;
241   }
242 
243   /**
244    * @param password
245    *          the password to set
246    */
247   public void setPassword(String password) {
248     this.password = password;
249   }
250 
251   /**
252    * {@inheritDoc}
253    */
254   @Override
255   public int hashCode() {
256     return hash(1, 31, addresses, compressionEnabled, connectionFactory, expiration, keyPrefix, timeUnit, timeout,
257         usingAsyncGet, usingSASL, username, password);
258   }
259 
260   /**
261    * Computes a hashCode given the input objects.
262    *
263    * @param initialNonZeroOddNumber
264    *          a non-zero, odd number used as the initial value.
265    * @param multiplierNonZeroOddNumber
266    *          a non-zero, odd number used as the multiplier.
267    * @param objs
268    *          the objects to compute hash code.
269    *
270    * @return the computed hashCode.
271    */
272   public static int hash(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber, Object... objs) {
273     int result = initialNonZeroOddNumber;
274     for (Object obj : objs) {
275       result = multiplierNonZeroOddNumber * result + (obj != null ? obj.hashCode() : 0);
276     }
277     return result;
278   }
279 
280   /**
281    * {@inheritDoc}
282    */
283   @Override
284   public boolean equals(Object obj) {
285     if (this == obj) {
286       return true;
287     }
288     if (obj == null || getClass() != obj.getClass()) {
289       return false;
290     }
291 
292     MemcachedConfiguration other = (MemcachedConfiguration) obj;
293     return eq(addresses, other.addresses) && eq(compressionEnabled, other.compressionEnabled)
294         && eq(connectionFactory, other.connectionFactory) && eq(expiration, other.expiration)
295         && eq(keyPrefix, other.keyPrefix) && eq(timeUnit, other.timeUnit) && eq(timeout, other.timeout)
296         && eq(usingAsyncGet, other.usingAsyncGet) && eq(usingSASL, other.usingSASL) && eq(username, other.username)
297         && eq(password, other.password);
298   }
299 
300   /**
301    * Verifies input objects are equal.
302    *
303    * @param o1
304    *          the first argument to compare
305    * @param o2
306    *          the second argument to compare
307    *
308    * @return true, if the input arguments are equal, false otherwise.
309    */
310   private static <O> boolean eq(O o1, O o2) {
311     return o1 != null ? o1.equals(o2) : o2 == null;
312   }
313 
314   /**
315    * {@inheritDoc}
316    */
317   @Override
318   public String toString() {
319     return "MemcachedConfiguration [addresses=%s, compressionEnabled=%s, connectionFactory=%s, , expiration=%s, keyPrefix=%s, timeUnit=%s, timeout=%s, usingAsyncGet=%s, usingSASL=%s, username=%s, password=%s]"
320         .formatted(addresses, compressionEnabled, connectionFactory, expiration, keyPrefix, timeUnit, timeout,
321             usingAsyncGet, usingSASL, username, password);
322   }
323 
324 }