Java Code Examples for org.ehcache.event.EventType

Following code examples demonstrate how to use org.ehcache.event.EventTypefrom ehcache. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.ehcache.event.EventTypeand various code implementation of this class.

	public void onEvent(CacheEvent<K, V> event) {

		LOGGER.debug("cacheEventType:" + event.getType().name());
		LOGGER.debug("cacheKey:" + event.getKey());
		LOGGER.debug("cacheNewValue:" + event.getNewValue());
		LOGGER.debug("cacheOldValue:" + event.getOldValue());

		String key = event.getKey().toString();
		String alias = key.substring(0, key.indexOf("."));
		if (null == alias || alias.equals("")) {
			return;
		}

		if (event.getType() == EventType.CREATED
				|| event.getType() == EventType.UPDATED) {
			ehcacheManager.putKeyCache(alias, key);
		} else {
			ehcacheManager.removeKeyCache(alias, key);
		}

		return;
	} 


    public void onEvent(CacheEvent<String, SessionData> event) {
        if (event.getType() == EventType.EXPIRED || event.getType() == EventType.EVICTED) {
            logger.info("Expired session: user=" + event.getOldValue().getUsername() + ", token=" + event.getOldValue().getToken());
        }
    } 

  public void unparseServiceConfiguration() {
    DefaultCacheEventListenerConfiguration listenerConfig =
      new DefaultCacheEventListenerConfiguration(EnumSet.of(CREATED, REMOVED), TestCacheEventListener.class);
    listenerConfig.setEventFiringMode(SYNCHRONOUS);
    listenerConfig.setEventOrderingMode(UNORDERED);

    CacheConfiguration<?, ?> cacheConfig = newCacheConfigurationBuilder(Object.class, Object.class, heap(10)).add(listenerConfig).build();
    CacheType cacheType = new CacheType();
    cacheType = new DefaultCacheEventListenerConfigurationParser().unparseServiceConfiguration(cacheConfig, cacheType);

    List<ListenersType.Listener> listeners = cacheType.getListeners().getListener();
    assertThat(listeners).hasSize(1);
    ListenersType.Listener listener = listeners.get(0);
    assertThat(listener.getEventFiringMode()).isEqualTo(EventFiringType.SYNCHRONOUS);
    assertThat(listener.getEventOrderingMode()).isEqualTo(EventOrderingType.UNORDERED);
    assertThat(listener.getEventsToFireOn()).contains(org.ehcache.xml.model.EventType.CREATED, org.ehcache.xml.model.EventType.REMOVED);
  } 

  public void parseServiceConfiguration() throws Exception {
    CacheConfiguration<?, ?> cacheConfiguration = new XmlConfiguration(getClass().getResource("/configs/ehcache-cacheEventListener.xml")).getCacheConfigurations().get("bar");

    DefaultCacheEventListenerConfiguration listenerConfig =
      findSingletonAmongst(DefaultCacheEventListenerConfiguration.class, cacheConfiguration.getServiceConfigurations());

    assertThat(listenerConfig).isNotNull();
    assertThat(listenerConfig.getClazz()).isEqualTo(TestCacheEventListener.class);
    assertThat(listenerConfig.firingMode()).isEqualTo(SYNCHRONOUS);
    assertThat(listenerConfig.orderingMode()).isEqualTo(UNORDERED);
    assertThat(listenerConfig.fireOn()).containsExactlyInAnyOrder(EventType.values());
  } 

    public void onEvent(CacheEvent cacheEvent) {
        if (cacheEvent.getType().equals(EventType.EXPIRED)) {
            Object objKey = cacheEvent.getKey();
            if (null != objKey) {
                String key = objKey.toString();
                redisStorage.del(key);
                Console.println("[K-Cache] 数据已过期, key:" + objKey);
            }
        }
    } 

	public void onEvent(CacheEvent<? extends Integer, ? extends Integer> cacheEvent) {

		if (cacheEvent.getType().equals(EventType.CREATED)) {
			log.info("Cache event CREATED for item with key {}. Old value = {}, New value = {}", cacheEvent.getKey(),
					cacheEvent.getOldValue(), cacheEvent.getNewValue());
		}
		if (cacheEvent.getType().equals(EventType.EXPIRED)) {
			log.info("Cache event EXPIRED for item with key {}. Old value = {}, New value = {}", cacheEvent.getKey(),
					cacheEvent.getOldValue(), cacheEvent.getNewValue());
		}
	} 

Advertisement
Advertisement