Using Multiple Dynamic Caches With Spring

In a third post about cache managers in spring (over a long period of time), I’d like to expand on the previous two by showing how to configure multiple cache managers that dynamically create caches. Spring has CompositeCacheManager which, in theory, should allow using more than one cache manager. It works by asking the underlying cache managers whether they have a cache with the requested name or not. The problem with that is when you need dynamically-created caches, based on some global configuration. And that’s the common scenario, when you don’t want to manually define caches, but instead want to just add @Cacheable and have spring (and the underlying cache manager) create the cache for you with some reasonable defaults. That’s great until you need to have more than one cache managers. For example – one for local cache and one for a distributed cache. In many cases a distributed cache is needed; however not all method calls need to be distributed – some can be local to the instance that handles it and you don’t want to burden your distributed cache with stuff that can be kept locally. Whether you can configure a distributed cache provider to designate some cache to be local, even though it’s handled by the distributed cache provider – maybe, but I don’t guarantee it will be trivial. So, faced with that issue, I had to devise some simple mechanism of designating some caches as “distributed” and some as “local”. Using CompositeCacheManager alone would not do it, so I extended the distributed cache manager (in this case, Hazelcast, but it can be done with...

Multiple Cache Configurations with Caffeine and Spring Boot

Caching is key for performance of nearly every application. Distributed caching is sometimes needed, but not always. In many cases a local cache would work just fine and there’s no need for the overhead and complexity of the distributed cache. So, in many applications, including plain Spring and Spring Boot, you can use @Cacheable on any method and its result will be cached so that the next time the method is invoked, the cached result is returned. Spring has some default cache manager implementations, but external libraries are always better and more flexible than simple implementations. Caffeine, for example is a high-performance Java cache library. And Spring Boot comes with a CaffeineCacheManager. So, ideally, that’s all you need – you just create a cache manager bean and you have caching for your @Cacheable annotated-methods. However, the provided cache manager allows you to configure just one cache specification. Cache specifications include the expiry time, initial capacity, max size, etc. So all of your caches under this cache manager will be created with a single cache spec. The cache manager supports a list of predefined caches as well as dynamically created caches, but on both cases a single cache spec is used. And that’s rarely useful for production. Built-in cache managers are something you have to be careful with, as a general rule. There are a few blogposts that tell you how to define custom caches with custom specs. However, these options do not support the dynamic, default cache spec usecase that the built-in manager supports. Ideally, you should be able to use any name in @Cacheable and automatically a cache...

Use Client Certificate Authentication with Java and RestTemplate

As a follow up of the http://gochev.blogspot.com/2019/04/convert-pfx-certificate-to-jks-p12-crt.html we now have a keystore and a truststore (if anyone needs) and we will use this keystore to send client side authentication using Spring’s RestTemplate .First copy your keystore.jks and truststore.jks in your classpath, no one wants absolute paths right ?:)The magic happens in the creation of SSLContext. Keep in mind the Spring Boot have a nice RestTemplateBuilder but I will not gonna use it, because someone of you might have an older version or like me, might just use a plain old amazing Spring.If you just want to use the keystore:final String allPassword = “123456”;SSLContext sslContext = SSLContextBuilder                .create()                .loadKeyMaterial(ResourceUtils.getFile(“classpath:keystore.jks”),                                    allPassword.toCharArray(), allPassword.toCharArray())                .build(); if you just want to use the truststore final String allPassword = “123456”;SSLContext sslContext = SSLContextBuilder                .create()                .loadTrustMaterial(ResourceUtils.getFile(“classpath:truststore.jks”), allPassword.toCharArray())                .build(); I guess you know how to use both ;), if you want to IGNORE the truststore certificate checking and trust ALL certificates (might be handy for testing purposes and localhost) final String allPassword = “123456”;TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;SSLContext sslContext = SSLContextBuilder                .create()                .loadTrustMaterial(ResourceUtils.getFile(“classpath:truststore.jks”), allPassword.toCharArray())                .loadTrustMaterial(null, acceptingTrustStrategy) //accept all                .build();...

Introduction to Spring MVC (In Bulgarian)

This is video from the presentation I did for Software University  at Sofia, Bulgaria. Camera : Screen capture : All demos can be downloaded from here https://github.com/gochev/examples/tree/master/SoftUni%20Spring%20MVC%20June%202015 If you missed the event .. don’t miss the next one. In Bulgarian Java User Group we do events like this one every month. Information about past and future events can be found here...

Introduction to Spring MVC (In Bulgarian)

This is video from the presentation I did for Software University  at Sofia, Bulgaria. Camera : Screen capture : All demos can be downloaded from here https://github.com/gochev/examples/tree/master/SoftUni%20Spring%20MVC%20June%202015 If you missed the event .. don’t miss the next one. In Bulgarian Java User Group we do events like this one every month. Information about past and future events can be found here...

Introduction to Spring MVC (In Bulgarian)

This is video from the presentation I did for Software University  at Sofia, Bulgaria. Camera : Screen capture : All demos can be downloaded from here https://github.com/gochev/examples/tree/master/SoftUni%20Spring%20MVC%20June%202015 If you missed the event .. don’t miss the next one. In Bulgarian Java User Group we do events like this one every month. Information about past and future events can be found here...