I want to annotate my method with @Cacheable
and also with @Timed
(from micrometer).
But I would like the @Timed
to be applied only in case when the data is not timed.
Is there a way to do that, is it enough to put the annotations in correct order - and which order would that be?
My @Timed
is also using TimedAspect
, not sure if that is relevant.
Right now I do it like below:
@Cacheable
@Timed
public String getValue(long id) {
...
}
I couldn't find any documentation for @Cacheable
that would discuss this.
This is tricky, since you are dealing with AOP proxies created by Spring around your business bean, so I am not sure whether you can rely on the order of the annotations.
I think that you have several options:
@Cacheable
and the other with @Timed
. However, I think that this will not work with you since you need more than one bean (calls whitin the same bean are not intercepted by the AOP proxies). Therefore, you would need two beans, one acting as facade and the other doing the actual @Timed
expensive call that you want to cache and monitor.@Timed
and @Cacheable
and implement Orderable
in both interfaces, then use them in your bean. I am not totally sure that this will work, as it will depend on how the annotations are picked up. Something along this line: Specifying the order of proxy creation in Spring
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With