2 Replies Latest reply on May 27, 2013 5:10 AM by Anna Manukyan

    Prevent entity instance to be cached

    R S Newbie

      Hi,

       

      using Infinispan 5.1.8 as second level cache (2lC) for Hibernate 4.2.0 and JBoss AS 7 how is it possible to cache not all instances of an entity MyEntity, but only some of them based on attribute value. For example, to cache only if myEntity.someValue == 1, but not to cache if myEntity.someValue == 2.

      I understand that this is possible to achieve using custom cache (manually managed cache), but it is possible with Infinispan as second level cache?

       

      Thanks

        • 1. Re: Prevent entity instance to be cached
          Anna Manukyan Newbie

          Hi,

           

          as far as I know, it is not possible to achieve what you want. The only thing that you can achieve with 2LC, is caching some entity classes that you want. E.g. if you have Car and Person entity classes in the same application, you can configure them so that only instances of Car are cached, or only instances of Person. But it is not possible to cache only some of the instances.

           

          Best regards,

          Anna.

          • 2. Re: Prevent entity instance to be cached
            Anna Manukyan Newbie

            Hi,

             

            I know that it is a month since this post, but I've just found a way of doing what you want:

             

            Infinispan supports custom interceptors which are exactly for what you want to do. You can find the information here:  https://docs.jboss.org/author/display/ISPN/Infinispan+Custom+Interceptors

             

            First lets assume that you want to do this without using the cache for the 2nd level cache. In this case you should do the following:

             

            1) Implement custom interceptor which will apply the logic that you want, e.g:

             

            public class CustomInterceptor extends CommandInterceptor {
            
               @Override
               public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) throws Throwable {
                  Object result = null;
            
                  
                  A a = (A) command.getValue();
            
                  if(a.a == 1) {
                     result = super.visitPutKeyValueCommand(ctx, command);
                  }
            
                  return result;
               }
            }
            
            public class A {
               int a;
            
               public A(int a) {
                  this.a = a;
               }
            }
            

             

            This way you declare custom interceptor which puts data into the cache only if the value of it's property a equals to 1, otherwise the data is not put into the cache.

             

            You can use the interceptor for both programmatic as well as for declarative cache configuration.

             

            For the declarative cache configuration it would be something like:

             

            <infinispan>
            
               <namedCache name="customInterceptorCache">
                  <customInterceptors>
                     <interceptor position="FIRST" class="example.CustomInterceptor" />
                  </customInterceptors>
               </namedCache>
            
            </infinispan>
            

             

            For the programmatic cache configuration, this will look like:

             

            Configuration conf = new ConfigurationBuilder().customInterceptors().addInterceptor()
                        .position(InterceptorConfiguration.Position.FIRST)
                        .interceptor(new CustomInterceptor()).build();
            
            DefaultCacheManager manager = new DefaultCacheManager();
            manager.defineConfiguration(CACHE_NAME, conf);
            

             

            Then if you'll insert data into the cache with different values of a, you'll see that only the ones which value is set to 1 will appear in the cache.

             

            Now, comming back to the usage of the cache as a 2LC for the Hibernate:

             

            1) If you are using 2lc cache declarations from the JBoss AS 7 standalone.xml infinispan subsystem, then probably it would not be possible to add custom interceptors to the cache. I've looked into the XML schema used for that and seems that it doesn't support addition of custom interceptors.

             

            2) If you will use your own infinispan-config.xml, then you can use custom interceptors by using the configuration for declarative ones.

             

            I hope this will help.

             

            Best regards,

            Anna.