blog podcast

Embedded Infinispan in Quarkus

Embedding the infinispan cache into your service is a cool idea. Instead of having to rely on a managed external service you can create your own in memory grid by using the infinispan library. Now the embedded cache used to be supported by Quarkus, but at some point they dropped the support. The artifact used to be called quarkus-infinispan-embedded but it got the boot somewhere around mid 2020.

Luckily that was just quarkus booting the feature, infinispan did not boot it. By using the infinispan library it’s still possible to also make your quarkus application use the power of embedded caches.

There are some challenges though. When you use the RemoteCacheManager you have Quarkus nicely instantiate this for you. Quarkus handles registering your correct marshallers for your protostream classes that have been generated. The EmbeddedCacheManager is something you have to create yourself and hence you don’t get this luxury. Here’s a dirty trick you can use though. Simply inject the RemoteCacheManager into your constructor that builds the EmbeddedCacheManager and steal the marshaller. Quick and dirty, problem solved.

Code of the Day

    @Produces
    @ApplicationScoped
    fun createCacheManager(remoteCacheManager: RemoteCacheManager): EmbeddedCacheManager {
      val global = GlobalConfigurationBuilder
          .defaultClusteredBuilder()

      val remoteCacheManagerMarshaller = remoteCacheManager.marshaller
      val marshaller = global.serialization().marshaller(remoteCacheManagerMarshaller)

      cacheManager = DefaultCacheManager(global.build())
      return cacheManager
    }