public HazelcastHybridCachedReference(String name, CacheFactory localFactory, final IMap<ReferenceKey, Long> versionMap, final Supplier<V> supplier, HazelcastCacheManager cacheManager) { super(name, cacheManager); Supplier<Versioned<V>> localSupplier = new Supplier<Versioned<V>>() { @Override public Versioned<V> get() { long version = getVersion(); V value = supplier.get(); if (value == null) { throw new CacheException("The Supplier for cached reference '" + getName() + "'returned null. Null values are not supported."); } return new Versioned<V>(value, version); } }; this.versionMap = versionMap; this.localReference = localFactory.getCachedReference(name, localSupplier, getCacheSettings()); }
public DefaultReadOnlyApplicationLinkService(final ApplicationLinkService applicationLinkService, final ApplicationLinkRequestFactoryFactory requestFactoryFactory, final TransactionTemplate transactionTemplate, final EventPublisher eventPublisher, final CacheFactory cacheFactory) throws InvocationTargetException, IllegalAccessException { this.applicationLinkService = applicationLinkService; this.requestFactoryFactory = requestFactoryFactory; this.transactionTemplate = transactionTemplate; this.eventPublisher = eventPublisher; // ROTP-975 expire entries after some time to pick up applinks created by applinks 2 code links = cacheFactory.getCachedReference(DefaultReadOnlyApplicationLinkService.class.getName() + ".links", new LinkSupplier(), newCacheSettings()); }
protected CachedReference<Long> makeReference() { // Build a Cache using the builder Supplier<Long> supplier = new Supplier<Long>() { private long value = 100; @Override public Long get() { return value++; } }; return factory.getCachedReference("mycache", supplier, settingsBuilder().build()); }
private CachedReference<Long> makeExceptionalReference() { Supplier<Long> supplier = new Supplier<Long>() { private boolean called = false; @Override public Long get() { if (called) { return 42L; } called = true; throw new IllegalArgumentException(); } }; return factory.getCachedReference("mycache", supplier, settingsBuilder().build()); }
public HazelcastAsyncHybridCachedReference(String name, CacheFactory localFactory, final ITopic<ReferenceKey> topic, final Supplier<V> supplier, HazelcastCacheManager cacheManager, CacheSettings settings) { super(name, cacheManager); this.localReference = localFactory.getCachedReference(name, supplier, settings); listener = new AsyncInvalidationListener(cacheManager.getHazelcastInstance().getCluster(), localReference, topic); }
@Test public void testOnSet() throws Exception { Supplier<String> supplier = mock(Supplier.class); when(supplier.get()).thenReturn("SET"); CachedReference<String> reference = factory.getCachedReference("set", supplier, settingsBuilder().build()); CachedReferenceListener listener = mock(CachedReferenceListener.class); reference.addListener(listener, true); reference.get(); ArgumentCaptor<CachedReferenceEvent> captor = ArgumentCaptor.forClass(CachedReferenceEvent.class); verify(listener, timeout(1000)).onSet(captor.capture()); CachedReferenceEvent<String> event = captor.getValue(); assertEquals("SET", event.getValue()); }
private CachedReference<Long> makeNullReference() { return factory.getCachedReference("mycache", nullSupplier(), settingsBuilder().build()); }
@Test public void testOnReset() throws Exception { Supplier<String> supplier = mock(Supplier.class); when(supplier.get()).thenReturn("RESET"); CachedReference<String> reference = factory.getCachedReference("reset", supplier, settingsBuilder().build()); CachedReferenceListener listener = mock(CachedReferenceListener.class); reference.addListener(listener, true); reference.get(); reference.reset(); ArgumentCaptor<CachedReferenceEvent> captor = ArgumentCaptor.forClass(CachedReferenceEvent.class); //With the changes to the delegated memory reset / remove there will be another call on the //listener when this test is run as part of the suite verify(listener, timeout(1000).atLeastOnce()).onReset(captor.capture()); CachedReferenceEvent<String> event = captor.getValue(); assertEquals("RESET", event.getValue()); }
@Test public void testOnEvict() throws Exception { Supplier<String> supplier = mock(Supplier.class); when(supplier.get()).thenReturn("EVICT"); CachedReference<String> reference = factory.getCachedReference("evict", supplier, settingsBuilder().expireAfterAccess(1, TimeUnit.SECONDS).expireAfterWrite(1, TimeUnit.SECONDS).build()); CachedReferenceListener listener = mock(CachedReferenceListener.class); reference.addListener(listener, true); reference.get(); Thread.sleep(EXPIRY_TIMEOUT); reference.get(); ArgumentCaptor<CachedReferenceEvent> captor = ArgumentCaptor.forClass(CachedReferenceEvent.class); verify(listener, timeout(EXPIRY_TIMEOUT).atLeastOnce()).onEvict(captor.capture()); CachedReferenceEvent<String> event = captor.getValue(); assertEquals("EVICT", event.getValue()); }
@Test public void testFactoryGeneratedName() throws Exception { CachedReference<Long> ref = factory.getCachedReference( Object.class, "mycache", nullSupplier(), settingsBuilder().build()); assertThat(((ManagedCache)ref).getName(), equalTo("java.lang.Object.mycache")); }
final CachedReference<Integer> ref1 = factory1.getCachedReference("resetConcurrentWithLoad", supplier, settingsBuilder().build()); final CachedReference<Integer> ref2 = (factory2 != null) ? factory2.getCachedReference("resetConcurrentWithLoad", supplier, settingsBuilder().build()) : ref1;
private <V> CachedReference<V> doCreateCachedReference(String name, Supplier<V> supplier, CacheSettings settings) { if (settings.getLocal(false)) { return localCacheFactory.getCachedReference(name, supplier, settings); } // remote cached reference if (settings.getReplicateViaCopy(true)) { return createDistributedCachedReference(name, supplier, settings); } if (settings.getReplicateAsynchronously(true)) { return createAsyncHybridCachedReference(name, supplier, settings); } else { return createHybridCachedReference(name, supplier, settings); } }