1 2 Previous Next 18 Replies Latest reply on Aug 31, 2010 10:41 AM by navssurtani

    How to do jboss caching

    amolumrani

      Hello,

       

      I am totally new on JBoss caching.

       

      I want to cache the objects.

       

      Please help me how jboss caching is implemented and provide me some tutorial from where i will come to know this..

       

       

      Thanks,

      Amol

        • 1. Re: How to do jboss caching
          navssurtani

          Hiya,

           

          If you're new to caching stuff then I'd recommend that you don't use JBoss Cache and that you use Infinispan instead. It's a lot better documented and in a way the 'new' JBoss Cache.

           

          Hope this helps,

          Navin.

          • 2. Re: How to do jboss caching
            amolumrani

            Hi,

             

            Thanks for your valuable answer.

             

            But still i want to use JBoss Cache..

             

            So please provide me help for this..

             

             

            Thanks,

            Amol

            • 3. Re: How to do jboss caching
              navssurtani

              Apologies for the late reply Amol.

               

              I would greatly discourage you from still wanting to use JBoss Cache. Can you explain your use case please?

               

              Infinispan has a TreeCache implementation which basically provides the same API as JBoss Cache but works a lot, lot faster under the hood. You can search for our wiki and find the numbers to see for yourself.

               

              I appreciate that you think that JBoss Cache is a good option - while it is - but Infinispan is something that is tonnes better and pretty much all of the devs here would prefer you use that as opposed to JBoss Cache.

               

              Thanks,

              Navin.

              • 4. Re: How to do jboss caching
                amolumrani

                I want to use JBoss caching in Java..

                 

                I want some tutorial which explains me in detail how to do JBoss caching with configuration etc..

                 

                I have come to know that by using cache.put and  cache.get we put object into cache and retrieve it from cache..

                 

                 

                 

                So please help me regarding this..

                • 5. Re: How to do jboss caching
                  navssurtani

                  Amol - you can find the official JBoss Cache documentation here.

                   

                  I still don't understand why you want to use JBoss Cache over Infinispan. Can you explain why please?

                  • 6. Re: How to do jboss caching
                    amolumrani

                    I have http://docs.jboss.org/jbosscache/3.2.1.GA/userguide_en/html/index.html.

                     

                    In this document only API is there.

                     

                    I want some tutorial by which i can cache an object using this API..

                     

                     

                    Thanks,

                    Amol

                    • 7. Re: How to do jboss caching
                      navssurtani

                      Infinispan's documentation has this. JBoss Cache however doesn't. There is a gui demo but no tutorial. Infinispan is a lot better documented and better in every single way. So I don't understand why you're insisting on using JBoss Cache still?

                      • 8. Re: How to do jboss caching
                        amolumrani

                        Hello Navin,

                         

                        1) I am going through http://community.jboss.org/wiki/5minutetutorialonInfinispan.

                         

                        I have download infinispan-4.1.0.CR3-all.zip from their site.

                         

                        I want to see how object caching is done by infinispan.

                         

                        bcoz of am new to caching can u tell me how to deploy this tutorial?

                         

                        I have tried GUI Demo of Infinispan but not getting enough idea of object caching.

                         

                         

                         

                        2) I have seen  infinispan interactive tutoril on site.

                         

                        They provide sample-configurations.xml file for cache definitions.

                         

                        My question is how to deploy this tutorial in java.

                         

                        Please help me regarding this...

                         

                        Thanks,

                        Amol

                        • 9. Re: How to do jboss caching
                          navssurtani

                          Hey Amol,


                          1) I am going through http://community.jboss.org/wiki/5minutetutorialonInfinispan.

                           

                          I have download infinispan-4.1.0.CR3-all.zip from their site.

                           

                          I want to see how object caching is done by infinispan.

                           

                          bcoz of am new to caching can u tell me how to deploy this tutorial?

                           

                          I have tried GUI Demo of Infinispan but not getting enough idea of object caching.

                           


                           

                           


                          What do you mean by Object caching? Do you mean actually how you put stuff into the cache and making use of it?

                           

                          If so you can just do something like: -

                           

                           

                          CacheManager cm = new DefaultCacheManager();
                          Cache<MySpecialKey, MySpecialValue> c = cm.getCache();
                          
                          
                          // Put some stuff into the cache
                          c.put(k1, v1);
                          
                          //etc etc
                          

                           

                          Now I want to get the stuff out of the cache using a different app on a different JVM even. {Note that this will only work if the Caches are in DIST or REPL configuration and have the SAME configuration. Also note that the caches have the same String name in both cases}

                           

                           

                          CacheManager cm2 = new DefaultCacheManager();
                          Cache<MySpecialKey, MySpecialValue> c = cm2.getCache();
                          
                          // Get my stuff out of the cache: - 
                          
                          MySpecialValue specialValue = c.get(k1);
                          
                          // etc etc
                          

                           

                           

                          Obviously if that's not what you want and have a more complex problem then please let me know and I'll see if I can help.

                           

                          With respect to the configuration file, as long as it's somewhere in your project directory it should be fine. Generally a good idea to keep it with your maven/ant build file or a config directory if you have one. Make sure you use the appropriate API on CacheManager though.

                           

                          Hope that helps,

                          Navs.

                          • 10. Re: How to do jboss caching
                            amolumrani

                            Hello Navin,

                             

                            Thanks for help.

                             

                            I am doing as follows..

                             

                            public class SimpleCaching {

                             

                                public static void main(String args[]) {
                                    try {
                                        GlobalConfiguration myGlobalConfig = new GlobalConfiguration();

                             

                                        // configure myGlobalConfig accordingly
                                        EmbeddedCacheManager manager = new DefaultCacheManager(myGlobalConfig);

                             

                                        Cache cache = manager.getCache();

                             

                                        Configuration config = new Configuration();

                             

                                        // configure your config bean accordingly
                                        manager.defineConfiguration("myCustomCache", config);
                                        Cache customCache = manager.getCache("myCustomCache");

                             

                                        // put some data in cache
                                        cache.put("key", "value");

                             

                                        cache.containsKey("key");

                             

                                        cache.remove("key");

                             

                                        cache.isEmpty();

                             

                                        cache.clear();

                             

                                    } catch (CacheException ex1) {
                                        System.out.println(ex1.getMessage());
                                    }
                                }
                            }

                             

                            This is as per 5 minutes tutorial of infinispan.

                             

                            My question is it right way to write the code for caching ...

                             

                            We are using JPA and i come to know we required to configure infinispan for second level cache in persistence.xml..

                             

                             

                            I have made changes in persistence.xml for infinispan for default settings as per http://community.jboss.org/wiki/usinginfinispanasjpahibernatesecondlevelcacheprovider#Configuration page.

                             

                            Object caching means I want to see how data is pushed to cache.

                             

                            Please help me regarding this..

                             

                             

                            Thnks,

                            Amol

                            • 11. Re: How to do jboss caching
                              navssurtani

                              Yes this seems okay - I don't seem to be finding any errors with what you've written.

                               

                              You had however, mentioned about putting in your configuration by xml. If you read the docs and figure out how you'd like to configure your cache by means of xml.

                               

                              Then just have this xml file somewhere in the directory of your application and then provide this as a String to your constructor of the DCM. It depends if you prefer to configure your cache programatically or by xml.

                               

                              HTH,

                              Navs.

                              • 12. Re: How to do jboss caching
                                amolumrani

                                I am using Global Configuration..

                                 

                                GlobalConfiguration myGlobalConfig = new GlobalConfiguration();

                                 

                                            // configure myGlobalConfig accordingly
                                            EmbeddedCacheManager manager = new DefaultCacheManager(myGlobalConfig);

                                 

                                That means default configuration right?

                                 

                                So i think not requred to pass configuration.xml to DCM constructor is it right?

                                 

                                bcoz i  have configured Infinispan in persistence.xml for second level cache..

                                 

                                i have cache.put("key1","value1");

                                 

                                My question is that after build this class what exactly will happened and how i came to know object cache is value1?

                                 

                                Thanks,

                                Amol

                                • 13. Re: How to do jboss caching
                                  navssurtani

                                  If you have configured Infinispan via xml already then you shouldn't need to do anything programatically. Just do something like: -

                                   

                                   

                                  EmbeddedCacheManager cacheManager = new DefaultCacheManager(persistence.xml);
                                  
                                  Cache cache = cacheManager.getCache();
                                  
                                  
                                  

                                   

                                  And that should do it for you.

                                   

                                  Well what you can do is get all the keys in the cache as a set - there's an API method on that. From there you can get hold of the values in the cache that you need. Simply put: - cache --> getKeys() --> {I can now get all the values that I'd require}.

                                  • 14. Re: How to do jboss caching
                                    amolumrani

                                    Hello Navin,

                                     

                                    Thanks for reply.

                                     

                                    I have set following in persistence.xml

                                     

                                    <property name="hibernate.cache.use_second_level_cache" value="true"/>
                                    <property name="hibernate.cache.use_query_cache" value="true"/>

                                    <property name="hibernate.cache.region.factory_class" 
                                       value="org.hibernate.cache.infinispan.JndiInfinispanRegionFactory"/>
                                    <property name="hibernate.cache.infinispan.cachemanager"
                                       value="java:CacheManager"/>

                                    <property name="hibernate.cache.region.factory_class" 
                                       value="org.hibernate.cache.infinispan.InfinispanRegionFactory"/>

                                    I want to bind infinispan cachemanager to JNDI thats why i amde this changes in persistence.xml.

                                    My question is how to bind Infinispan's cachemanger to JNDI?

                                    by going through infinispan user guide i come to know that there are two types of configuration..

                                    1.Global
                                    2.Default

                                    In these where should i have not needed any xml file

                                    I want to implement caching globally where i should not need any xml file cache will be default.

                                     

                                    I think for default configuration u should not required configuration file.

                                     

                                    Is it correct?

                                     

                                    thats why i have used GlobalConfiguration.

                                     

                                    Please help me regarding this...

                                     

                                     

                                    Thanks,

                                    Amol

                                    1 2 Previous Next