简体   繁体   中英

Spring Boot | Create custom caching annotation and implementation

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 eg, Ehcache has timeToLiveSeconds parameter for each cache)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM