3 Replies Latest reply on Jan 3, 2011 9:58 AM by vblagojevic

    L1 Cache & Cache EventListeners

    canokar

      Hi

       

      i ask myself if entries which are removed from L1 cache also notify the registered listeners?

      I got a distributed Cache with one entry loaded from Persistent FileCacheStore. if i connect with

      a second Cache in ClusteredMode (CacheMode Dist_Sync) the Lifespan of the L1 Cache ist set to

      500ms. I didn't get @CacheEntryCreated nor @CacheEntryRemoved Events. Maybe i have a wrong

      understanding of the EventListeners, it seems that they did'nt work for L1 Cache.

        • 1. Re: L1 Cache & Cache EventListeners
          vblagojevic

          Hi Cano,

           

          I believe that even though an entry is in L1 you should get notified about its eviction. However, you would have to listen for either CacheEntryEvicted and CacheEntryRemoved events. How are you attaching these listeners? Post your code snippet.

           

          Regards,

          Vladimir

          • 2. Re: L1 Cache & Cache EventListeners
            canokar

            Hi Vladimir,

             

            here is my test code. On the opposite i create a Cache and load the content of the cache via FileStoreConfig.

            Currently there are only one element inside the cache (Key=key and Value=value) for testing purpose.

            The JUnit Test successfully connects to the "Server" Cache and gets the correct value for Key=key.

             

            import org.infinispan.Cache;
            import org.infinispan.config.CacheLoaderManagerConfig;
            import org.infinispan.config.Configuration;
            import org.infinispan.config.GlobalConfiguration;
            import org.infinispan.config.Configuration.CacheMode;
            import org.infinispan.eviction.EvictionStrategy;
            import org.infinispan.loaders.CacheStoreConfig;
            import org.infinispan.loaders.file.FileCacheStoreConfig;
            import org.infinispan.manager.DefaultCacheManager;
            import org.infinispan.manager.EmbeddedCacheManager;
            import org.infinispan.notifications.Listener;
            import org.infinispan.notifications.cachelistener.annotation.CacheEntryCreated;
            import org.infinispan.notifications.cachelistener.annotation.CacheEntryRemoved;
            import org.infinispan.notifications.cachelistener.event.CacheEntryCreatedEvent;
            import org.infinispan.notifications.cachelistener.event.CacheEntryRemovedEvent;
            import org.junit.Before;
            import org.junit.Test;

             

            public class CacheTest {

             

              @Before
              public void setUp() throws Exception {
              }

             

              @Test
              public void accessCache() {
                EmbeddedCacheManager manager = getCacheManager();
                Cache<String, String> cache = manager.getCache("CacheStore");
                cache.addListener(new DummyListener());

             

                System.out.println("Cache value for \"key\" = " + cache.get("key"));
                try {
                  Thread.sleep(600);
                } catch (InterruptedException e) {
                  e.printStackTrace();
                }
                System.out.println("Cache value for \"key\" = " + cache.get("key"));
                try {
                  Thread.sleep(1200);
                } catch (InterruptedException e) {
                  e.printStackTrace();
                }
                System.out.println("Cache value for \"key\" = " + cache.get("key"));

             

                cache.stop();
                manager.stop();
              }

             

              private EmbeddedCacheManager getCacheManager() {
                GlobalConfiguration globalConf = GlobalConfiguration.getClusteredDefault();
                Configuration config = new Configuration();
                config.setEvictionStrategy(EvictionStrategy.NONE);
                config.setCacheLoaderManagerConfig(new CacheLoaderManagerConfig());
                config.setCacheMode(CacheMode.DIST_SYNC);
                config.setL1Lifespan(500);
                return new DefaultCacheManager(globalConf, config);
              }
             
              @Listener
              public class DummyListener {
                @CacheEntryCreated
                public void print(CacheEntryCreatedEvent<String, String> event) {
                  System.out.println("New entry " + event.getKey() + " created in the cache");
                }
               
                @CacheEntryRemoved
                public void print(CacheEntryRemovedEvent<String, String> event) {
                  System.out.println("Entry " + event.getKey() + " removed in the cache");
                }
              }
            }

            • 3. Re: L1 Cache & Cache EventListeners
              vblagojevic

              The key/value pair was either transfered during state transfer or there was a remote get. That is why you are not seeing listeners invoked. Do a put or remove in this test code and observe if the listener is getting invoked.

               

              Regards,

              Vladimir