Policy to wrap route with a cache

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Policy to wrap route with a cache

Balazs Szeti
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
Reply | Threaded
Open this post in threaded view
|

Re: Policy to wrap route with a cache

Ralf Claussnitzer
Hi Balazs,

good work. I was thinking of doing such thing myself. It think the
Policy interface is the way to go, since it wraps all the processors.

How about Endpoints (consumers/producers)? Would processor policies work
for them as well?

The code looks pretty complicated though. Could you elaborate a bit on
the AsyncCallback part?

Regards,
Ralf


On 06/28/2018 02:03 AM, Balazs Szeti wrote:

> 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
>