Java Code Examples for javax.cache.event.EventType#CREATED

The following examples show how to use javax.cache.event.EventType#CREATED . 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: TCacheJSR107.java    From triava with Apache License 2.0 6 votes vote down vote up
void put0(K key, V value, boolean writeThrough)
{
	throwISEwhenClosed();
	kvUtil.verifyKeyAndValueNotNull(key, value);

	Action<K,V,Object> action = new PutAction<>(key, value, EventType.CREATED, false, writeThrough);

	if (actionRunner.preMutate(action))
	{
		Holders<V> holders = tcache.putToMapI(key, value, tcache.cacheTimeSpread(), false);
		final EventType eventType;
		if (holders == null)
			eventType = null;
		else
		{
			if (holders.newHolder == null || holders.newHolder.isInvalid())
				eventType = null; // new is invalid
			else
				eventType = holders.oldHolder == null ? EventType.CREATED : EventType.UPDATED;
		}
		action.setEventType(eventType);
		actionRunner.postMutate(action);	
	}
	action.close();
}
 
Example 2
Source File: TCacheJSR107.java    From triava with Apache License 2.0 6 votes vote down vote up
@Override
public boolean putIfAbsent(K key, V value)
{
	throwISEwhenClosed();

	PutAction<K,V,Object> action = new PutAction<>(key, value, EventType.CREATED, false);
	boolean added = false;
	if (actionRunnerWriteBehind.preMutate(action))
	{
		Holders<V> holders = tcache.putIfAbsentH(key, value);
		// For JSR107 putIfAbsent() should return whether a value was set.
		// It is set when the old holder was null AND the new holder is not null.
		// The latter case (new holder == null) can happen if it immediately expires.
		added = holders.oldHolder == null && holders.newHolder != null;
		if (added)
			actionRunnerWriteBehind.postMutate(action);
	}

	return added;
}
 
Example 3
Source File: BlazingCacheCacheEntryListenerWrapper.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
void onEntryCreated(K key, V value) {
    if (onCreate) {
        BlazingCacheCacheEntryEvent event = new BlazingCacheCacheEntryEvent(key, null, value, parent, EventType.CREATED, false);
        if (filter != null && !filter.evaluate(event)) {
            return;
        }
        ((CacheEntryCreatedListener) listener).onCreated(Arrays.asList(event));
    }
}
 
Example 4
Source File: UnwrapTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCacheEntryEventUnwrap() {
  MutableConfiguration<String, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(String.class, String.class);
  Cache<String, String> cache = cacheManager.createCache("cache", configuration);
  org.ehcache.event.CacheEvent<String, String> ehEvent = new EhEvent();
  Eh107CacheEntryEvent<String, String> cacheEntryEvent = new Eh107CacheEntryEvent.NormalEvent<>(cache, EventType.CREATED, ehEvent, false);
  assertThat(cacheEntryEvent.unwrap(org.ehcache.event.CacheEvent.class), is(instanceOf(CacheEvent.class)));
  assertThat(cacheEntryEvent.unwrap(cacheEntryEvent.getClass()), is(instanceOf(Eh107CacheEntryEvent.NormalEvent.class)));
}
 
Example 5
Source File: EventHandlingImpl.java    From cache2k with Apache License 2.0 5 votes vote down vote up
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 6
Source File: JCacheEntryEventTest.java    From caffeine with Apache License 2.0 4 votes vote down vote up
@Test
public void isOldValueAvailable_false() {
  JCacheEntryEvent<Integer, Integer> e =
      new JCacheEntryEvent<>(cache, EventType.CREATED, 1, false, null, 3);
  assertThat(e.isOldValueAvailable(), is(false));
}
 
Example 7
Source File: CacheEntryListenerClientServerTest.java    From cache2k with Apache License 2.0 4 votes vote down vote up
/**
 * Ensure that values can be loaded from the {@link org.jsr107.tck.event.CacheEntryListenerClient} via
 * the {@link org.jsr107.tck.event.CacheEntryListenerServer}.
 */
@Test
public void shouldHandleCacheEntryEventFromServerWithClient() {

  CacheTestSupport.MyCacheEntryListener<String, String> listener = new CacheTestSupport.MyCacheEntryListener<>();


  CacheEntryListenerServer<String, String> serverListener =
    new CacheEntryListenerServer<>(10011, String.class, String.class);
  serverListener.addCacheEventListener(listener);

  try {
    serverListener.open();

    CacheEntryListenerClient<String, String> clientListener =
      new CacheEntryListenerClient<>(serverListener.getInetAddress(), serverListener.getPort());

    TestCacheEntryEvent<String, String> event = new TestCacheEntryEvent(null, EventType.CREATED);
    event.setKey("key");
    event.setValue("value");
    event.setOldValueAvailable(false);
    ArrayList events = new ArrayList();
    events.add(event);

    clientListener.onCreated(events);
    Assert.assertThat(listener.getCreated(), is(1));

    clientListener.onRemoved(events);
    Assert.assertThat(listener.getRemoved(), is(0));

    event = new TestCacheEntryEvent(null, EventType.UPDATED);
    event.setKey("key");
    event.setValue("value");
    event.setOldValue("oldValue");
    event.setOldValueAvailable(true);

    events.clear();
    events.add(event);
    clientListener.onUpdated(events);
    Assert.assertThat(listener.getUpdated(), is(1));
    Assert.assertThat(listener.getCreated(), is(1));

  } catch (Exception e) {

  } finally {
    serverListener.close();
  }
}
 
Example 8
Source File: Listener.java    From cache2k with Apache License 2.0 4 votes vote down vote up
@Override
public EventType getEventType() {
  return EventType.CREATED;
}