4 Replies Latest reply on Feb 3, 2012 10:36 AM by usydrajani

    Does only one copy of an object exist throught out the nodes of Infinispan in  Invalidation modes

    usydrajani

      Hi

       

      I am running cache in Invalidation mode. What I am doing is I am putting an object in cache node1. When I am putting the same object in cache node 2 then the first copy I put in cache node1 is invalidating and I am getting  null when I do cahe1.get(1);

       

       

      package org.infinispan.sampleModuleMeena;

       

       

      import org.apache.log4j.PropertyConfigurator;

      import org.infinispan.Cache;

      import org.infinispan.config.Configuration;

      import org.infinispan.config.GlobalConfiguration;

      import org.infinispan.manager.DefaultCacheManager;

      import org.infinispan.manager.EmbeddedCacheManager;

      import org.infinispan.sampleModuleMeena.api.SampleModuleDecorator;

       

       

      public class SampleUsageTestFeb2 {

           public static void main(String[] args)  {

              

               PropertyConfigurator.configure("log4j.properties");

              

              

              

       

                          

                       

                      Configuration cfg = new Configuration();

                      cfg.setCacheMode(Configuration.CacheMode.INVALIDATION_SYNC);

                     

                      EmbeddedCacheManager manager1 = new DefaultCacheManager(GlobalConfiguration.getClusteredDefault(), cfg );

                      EmbeddedCacheManager manager2 = new DefaultCacheManager(GlobalConfiguration.getClusteredDefault() , cfg);

                     

                 

                     

                      Cache<Integer, Integer> cache1 = manager1.getCache("entity");

                      Cache<Integer, Integer> cache2 = manager2.getCache("entity");

                      SampleModuleDecorator<Integer, Integer> moduleApi1 = new SampleModuleDecorator<Integer, Integer>(cache1);

                      SampleModuleDecorator<Integer, Integer> moduleApi2 = new SampleModuleDecorator<Integer, Integer>(cache2);

                     

                     

            

           

            cache1.put(1, 11);

            System.out.println(cache1.containsKey(1) + "         cache1.containsKey(1) This is after we did put some object in cache1");

           

            cache2.put(1, 11);

            System.out.println(cache2.containsKey(1) + "         cache2.containsKey(1) This is after we did put some object in cache2");

           

            System.out.println(cache1.containsKey(1) + "         cache1.containsKey(1) This is after we did put some object in cache2");

           

              

        

           }

      }

       

       

      The am getting following output .  

       

      Cache [pc-4e63-0-25169] replicating InvalidateCommand{keys=[1]}

      true         cache1.containsKey(1) This is after we did put some object in cache1

      Cache [pc-4e63-0-5368] replicating InvalidateCommand{keys=[1]}

      true         cache2.containsKey(1) This is after we did put some object in cache2

      false         cache1.containsKey(1) This is after we did put some object in cache2

       

       

      My question is how can I put same object in two cache nodes? without invalidating the other.  The other question is which infinispan-configs.xml file my this program is using as I am not using JBoss, so this infinispan is running without application server and I have not put any Infinispan-config.xml in the class path.

       

      Thanks

       

      Meena

        • 1. Re: Does only one copy of an object exist throught out the nodes of Infinispan in  Invalidation modes
          galder.zamarreno

          Well, use replication instead of invalidation, see cache modes docu.

          • 2. Re: Does only one copy of an object exist throught out the nodes of Infinispan in  Invalidation modes
            dan.berindei

            Galder, I think Meena's scenario is actually a good fit for invalidation mode. If you don't care about preloading data from the DB, then invalidation makes more sense than replication or distribution. If you also know that your application is the only one writing to the DB, then you want to invalidate the data on the other nodes only when you write an entity to the DB, not when you read it.

             

            Meena, in order to skip invalidation, you have to use the CACHE_MODE_LOCAL flag (which actually skips all comunication with the other nodes):

             

                cache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL).put(k, v)
            

             

            You must be careful though to not use the CACHE_MODE_LOCAL flag when you update an entity in your application, otherwise the other nodes will keep using the stale data.

             

             

            To answer your other question, you don't need an infinispan-config.xml file because GlobalConfiguration.getClusteredDefault() builds a GlobalConfiguration instance programatically. Actually in 5.1 we've introduced a new way of configuring Infinispan programatically, and now you can use something like this to customize your configuration:

             

            GlobalConfiguration gc = GlobalConfigurationBuilder.defaultClusteredBuilder()
                  .transport().nodeName("node1")
                  .build();
            
            1 of 1 people found this helpful
            • 3. Re: Does only one copy of an object exist throught out the nodes of Infinispan in  Invalidation modes
              galder.zamarreno

              Dan+Meena, to write something and not invalidate, call putForExternalRead(), which is better suited for the use case when you're putting something in the cache as a result of a read: https://docs.jboss.org/author/x/RAY5

              • 4. Re: Does only one copy of an object exist throught out the nodes of Infinispan in  Invalidation modes
                usydrajani

                Just to explain a little more. I am research student and want to extend the invalidation cache  to cache with freshness functionality. My question is how to load data from database to cache in invalidation mode, both with Jboss application server and without AS.Can you point me to any example of loading data from database into cache.

                 

                Thanks for the answer it is useful as I want to see how cache behave after I modify something in cache in one node in Invalidation mode(the data is already cached in more than one nodes ) and then  extend it to according to my idea.

                 

                What I have done so far is I have loaded the data in cache node manually by doing a named query and then getting each object from list and then by putting each data item into cache by  cache.put(key, value). Please let me know how do I load data from database in postgres to infinispan by both using cache directly and using cache with jboss and JEB.

                 

                Thanks