org.cache2k.CacheEntry Java Examples
The following examples show how to use
org.cache2k.CacheEntry.
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: ProductHelperWithEventListener.java From tutorials with MIT License | 6 votes |
public ProductHelperWithEventListener() { cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class) .name("discount-listener") .expireAfterWrite(10, TimeUnit.MILLISECONDS) .entryCapacity(100) .loader((key) -> { cacheMissCount++; return "Sports".equalsIgnoreCase(key) ? 20 : 10; }) .addListener(new CacheEntryCreatedListener<String, Integer>() { @Override public void onEntryCreated(Cache<String, Integer> cache, CacheEntry<String, Integer> entry) { LOGGER.info("Entry created: [{}, {}].", entry.getKey(), entry.getValue()); } }) .build(); }
Example #2
Source File: TouchyJCacheAdapter.java From cache2k with Apache License 2.0 | 6 votes |
@Override public long calculateExpiryTime(K _key, V _value, long _loadTime, CacheEntry<K, V> _oldEntry) { if (_value == null) { return NO_CACHE; } Duration d; if (_oldEntry == null || _oldEntry.getException() != null) { d = policy.getExpiryForCreation(); } else { d = policy.getExpiryForUpdate(); } if (d == null) { return ExpiryTimeValues.NEUTRAL; } if (d.equals(Duration.ETERNAL)) { return ExpiryTimeValues.ETERNAL; } if (d.equals(Duration.ZERO)) { return ExpiryTimeValues.NO_CACHE; } return _loadTime + d.getTimeUnit().toMillis(d.getDurationAmount()); }
Example #3
Source File: TimingHandlerTest.java From cache2k with Apache License 2.0 | 6 votes |
/** * The configuration setting serves as a time limit. */ @Test public void expireAfterWrite_policy_limit() { TimingHandler h = TimingHandler.of( CLOCK, Cache2kBuilder.forUnknownTypes() .expiryPolicy(new ExpiryPolicy() { @Override public long calculateExpiryTime(Object key, Object value, long loadTime, CacheEntry oldEntry) { return ETERNAL; } }) .expireAfterWrite(5, TimeUnit.MINUTES) .toConfiguration() ); Entry e = new Entry(); long t = h.calculateNextRefreshTime(e, null, NOW); assertNotEquals(Long.MAX_VALUE, t); assertEquals(NOW + TimeUnit.MINUTES.toMillis(5), t); t = h.calculateNextRefreshTime(e, null, NOW); assertEquals(NOW + TimeUnit.MINUTES.toMillis(5), t); }
Example #4
Source File: ListenerTest.java From cache2k with Apache License 2.0 | 6 votes |
@Test public void customExecutor() { final AtomicInteger _counter = new AtomicInteger(); Cache<Integer, Integer> c = Cache2kBuilder.of(Integer.class, Integer.class) .addAsyncListener(new CacheEntryCreatedListener<Integer, Integer>() { @Override public void onEntryCreated(final Cache<Integer, Integer> cache, final CacheEntry<Integer, Integer> entry) { } }) .asyncListenerExecutor(new Executor() { @Override public void execute(final Runnable command) { _counter.incrementAndGet(); } }) .build(); c.put(1,2); c.close(); assertEquals(1, _counter.get()); }
Example #5
Source File: EntryProcessorTest.java From cache2k with Apache License 2.0 | 6 votes |
@Test public void load_changeRefreshTimeInLoader() { final long _probeTime = 4711; Cache<Integer,Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() { @Override public void extend(final Cache2kBuilder<Integer, Integer> b) { b.recordRefreshedTime(true) .wrappingLoader(new AdvancedCacheLoader<Integer, LoadDetail<Integer>>() { @Override public LoadDetail<Integer> load(final Integer key, final long startTime, final CacheEntry<Integer, LoadDetail<Integer>> currentEntry) throws Exception { return Loaders.wrapRefreshedTime(key, _probeTime); } }); } }); c.get(1); c.invoke(1, new EntryProcessor<Integer, Integer, Object>() { @Override public Object process(final MutableCacheEntry<Integer, Integer> e) { assertEquals(_probeTime, e.getRefreshedTime()); return null; } }); }
Example #6
Source File: TimingHandlerTest.java From cache2k with Apache License 2.0 | 6 votes |
@Test public void almostEternal_expiryPolicy_noOverflow() { long _BIG_VALUE = Long.MAX_VALUE - 47; TimingHandler h = TimingHandler.of( CLOCK, Cache2kBuilder.forUnknownTypes() .expiryPolicy(new ExpiryPolicy() { @Override public long calculateExpiryTime(final Object key, final Object value, final long loadTime, final CacheEntry oldEntry) { return ETERNAL; } }) .expireAfterWrite(_BIG_VALUE, TimeUnit.MILLISECONDS) .toConfiguration() ); long t = h.calculateNextRefreshTime(ENTRY, null, 0); assertEquals(_BIG_VALUE, t); t = h.calculateNextRefreshTime(ENTRY, null, 48); assertEquals(Long.MAX_VALUE, t); }
Example #7
Source File: EntryProcessorTest.java From cache2k with Apache License 2.0 | 6 votes |
@Test public void load_changeRefreshTimeInLoaderNoRecord() { final long _probeTime = 4711; Cache<Integer,Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() { @Override public void extend(final Cache2kBuilder<Integer, Integer> b) { b.wrappingLoader(new AdvancedCacheLoader<Integer, LoadDetail<Integer>>() { @Override public LoadDetail<Integer> load(final Integer key, final long startTime, final CacheEntry<Integer, LoadDetail<Integer>> currentEntry) throws Exception { return Loaders.wrapRefreshedTime(key, _probeTime); } }); } }); c.get(1); c.invoke(1, new EntryProcessor<Integer, Integer, Object>() { @Override public Object process(final MutableCacheEntry<Integer, Integer> e) { assertEquals(0, e.getRefreshedTime()); return null; } }); }
Example #8
Source File: CacheLoaderTest.java From cache2k with Apache License 2.0 | 6 votes |
@Test public void testAdvancedLoader() { Cache<Integer,Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() { @Override public void extend(final Cache2kBuilder<Integer, Integer> b) { b .loader(new AdvancedCacheLoader<Integer, Integer>() { @Override public Integer load(final Integer key, long startTime, CacheEntry<Integer,Integer> e) throws Exception { return key * 2; } }); } }); assertEquals((Integer) 10, c.get(5)); assertEquals((Integer) 20, c.get(10)); assertFalse(c.containsKey(2)); assertTrue(c.containsKey(5)); }
Example #9
Source File: ExpiryTest.java From cache2k with Apache License 2.0 | 6 votes |
/** * Refresh ahead is on but the expiry policy returns 0. * That is a contradiction. Expiry policy overrules refresh ahead, the * entry is expired and not visible. */ @Test public void testRefreshButNoRefreshIfAlreadyExpiredZeroTime() { IntCountingCacheSource _countingLoader = new IntCountingCacheSource(); Cache<Integer, Integer> c = builder(Integer.class, Integer.class) .refreshAhead(true) .eternal(true) .expiryPolicy(new ExpiryPolicy<Integer, Integer>() { @Override public long calculateExpiryTime(final Integer key, final Integer value, final long loadTime, final CacheEntry<Integer, Integer> oldEntry) { return 0; } }) .loader(_countingLoader) .build(); c.get(1); assertFalse(c.containsKey(1)); }
Example #10
Source File: ExpiryTest.java From cache2k with Apache License 2.0 | 6 votes |
@Test public void testImmediateExpireAfterPut() { final Cache<Integer, Integer> c = cache = builder(Integer.class, Integer.class) .expiryPolicy(new ExpiryPolicy<Integer, Integer>() { @Override public long calculateExpiryTime(final Integer key, final Integer value, final long loadTime, final CacheEntry<Integer, Integer> oldEntry) { if (oldEntry == null) { return ExpiryPolicy.ETERNAL; } return ExpiryPolicy.NO_CACHE; } }) .keepDataAfterExpired(false) .sharpExpiry(true) .build(); c.put(1, 1); assertEquals(1, getInfo().getPutCount()); assertEquals(1, getInfo().getSize()); c.put(1,3); assertEquals(0, getInfo().getSize()); assertEquals(1, getInfo().getPutCount()); }
Example #11
Source File: ExpiryTest.java From cache2k with Apache License 2.0 | 6 votes |
@Test public void testImmediateExpireWithExpiryCalculator() { final Cache<Integer, Integer> c = cache = builder(Integer.class, Integer.class) .loader(new IntCountingCacheSource()) .expiryPolicy(new ExpiryPolicy<Integer, Integer>() { @Override public long calculateExpiryTime(final Integer key, final Integer value, final long loadTime, final CacheEntry<Integer, Integer> oldEntry) { return 0; } }) .keepDataAfterExpired(false) .sharpExpiry(true) .build(); c.get(1); assertEquals(0, getInfo().getSize()); c.put(3,3); assertEquals(0, getInfo().getSize()); }
Example #12
Source File: OsgiIT.java From cache2k with Apache License 2.0 | 6 votes |
/** * Simple test to see whether event package is exported. */ @Test public void testEventPackage() { CacheManager m = CacheManager.getInstance("testEventPackage"); final AtomicInteger _count = new AtomicInteger(); Cache<String, String> c = new Cache2kBuilder<String, String>() {} .manager(m) .eternal(true) .addListener(new CacheEntryCreatedListener<String, String>() { @Override public void onEntryCreated(final Cache<String, String> cache, final CacheEntry<String, String> entry) { _count.incrementAndGet(); } }) .build(); c.put("abc", "123"); assertTrue(c.containsKey("abc")); assertEquals("123", c.peek("abc")); assertEquals(1, _count.get()); c.close(); }
Example #13
Source File: ExtraSpringCache2kCacheTest.java From cache2k with Apache License 2.0 | 6 votes |
@Test public void testLoadingCacheAdvancedLoader() { SpringCache2kCache cacheWithLoader = new SpringCache2kCacheManager().addCache( Cache2kBuilder.forUnknownTypes() .name(ExtraSpringCache2kCacheTest.class.getSimpleName() + "-withAdvancedLoader") .loader(new AdvancedCacheLoader() { @Override public Object load( final Object key, final long startTime, final CacheEntry currentEntry) { return "123"; } })); assertTrue(cacheWithLoader.isLoaderPresent()); Object v = cacheWithLoader.get("321", new Callable<Object>() { @Override public Object call() throws Exception { fail("this is never called"); return null; } }); assertEquals("123", v); cacheWithLoader.getNativeCache().close(); }
Example #14
Source File: ExpiryTest.java From cache2k with Apache License 2.0 | 6 votes |
@Test public void testImmediateExpireAfterUpdate() { final Cache<Integer, Integer> c = cache = builder(Integer.class, Integer.class) .loader(new IntCountingCacheSource()) .expiryPolicy(new ExpiryPolicy<Integer, Integer>() { @Override public long calculateExpiryTime(final Integer key, final Integer value, final long loadTime, final CacheEntry<Integer, Integer> oldEntry) { if (oldEntry == null) { return ExpiryPolicy.ETERNAL; } return ExpiryPolicy.NO_CACHE; } }) .keepDataAfterExpired(false) .sharpExpiry(true) .build(); c.get(1); assertEquals(1, getInfo().getSize()); c.put(1,3); assertEquals(0, getInfo().getSize()); assertEquals(0, getInfo().getPutCount()); }
Example #15
Source File: ExpiryWithExpiryListenerTest.java From cache2k with Apache License 2.0 | 5 votes |
@Override protected <K, T> Cache2kBuilder<K, T> builder(final Class<K> k, final Class<T> t) { return super.builder(k,t).addListener(new CacheEntryExpiredListener<K, T>() { @Override public void onEntryExpired(final Cache<K, T> cache, final CacheEntry<K, T> entry) { } }); }
Example #16
Source File: InMemoryCache.java From orianna with MIT License | 5 votes |
@Override public long calculateExpiryTime(final Integer key, final Object value, final long loadTime, final CacheEntry<Integer, Object> oldEntry) { final Long period = expirationPeriods.get(value.getClass()); if(period != null && period > 0L) { return loadTime + period.longValue(); } return ExpiryTimeValues.ETERNAL; }
Example #17
Source File: BasicCacheOperationsWithoutCustomizationsTest.java From cache2k with Apache License 2.0 | 5 votes |
@Test public void getEntry_lastModification() { cache.put(KEY, VALUE); CacheEntry<Integer, Integer> e = cache.getEntry(KEY); try { long t = cache.peekEntry(e.getKey()).getLastModification(); fail("expected UnsupportedOperationException"); } catch (UnsupportedOperationException ex) { } }
Example #18
Source File: BasicCacheOperationsWithoutCustomizationsTest.java From cache2k with Apache License 2.0 | 5 votes |
@Test public void getEntry() { cache.put(KEY, VALUE); CacheEntry<Integer, Integer> e = cache.getEntry(KEY); assertEquals(KEY, e.getKey()); assertEquals(VALUE, e.getValue()); assertNull(e.getException()); checkRefreshTime(e); }
Example #19
Source File: BasicCacheOperationsWithoutCustomizationsTest.java From cache2k with Apache License 2.0 | 5 votes |
@Test public void peekEntry_lastModification() { cache.put(KEY, VALUE); CacheEntry<Integer, Integer> e = cache.peekEntry(KEY); try { long t = cache.peekEntry(e.getKey()).getLastModification(); fail("expected UnsupportedOperationException"); } catch (UnsupportedOperationException ex) { } }
Example #20
Source File: BasicCacheOperationsWithoutCustomizationsTest.java From cache2k with Apache License 2.0 | 5 votes |
@Test public void peekEntry_Exception() { assignException(KEY); CacheEntry<Integer, Integer> e = cache.peekEntry(KEY); assertEquals(KEY, e.getKey()); entryHasException(e); assertEquals(OUCH, e.getException()); }
Example #21
Source File: BasicCacheOperationsWithoutCustomizationsTest.java From cache2k with Apache License 2.0 | 5 votes |
@Test public void getEntry_Null() { cache.put(KEY, null); CacheEntry<Integer, Integer> e = cache.getEntry(KEY); assertEquals(KEY, e.getKey()); assertNull(e.getValue()); checkRefreshTime(e); }
Example #22
Source File: ExpiryTest.java From cache2k with Apache License 2.0 | 5 votes |
@Test public void testFetchAlwaysWithVariableExpiryInPastAfterLoad() { IntCountingCacheSource g = new IntCountingCacheSource(); final Cache<Integer, Integer> c = cache = Cache2kBuilder.of(Integer.class, Integer.class) .loader(g) .expiryPolicy(new ExpiryPolicy<Integer, Integer>() { @Override public long calculateExpiryTime(final Integer key, final Integer value, final long loadTime, final CacheEntry<Integer, Integer> oldEntry) { sleep(3); return loadTime + 1; } }) .expireAfterWrite(0, TimeUnit.SECONDS).build(); checkAlwaysLoaded(g, c); }
Example #23
Source File: SlowExpiryTest.java From cache2k with Apache License 2.0 | 5 votes |
/** Entry does not go into probation if eternal */ @Test public void refresh_sharp_noKeep_eternalAfterRefresh() throws Exception { final int KEY = 1; final CountingLoader _LOADER = new CountingLoader(); final Cache<Integer, Integer> c = builder(Integer.class, Integer.class) .refreshAhead(true) .sharpExpiry(true) .eternal(true) .keepDataAfterExpired(false) .expiryPolicy(new ExpiryPolicy<Integer, Integer>() { @Override public long calculateExpiryTime(final Integer key, final Integer value, final long loadTime, final CacheEntry<Integer, Integer> oldEntry) { if (oldEntry != null) { return ETERNAL; } return loadTime + TestingParameters.MINIMAL_TICK_MILLIS; } }) .loader(_LOADER) .build(); int v = c.get(KEY); assertEquals(0, v); await("Refresh is done", new Condition() { @Override public boolean check() throws Exception { return getInfo().getRefreshCount() > 0; } }); await("Entry stays", new Condition() { @Override public boolean check() throws Exception { return c.containsKey(KEY); } }); }
Example #24
Source File: BasicCacheOperationsWithoutCustomizationsTest.java From cache2k with Apache License 2.0 | 5 votes |
private static void entryHasException(final CacheEntry<Integer, Integer> e) { try { e.getValue(); fail("exception expected"); } catch (CacheLoaderException ex) { } assertNotNull(e.getException()); }
Example #25
Source File: ExpiryTest.java From cache2k with Apache License 2.0 | 5 votes |
/** * Switching to eternal means exceptions expire immediately. */ @Test public void testEternal_keepData() throws Exception { IntCountingCacheSource g = new IntCountingCacheSource() { @Override public Integer load(final Integer o) { incrementLoadCalledCount(); if (o == 99) { throw new RuntimeException("ouch"); } return o; } }; final Cache<Integer, Integer> c = cache = builder(Integer.class, Integer.class) .loader(g) .eternal(true) .keepDataAfterExpired(true) .build(); assertEquals("no miss", 0, g.getLoaderCalledCount()); c.get(1802); assertEquals("one miss", 1, g.getLoaderCalledCount()); c.get(1802); assertEquals("one miss", 1, g.getLoaderCalledCount()); assertTrue(((InternalCache) c).getEntryState(1802).contains("nextRefreshTime=ETERNAL")); try { Integer obj = c.get(99); fail("exception expected"); } catch (CacheLoaderException ex) { assertTrue("no expiry on exception", !ex.toString().contains(EXPIRY_MARKER)); assertTrue(ex.getCause() instanceof RuntimeException); } assertEquals("miss", 2, g.getLoaderCalledCount()); assertTrue(((InternalCache) c).getEntryState(99).contains("state=4")); CacheEntry<Integer, Integer> e = c.getEntry(99); entryHasException(e); assertEquals(RuntimeException.class, e.getException().getClass()); assertEquals("miss", 3, g.getLoaderCalledCount()); }
Example #26
Source File: ExpiryTest.java From cache2k with Apache License 2.0 | 5 votes |
@Test public void testRefreshIfAlreadyExpiredLoadTime() { final int _COUNT = 3; Cache<Integer, Integer> c = builder(Integer.class, Integer.class) .refreshAhead(true) .eternal(true) .expiryPolicy(new ExpiryPolicy<Integer, Integer>() { @Override public long calculateExpiryTime(final Integer key, final Integer value, final long loadTime, final CacheEntry<Integer, Integer> oldEntry) { return loadTime; } }) .loader(new IdentIntSource()) .build(); c.get(1); c.get(2); c.get(3); await("All refreshed", new Condition() { @Override public boolean check() { return getInfo().getRefreshCount() + getInfo().getRefreshFailedCount() >= _COUNT; } }); await("All expired", new Condition() { @Override public boolean check() { return getInfo().getExpiredCount() >= _COUNT; } }); }
Example #27
Source File: EventHandlingImpl.java From cache2k with Apache License 2.0 | 5 votes |
private void fireCreated(final javax.cache.Cache<K, V> _jCache, final CacheEntry<K, V> e) { EntryEvent<K, V> cee = new EntryEvent<K, V>(_jCache, EventType.CREATED, e.getKey(), extractValue(e.getValue())); asyncDispatcher.deliverAsyncEvent(cee); for (Listener<K,V> t : createdListener) { t.fire(cee); } }
Example #28
Source File: ExpiryTest.java From cache2k with Apache License 2.0 | 5 votes |
private static void entryHasException(final CacheEntry<Integer, Integer> e) { try { e.getValue(); fail("exception expected"); } catch (CacheLoaderException ex) { } assertNotNull(e.getException()); }
Example #29
Source File: ExpiryTest.java From cache2k with Apache License 2.0 | 5 votes |
/** * Refresh ahead is on but the expiry policy returns 0. * That is a contradiction. Expiry policy overrules refresh ahead, the * entry is expired and not visible. */ @Test // enabled again 23.8.2016;jw @Ignore("no keep and refresh ahead is prevented") public void testRefreshNoKeepButNoRefreshIfAlreadyExpiredZeroTimeCheckCounters() { final int _COUNT = 3; IntCountingCacheSource _countingLoader = new IntCountingCacheSource(); Cache<Integer, Integer> c = builder(Integer.class, Integer.class) .refreshAhead(true) .eternal(true) .expiryPolicy(new ExpiryPolicy<Integer, Integer>() { @Override public long calculateExpiryTime(final Integer key, final Integer value, final long loadTime, final CacheEntry<Integer, Integer> oldEntry) { return 0; } }) .loader(_countingLoader) .keepDataAfterExpired(false) .build(); c.get(1); c.get(2); c.get(3); await("All expired", new Condition() { @Override public boolean check() { return getInfo().getExpiredCount() >= _COUNT; } }); assertEquals(0, getInfo().getRefreshCount() + getInfo().getRefreshFailedCount()); assertEquals(_COUNT, _countingLoader.getLoaderCalledCount()); assertEquals(_COUNT, getInfo().getExpiredCount()); }
Example #30
Source File: ExpiryTest.java From cache2k with Apache License 2.0 | 5 votes |
/** * Refresh ahead is on but the expiry policy returns 0. * That is a contradiction. Expiry policy overrules refresh ahead, the * entry is expired and not visible. */ @Test public void testRefreshButNoRefreshIfAlreadyExpiredZeroTimeCheckCounters() { final int _COUNT = 3; IntCountingCacheSource _countingLoader = new IntCountingCacheSource(); Cache<Integer, Integer> c = builder(Integer.class, Integer.class) .refreshAhead(true) .eternal(true) .expiryPolicy(new ExpiryPolicy<Integer, Integer>() { @Override public long calculateExpiryTime(final Integer key, final Integer value, final long loadTime, final CacheEntry<Integer, Integer> oldEntry) { return 0; } }) .loader(_countingLoader) .build(); c.get(1); c.get(2); c.get(3); await("All expired", new Condition() { @Override public boolean check() { return getInfo().getExpiredCount() >= _COUNT; } }); assertEquals(0, getInfo().getRefreshCount() + getInfo().getRefreshFailedCount()); assertEquals(_COUNT, _countingLoader.getLoaderCalledCount()); assertEquals(_COUNT, getInfo().getExpiredCount()); }