I want to create a custom spring @Cachable annotation, where i can defined the expire time in seconds. The problem is, i don't know how i implement the new method "expiresInSec()" in the custom annotation.
Here is my custom @Cachable annotation:
@Retention(RetentionPolicy.RUNTIME)
@Target(
{ElementType.TYPE, ElementType.METHOD})
public @interface Cachable
{
Cacheable cachable();
// cache entry expires in 1 hour by default
int expiresInSec() default 3600;
}
This is the call of custom annotation:
@Cachable(cachable = @Cacheable("WorkListRepository::getWorkList"), expiresInSec = 60)
But the expireInSec parameter doesn't work. Where i must implementing this parameter method.
Thank you
Extending the Spring Cacheable
would require more than just adding extra attributes in the annotation. You need to extends the capabilities of annotation processor for caching in Spring as well. Check here for more details on what it would take.
Spring caching is an abstraction and requires a cache provider and generally the expiry is set on the cache level ( for e.g, Ehcache has timeToLiveSeconds
parameter for each cache)