Java Code Examples for org.cache2k.Cache2kBuilder#build()
The following examples show how to use
org.cache2k.Cache2kBuilder#build() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Cache2kCacheFactory.java From joyrpc with Apache License 2.0 | 6 votes |
@Override public <K, V> Cache<K, V> build(final String name, final CacheConfig<K, V> config) { Cache2kBuilder<K, CacheObject<V>> builder = Cache2kBuilder.forUnknownTypes(); if (config.getKeyClass() != null) { builder.keyType(config.getKeyClass()); } builder.valueType(CacheObject.class); builder.permitNullValues(config.isNullable()); builder.entryCapacity(config.getCapacity() > 0 ? config.getCapacity() : Long.MAX_VALUE); if (config.getExpireAfterWrite() > 0) { builder.expireAfterWrite(config.getExpireAfterWrite(), TimeUnit.MILLISECONDS); } else { builder.eternal(true); } return new Cache2kCache<>(builder.build(), config); }
Example 2
Source File: CacheRule.java From cache2k with Apache License 2.0 | 6 votes |
Cache<K, V> buildCache() { String _name = description.getTestClass().getName(); Cache2kBuilder b = getInitialBuilder(); for (Specialization sp : configurationSpecialization) { sp.extend(b); } if (shared) { b.name(description.getTestClass()); sharedCache.put(_name, _name); } else { if (sharedCache.containsKey(_name)) { throw new IllegalArgumentException("Shared cache usage: Method rule must be identical instance."); } b.name(description.getTestClass(), description.getMethodName()); } return b.build(); }
Example 3
Source File: Cache2kBuilderTest.java From cache2k with Apache License 2.0 | 6 votes |
private void cacheClosedEventFired(boolean _wiredCache) { final AtomicBoolean _FIRED = new AtomicBoolean(); Cache2kBuilder _builder = Cache2kBuilder.forUnknownTypes(); _builder = _builder.addCacheClosedListener(new CacheClosedListener() { @Override public void onCacheClosed(final Cache cache) { _FIRED.set(true); } }); if (_wiredCache) { StaticUtil.enforceWiredCache(_builder); } Cache c = _builder.build(); c.close(); assertTrue(_FIRED.get()); }
Example 4
Source File: LocalCache.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Constructor to instantiate LocalCache object. * * @param cacheBuilder CacheBuilder instance */ @SuppressWarnings("unchecked") public LocalCache( final CacheBuilder<V> cacheBuilder ) { Cache2kBuilder<?, ?> builder = Cache2kBuilder.forUnknownTypes(); if ( cacheBuilder.isExpiryEnabled() ) { builder.eternal( false ); if ( cacheBuilder.isRefreshExpiryOnAccess() ) { // TODO https://github.com/cache2k/cache2k/issues/39 is still // Open. Once the issue is resolved it can be updated here builder.expireAfterWrite( cacheBuilder.getExpiryInSeconds(), SECONDS ); } else { builder.expireAfterWrite( cacheBuilder.getExpiryInSeconds(), SECONDS ); } } else { builder.eternal( true ); } if ( cacheBuilder.getMaximumSize() > 0 ) { builder.entryCapacity( cacheBuilder.getMaximumSize() ); } // Using unknown typed key for builder and casting it this.cache2kInstance = (org.cache2k.Cache<String, V>) builder.build(); this.defaultValue = cacheBuilder.getDefaultValue(); }
Example 5
Source File: Cache2kFactory.java From cache2k-benchmark with Apache License 2.0 | 5 votes |
private <K,V> Cache<K, V> createInternal(final Class<K> _keyType, final Class<V> _valueType, final int _maxElements, final BenchmarkCacheLoader<K, V> _source) { Cache2kBuilder<K, V> b = Cache2kBuilder.of(_keyType, _valueType) .name("testCache-" + counter.incrementAndGet()) .entryCapacity(_maxElements) .refreshAhead(false) .strictEviction(strictEviction); if (withExpiry) { b.expireAfterWrite(2 * 60, TimeUnit.SECONDS); } else { b.eternal(true); } if (disableStatistics) { b.disableStatistics(true).strictEviction(false).boostConcurrency(true); } else { b.strictEviction(true); } final AtomicInteger _evictCount = new AtomicInteger(); if (_source != null) { b.loader(new CacheLoader<K, V>() { @Override public V load(final K key) throws Exception { return _source.load(key); } }); } return b.build(); }
Example 6
Source File: SpringCache2kCacheManager.java From cache2k with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") static SpringCache2kCache buildAndWrap(Cache2kBuilder<?, ?> builder) { org.cache2k.Cache nativeCache = builder.build(); Cache2kConfiguration<?,?> cfg = builder.toConfiguration(); boolean loaderPresent = cfg.getLoader() != null || cfg.getAdvancedLoader() != null; return loaderPresent ? new SpringLoadingCache2kCache(nativeCache) : new SpringCache2kCache(nativeCache); }
Example 7
Source File: IntegrationTest.java From cache2k with Apache License 2.0 | 5 votes |
@Test public void defaultAndIndividualIsApplied() { Cache2kBuilder<String, String> b = new Cache2kBuilder<String, String>(){}.name("IntegrationTest-defaultAndIndividualIsApplied"); Cache2kConfiguration<String, String> cfg = b.toConfiguration(); assertEquals(-1, cfg.getEntryCapacity()); assertEquals(5, cfg.getLoaderThreadCount()); assertEquals(-1, cfg.getExpireAfterWrite()); Cache<String, String> c = b.build(); assertEquals(-1, cfg.getEntryCapacity()); assertEquals(47000, cfg.getExpireAfterWrite()); c.close(); }
Example 8
Source File: IntegrationTest.java From cache2k with Apache License 2.0 | 5 votes |
@Test public void validateVariableExpansion() { Cache2kBuilder b = new Cache2kBuilder<String, String>() { } .manager(CacheManager.getInstance("all")) .name("jcache1"); Cache2kConfiguration cfg = b.toConfiguration(); Cache c = b.build(); assertEquals(1153, cfg.getEntryCapacity()); assertEquals(123000, cfg.getMaxRetryInterval()); c.close(); }
Example 9
Source File: TestingBase.java From cache2k with Apache License 2.0 | 5 votes |
protected <K, T> Cache<K, T> freshCache( Class<K> _keyClass, Class<T> _dataClass, CacheLoader g, long _maxElements, int _expiry) { Cache2kBuilder<K, T> b = builder(_keyClass, _dataClass).loader(g).refreshAhead(_expiry >= 0 && g != null); if (_expiry < 0) { b.eternal(true); } else { b.expireAfterWrite(_expiry, TimeUnit.SECONDS); } applyMaxElements(b, _maxElements); return cache = b.build(); }
Example 10
Source File: CacheManagerAndCacheLifeCycleTest.java From cache2k with Apache License 2.0 | 5 votes |
@Test public void onlyOneCacheForWired() { String _uniqueName = this.getClass().getName() + ".onlyOneCacheForWired"; CacheManager cm = CacheManager.getInstance(_uniqueName); Cache2kBuilder b = Cache2kBuilder.forUnknownTypes().manager(cm); StaticUtil.enforceWiredCache(b); Cache c = b.build(); assertEquals("one cache active", 1, StaticUtil.count(cm.getActiveCaches())); cm.close(); }
Example 11
Source File: Cache2kBuilderTest.java From cache2k with Apache License 2.0 | 5 votes |
@Test public void cacheRemovedAfterClose_WiredCache() { final String _NAME = this.getClass().getSimpleName() + "-cacheRemovedAfterCloseWiredCache"; CacheManager cm = CacheManager.getInstance(_NAME); Cache2kBuilder _builder = Cache2kBuilder.forUnknownTypes() .manager(cm) .name(_NAME); StaticUtil.enforceWiredCache(_builder); Cache c = _builder.build(); assertEquals(c, cm.getActiveCaches().iterator().next()); c.close(); assertFalse(cm.getActiveCaches().iterator().hasNext()); }
Example 12
Source File: BasicCacheOperationsWithoutCustomizationsTest.java From cache2k with Apache License 2.0 | 5 votes |
protected Cache<Integer,Integer> createCache() { Cache2kBuilder b; if (pars.useObjectKey) { b = Cache2kBuilder.forUnknownTypes(); } else { b = Cache2kBuilder.of(Integer.class, Integer.class); } b.name(this.getClass().getSimpleName() + "-" + pars.toString().replace('=', '~')) .retryInterval(Long.MAX_VALUE, TimeUnit.MILLISECONDS) .entryCapacity(1000) .permitNullValues(true) .keepDataAfterExpired(pars.keepDataAfterExpired) .recordRefreshedTime(pars.recordRefreshTime) .disableStatistics(pars.disableStatistics); if (pars.withExpiryAfterWrite) { b.expireAfterWrite(TestingParameters.MAX_FINISH_WAIT_MILLIS, TimeUnit.MILLISECONDS); } else { b.eternal(true); } if (pars.withWiredCache) { StaticUtil.enforceWiredCache(b); } if (pars.withExpiryListener) { b.addListener(new CacheEntryExpiredListener() { @Override public void onEntryExpired(final Cache cache, final CacheEntry entry) { } }); } Cache<Integer,Integer> c = b.build(); if (pars.withEntryProcessor) { c = new EntryProcessorCacheWrapper<Integer, Integer>(c); } if (pars.withForwardingAndAbstract) { c = wrapAbstractAndForwarding(c); } return c; }
Example 13
Source File: XmlConfigurationTest.java From cache2k with Apache License 2.0 | 5 votes |
@Test public void sectionIsThere() { Cache2kBuilder<String, String> b = new Cache2kBuilder<String, String>(){} .manager(CacheManager.getInstance(MANAGER_NAME)) .name("withSection"); Cache2kConfiguration<String, String> cfg = b.toConfiguration(); Cache<String, String> c = b.build(); assertEquals("default is false", false, new JCacheConfiguration().isCopyAlwaysIfRequested()); assertNotNull("section present", cfg.getSections().getSection(JCacheConfiguration.class)); assertEquals("config applied", true, cfg.getSections().getSection(JCacheConfiguration.class).isCopyAlwaysIfRequested()); c.close(); }
Example 14
Source File: XmlConfigurationTest.java From cache2k with Apache License 2.0 | 5 votes |
@Test public void sectionIsThereViaStandardElementName() { Cache2kBuilder<String, String> b = new Cache2kBuilder<String, String>(){} .manager(CacheManager.getInstance(MANAGER_NAME)) .name("withJCacheSection"); Cache2kConfiguration<String, String> cfg = b.toConfiguration(); Cache<String, String> c = b.build(); assertEquals("default is false", false, new JCacheConfiguration().isCopyAlwaysIfRequested()); assertNotNull("section present", cfg.getSections().getSection(JCacheConfiguration.class)); assertEquals("config applied", true, cfg.getSections().getSection(JCacheConfiguration.class).isCopyAlwaysIfRequested()); c.close(); }
Example 15
Source File: SlowExpiryTest.java From cache2k with Apache License 2.0 | 4 votes |
public void testExceptionWithRefreshAndLoader(boolean asyncLoader) { String _cacheName = generateUniqueCacheName(this); final int COUNT = 4; final long TIMESPAN = TestingParameters.MINIMAL_TICK_MILLIS; final Cache2kBuilder<Integer, Integer> cb = builder(_cacheName, Integer.class, Integer.class) .refreshAhead(true) .retryInterval(TIMESPAN, TimeUnit.MILLISECONDS); if (asyncLoader) { cb.loader(new AsyncCacheLoader<Integer, Integer>() { @Override public void load(final Integer key, final Context<Integer, Integer> context, final Callback<Integer> callback) throws Exception { throw new RuntimeException("always"); } }); } else { cb.loader(new BasicCacheTest.AlwaysExceptionSource()); } final Cache<Integer, Integer> c = cb.build(); cache = c; within(TIMESPAN) .work(new Runnable() { @Override public void run() { for (int i = 1; i <= COUNT; i++) { try { c.get(i); fail("expect exception"); } catch (CacheLoaderException ignore) { } } } }) .check(new Runnable() { @Override public void run() { assertEquals(COUNT, getInfo().getSize()); assertEquals("no refresh yet", 0, getInfo().getRefreshCount() + getInfo().getRefreshFailedCount() ); } } ); await("All refreshed", new Condition() { @Override public boolean check() { return getInfo().getRefreshCount() + getInfo().getRefreshFailedCount() >= COUNT; } }); assertEquals("no internal exceptions",0, getInfo().getInternalExceptionCount()); assertTrue("got at least 8 - submitFailedCnt exceptions", getInfo().getLoadExceptionCount() >= getInfo().getRefreshFailedCount()); assertTrue("no alert", getInfo().getHealth().isEmpty()); await("All expired", new Condition() { @Override public boolean check() { return getInfo().getExpiredCount() >= COUNT; } }); assertEquals(0, getInfo().getSize()); }