Hi Camel Users, In Spring we have the @Cacheable <https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-caching.html> annotation that provides an easy way to wrap a method call with a cache. The method result is cached and the method is not executed the next time the it's called with the same input parameters.
I was wondering if we have something similar for Camel routes, where we can cache the "result" of a route and execute the next time only if nothing is found in the cache. I haven't found any solution out-of-the-box, but a few blogs <https://medium.com/@joelicious/camel-3scale-component-part-2-355faaaba5e0> about doing similar caching in certain specific scenarios using the Policy for processors. So I tried to put together a more generic solution: See CachePolicy class: https://gist.github.com/bszeti/552bf7a1f05a75fc2964a4ce7ce889b5 This is how it can be used in a route: https://gist.github.com/bszeti/4f855cd80cd69e3953ff990b0ae056a2 public class CachedRoute extends RouteBuilder{ @Override public void configure() throws Exception { from("direct:cachedRoute") *.policy(getUserDataCachePolicy())* .setProperty("userData").method(userDataService.class,"lookupUserDataByName"); } //Cache user data by user name. If user data is found in cache the rest of the route is not executed. *private Policy getUserDataCachePolicy()*{ CachePolicy cachePolicy = new CachePolicy(); cachePolicy.setCache(cacheManager.getCache("userDataCache")); //User name is the cache key cachePolicy.setKeyExpression(simple("${header.userName}")); //What to do with the cached object if found cachePolicy.setApplyCachedObject(new BiConsumer<Exchange, Object>() { public void accept(Exchange exchange, Object cached){ exchange.setProperty("userData",cached); } }); //Cache userData at the end of the route cachePolicy.setValueExpression(exchangeProperty("userData")); return cachePolicy; } It would be great to know if someone tried anything similar and especially if the "AsyncCallback()" looks OK in CachePolicy.wrap(). I found that piece always a bit tricky... Thanks, Balazs