001package org.cache2k; 002 003/* 004 * #%L 005 * cache2k API 006 * %% 007 * Copyright (C) 2000 - 2016 headissue GmbH, Munich 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import org.cache2k.configuration.Cache2kConfiguration; 024import org.cache2k.configuration.CacheType; 025import org.cache2k.expiry.*; 026import org.cache2k.event.CacheEntryOperationListener; 027import org.cache2k.integration.AdvancedCacheLoader; 028import org.cache2k.integration.CacheLoader; 029import org.cache2k.integration.CacheWriter; 030import org.cache2k.integration.ExceptionPropagator; 031import org.cache2k.integration.ExceptionInformation; 032import org.cache2k.integration.ResiliencePolicy; 033 034import java.util.Collection; 035import java.util.Collections; 036import java.util.concurrent.TimeUnit; 037 038/** 039 * @deprecated Replaced with {@link Cache2kBuilder} 040 */ 041public class CacheBuilder<K,V> { 042 043 public static CacheBuilder<?,?> newCache() { 044 return new CacheBuilder(Cache2kBuilder.forUnknownTypes()); 045 } 046 047 public static <K,V> CacheBuilder<K,V> newCache(Class<K> _keyType, Class<V> _valueType) { 048 return fromConfig(Cache2kConfiguration.of(_keyType, _valueType)); 049 } 050 051 @SuppressWarnings("unchecked") 052 public static <K, C extends Collection<T>, T> CacheBuilder<K, C> newCache( 053 Class<K> _keyType, Class<C> _collectionType, Class<T> _entryType) { 054 return newCache(_keyType, _collectionType); 055 } 056 057 public static <K1, T> CacheBuilder<K1, T> fromConfig(final Cache2kConfiguration<K1, T> c) { 058 return new CacheBuilder<K1, T>(Cache2kBuilder.of(c)); 059 } 060 061 public CacheBuilder(final Cache2kBuilder<K, V> _builder) { 062 builder = _builder; 063 /** backwards compatible */ 064 builder.permitNullValues(true); 065 } 066 067 Cache2kBuilder<K,V> builder; 068 069 public CacheBuilder<K, V> addListener(final CacheEntryOperationListener<K, V> listener) { 070 builder.addListener(listener); 071 return this; 072 } 073 074 public CacheBuilder<K, V> entryCapacity(final int v) { 075 builder.entryCapacity(v); 076 return this; 077 } 078 079 public CacheBuilder<K, V> loaderThreadCount(final int v) { 080 builder.loaderThreadCount(v); 081 return this; 082 } 083 084 /** 085 * @deprecated Replaced by {@link Cache2kBuilder#expireAfterWrite} 086 */ 087 public CacheBuilder<K, V> expiryDuration(final long v, final TimeUnit u) { 088 builder.expireAfterWrite(v, u); 089 return this; 090 } 091 092 public CacheBuilder<K, V> entryExpiryCalculator(final EntryExpiryCalculator<K, V> c) { 093 builder.expiryPolicy(c); 094 return this; 095 } 096 097 public CacheBuilder<K, V> name(final Class<?> _class) { 098 name(_class.getSimpleName()); 099 return this; 100 } 101 102 public CacheBuilder<K, V> name(final Object _containingObject, final String _fieldName) { 103 name(_containingObject.getClass(), _fieldName); 104 return this; 105 } 106 107 public CacheBuilder<K, V> loader(final CacheLoader<K, V> l) { 108 builder.loader(l); 109 return this; 110 } 111 112 public CacheBuilder<K, V> sharpExpiry(final boolean f) { 113 builder.sharpExpiry(f); 114 return this; 115 } 116 117 public CacheBuilder<K, V> eternal(final boolean v) { 118 builder.eternal(v); 119 return this; 120 } 121 122 public CacheBuilder<K, V> suppressExceptions(final boolean v) { 123 builder.suppressExceptions(v); 124 return this; 125 } 126 127 public CacheBuilder<K, V> writer(final CacheWriter<K, V> w) { 128 builder.writer(w); 129 return this; 130 } 131 132 public <T2> CacheBuilder<K, T2> valueType(final Class<T2> t) { 133 builder.valueType(t); 134 return (CacheBuilder<K, T2>) this; 135 } 136 137 public <K2> CacheBuilder<K2, V> keyType(final Class<K2> t) { 138 builder.keyType(t); 139 return (CacheBuilder<K2, V>) this; 140 } 141 142 public CacheBuilder<K, V> expiryCalculator(final ExpiryPolicy<K, V> c) { 143 builder.expiryPolicy(c); 144 return this; 145 } 146 147 public CacheBuilder<K, V> name(final Class<?> _class, final String _fieldName) { 148 builder.name(_class.getSimpleName() + "." + _fieldName); 149 return this; 150 } 151 152 public CacheBuilder<K, V> exceptionExpiryDuration(final long v, final TimeUnit u) { 153 builder.retryInterval(v, u); 154 return this; 155 } 156 157 public Cache<K, V> build() { 158 return builder.build(); 159 } 160 161 public Cache2kConfiguration createConfiguration() { 162 return builder.toConfiguration(); 163 } 164 165 public CacheBuilder<K, V> manager(final CacheManager m) { 166 builder.manager(m); 167 return this; 168 } 169 170 public CacheBuilder<K, V> storeByReference(final boolean v) { 171 builder.storeByReference(v); 172 return this; 173 } 174 175 @Deprecated 176 public Cache2kConfiguration getConfig() { 177 return builder.toConfiguration(); 178 } 179 180 public CacheBuilder<K, V> expirySecs(final int v) { 181 builder.expireAfterWrite(v, TimeUnit.SECONDS); 182 return this; 183 } 184 185 public CacheBuilder<K, V> maxSizeBound(final int v) { 186 return this; 187 } 188 189 public CacheBuilder<K, V> name(final String v) { 190 builder.name(v); 191 return this; 192 } 193 194 public CacheBuilder<K, V> source(final CacheSourceWithMetaInfo<K, V> eg) { 195 builder.loader(new AdvancedCacheLoader<K, V>() { 196 @Override 197 public V load(final K key, final long currentTime, final CacheEntry<K, V> previousEntry) throws Exception { 198 try { 199 if (previousEntry != null && previousEntry.getException() == null) { 200 return eg.get(key, currentTime, previousEntry.getValue(), previousEntry.getLastModification()); 201 } else { 202 return eg.get(key, currentTime, null, 0); 203 } 204 } catch (Exception e) { 205 throw e; 206 } catch (Throwable t) { 207 throw new RuntimeException("rethrow throwable", t); 208 } 209 } 210 }); 211 return this; 212 } 213 214 /** 215 * Removed without replacement. 216 */ 217 public CacheBuilder<K, V> implementation(final Class<?> c) { 218 return this; 219 } 220 221 public <K2> CacheBuilder<K2, V> keyType(final CacheType<K2> t) { 222 builder.keyType(t); 223 return (CacheBuilder<K2, V>) this; 224 } 225 226 public CacheBuilder<K, V> expiryMillis(final long v) { 227 builder.expireAfterWrite(v, TimeUnit.MILLISECONDS); 228 return this; 229 } 230 231 public CacheBuilder<K, V> backgroundRefresh(final boolean f) { 232 builder.refreshAhead(f); 233 return this; 234 } 235 236 public CacheBuilder<K, V> source(final CacheSource<K, V> g) { 237 builder.loader(new AdvancedCacheLoader<K, V>() { 238 @Override 239 public V load(final K key, final long currentTime, final CacheEntry<K, V> previousEntry) throws Exception { 240 try { 241 return g.get(key); 242 } catch (Exception e) { 243 throw e; 244 } catch (Throwable t) { 245 throw new RuntimeException("rethrow throwable", t); 246 } 247 } 248 }); 249 return this; 250 } 251 252 public CacheBuilder<K, V> source(final BulkCacheSource<K, V> s) { 253 builder.loader(new AdvancedCacheLoader<K, V>() { 254 @Override 255 public V load(final K key, final long currentTime, final CacheEntry<K, V> previousEntry) throws Exception { 256 CacheEntry<K, V> entry = previousEntry; 257 if (previousEntry == null) 258 entry = new CacheEntry<K, V>() { 259 @Override 260 public K getKey() { 261 return key; 262 } 263 264 @Override 265 public V getValue() { 266 return null; 267 } 268 269 @Override 270 public Throwable getException() { 271 return null; 272 } 273 274 @Override 275 public long getLastModification() { 276 return 0; 277 } 278 }; 279 try { 280 return s.getValues(Collections.singletonList(entry), currentTime).get(0); 281 } catch (Exception e) { 282 throw e; 283 } catch (Throwable t) { 284 throw new RuntimeException("rethrow throwable", t); 285 } 286 } 287 }); 288 return this; 289 } 290 291 public CacheBuilder<K, V> exceptionExpiryCalculator(final org.cache2k.ExceptionExpiryCalculator<K> c) { 292 builder.resiliencePolicy(new ResiliencePolicy<K, V>() { 293 @Override 294 public long suppressExceptionUntil(final K key, final ExceptionInformation exceptionInformation, final CacheEntry<K, V> cachedContent) { 295 return c.calculateExpiryTime(key, exceptionInformation.getException(), exceptionInformation.getLoadTime()); 296 } 297 298 @Override 299 public long retryLoadAfter(final K key, final ExceptionInformation exceptionInformation) { 300 return c.calculateExpiryTime(key, exceptionInformation.getException(), exceptionInformation.getLoadTime()); 301 } 302 }); 303 return this; 304 } 305 306 public CacheBuilder<K, V> maxSize(final int v) { 307 builder.entryCapacity(v); 308 return this; 309 } 310 311 public <T2> CacheBuilder<K, T2> valueType(final CacheType<T2> t) { 312 builder.valueType(t); 313 return (CacheBuilder<K, T2>) this; 314 } 315 316 public CacheBuilder<K, V> keepDataAfterExpired(final boolean v) { 317 builder.keepDataAfterExpired(v); 318 return this; 319 } 320 321 public CacheBuilder<K, V> refreshController(final RefreshController lc) { 322 expiryCalculator(new ExpiryPolicy<K, V>() { 323 @Override 324 public long calculateExpiryTime(K _key, V _value, long _loadTime, CacheEntry<K, V> _oldEntry) { 325 if (_oldEntry != null) { 326 return lc.calculateNextRefreshTime(_oldEntry.getValue(), _value, _oldEntry.getLastModification(), _loadTime); 327 } else { 328 return lc.calculateNextRefreshTime(null, _value, 0L, _loadTime); 329 } 330 } 331 }); 332 return this; 333 } 334 335 public CacheBuilder<K, V> refreshAhead(final boolean f) { 336 builder.refreshAhead(f); 337 return this; 338 } 339 340 public CacheBuilder<K, V> exceptionPropagator(final ExceptionPropagator ep) { 341 builder.exceptionPropagator(ep); 342 return this; 343 } 344 345 public CacheBuilder<K, V> loader(final AdvancedCacheLoader<K, V> l) { 346 builder.loader(l); 347 return this; 348 } 349 350}