9 Replies Latest reply on Oct 18, 2010 12:45 PM by galder.zamarreno

    Best Practice to clear the caches

    matth

      Hey,

       

      I wanted to know if there was a BP to clear the caches.

       

      My data can be altered via multiple sources, so I often need to clear the caches (2nd level + query cache).

       

      Thanks!

        • 1. Re: Best Practice to clear the caches
          sannegrinovero

          you mean when using Infinispan as a second level cache for Hibernate?

          • 2. Re: Best Practice to clear the caches
            matth

            yes, because I tried to clear the caches via the method of the sessionFactory instance but the cache behavior became very strange :/

            • 3. Re: Best Practice to clear the caches
              sannegrinovero

              can you define "strange" ? It's hard to help with all this information.

              • 4. Re: Best Practice to clear the caches
                matth

                Actually when I clear my caches (2nd level + query cache), the first time a new query is executed, the request works as expected : the fetch entities are retrieved, and the query is put into cache, but the second time a query is executed it's strange : the query no longer appear (it has been put into cache) however the fetch entites are not retrieved, and as the matter of fact i have a lot of loads then... and it takes way too much timee :/

                • 5. Re: Best Practice to clear the caches
                  sannegrinovero

                  are you having this problem just when using Infinispan or also when using other cache providers? Could you try the HashtableCacheProvider?

                  I suspect that you didn't flag the entities as cacheable, it's possible to cache the queries only or the entities only, or both.

                  • 6. Re: Best Practice to clear the caches
                    matth

                    Thanks for your help Sann.

                     

                    I've just tried with HashtableCacheProvider but i didn't manage to clear the caches. With Infinispan, I used the methods :

                     

                     

                    // collections
                    Map<String, CollectionMetadata> roleMap = factory.getAllCollectionMetadata();
                            for (String roleName : roleMap.keySet()) {
                                factory.getCache().evictCollectionRegion(roleName);
                            }
                    
                    // entities
                    Map<String, ClassMetadata> entityMap = factory.getAllClassMetadata();
                            for (String entityName : entityMap.keySet()) {
                                factory.getCache().evictEntityRegion(entityName);
                            }
                    
                    // query
                    factory.getCache().evictDefaultQueryRegion();
                    factory.getCache().evictQueryRegion(UpdateTimestampsCache.REGION_NAME);
                    

                     

                     

                    I don't know why they don't work with other cache providers...

                    • 7. Re: Best Practice to clear the caches
                      sannegrinovero

                      I've just tried with HashtableCacheProvider but i didn't manage to clear the caches.

                      Sorry I couldn't understand. You mean it doesn't work as expected with HashtableCacheProvider? In that case I doubt it's a problem with Infinispan.

                      • 8. Re: Best Practice to clear the caches
                        matth

                        Hey,

                         

                        Actually with HashtableCacheProvider, it's worse because I can't clear the caches with the standard methods I wrote above.

                         

                        I have the feeling that clearing the caches is not a process often used as there is a lack of documentation about it.

                         

                        Thanks for your help

                        • 9. Re: Best Practice to clear the caches
                          galder.zamarreno

                          Matthieu, I don't think this is the right place to ask this question. Hibernate gives you no chance to access the cache directly, so you can't get hold of the Infinispan cache instances from Hibernate. Instead, you should ask and search the Hibernate forum.

                           

                          In the mean time, I see that Hibernate trunk has evictEntityRegions(), evictCollectionRegions() and evictQueryRegions() methods in Cache. Not sure if they're in any released version, but you should try to see if those work.