5 Replies Latest reply on Feb 9, 2017 10:15 AM by Radim Vansa

    Cache loading barrier

    Demis Gallisto Newbie


      Do you know if Infinispan (with the hotrod remote client) supports with the JCache API something like a cache barrier when I try to put in the cache a value from two threads but with the same key?

      Like the EHCache barrier of EHCache, or the atomically cache loading of Guava.




        • 1. Re: Cache loading barrier
          Tristan Tarrant Master

          Infinispan implements the semantics of the JDK ConcurrentHashMap. It seems to me like what you want is putIfAbsent which is present in both RemoteCache and in JCache.

          • 2. Re: Cache loading barrier
            Demis Gallisto Newbie

            Hi Tristan,

            what I need is a little different.

            I want to invoke the get(String key) method on the javax.cache.Cache interface (configured with setReadThrough(true) and use a custom CacheLoader<String, String>) from two (or more) different concurrent threads for a key not already cached.

            When the first thread invokes the load(String key) method on my CacheLoader (configured with setCacheLoaderFactory(new FactoryBuilder.SingletonFactory<CacheLoader<String, String>>(new JCacheCacheLoader())) I want the others threads blocked on the javax.cache.get() method, waiting for the result retrieved from the first CacheLoader.

            This because the operation to retrieve the cache value on the CacheLoader is very expansive, and the result will be the same for the same key.

            Is it possibile not to execute the others CacheLoader if the first returns correctly?

            • 3. Re: Cache loading barrier
              Radim Vansa Master

              No, such feature is not natively available, it's tracked as [ISPN-7224] Support synchronous get in Spring's cache abstraction Currently the closest thing you can do is pessimistic transactional cache and issue the read with cache.withFlags(Flag.FORCE_WRITE_LOCK).get(key).

              • 4. Re: Cache loading barrier
                Demis Gallisto Newbie

                Hi Radim!

                Thank you for your response,

                The only way to enable the pessimistic transactional cache is to reconfigure the cache on the remote server?

                Is it possible to set the FORCE_WITH_LOCK flag with the JCache API?

                • 5. Re: Cache loading barrier
                  Radim Vansa Master

                  Regrettably not, not with JCache, and not even if you're using Hot Rod or REST (as you speak about remote server).


                  Actually I think that implementing that would be a doable for a new contributor; the changes could be isolated to single interceptor/extending the current CacheLoaderInterceptor.