Micronaut的@Cacheable注解TTL和Retention-Policy

huangapple go评论70阅读模式
英文:

Micronaut's @Cacheable annotation TTL and Retention-Policy

问题

  1. 默认的TTL是多少(我已经查看了文档但没有找到)
  2. 文档中的保留策略是什么意思,具有以下枚举值:SOURCE,CLASS,RUNTIME
  3. 缓存的项目存储在哪里?

感谢您的帮助,提前感谢!

英文:

I am using Kotlin-Micronaut's cacheable annotation for a method that calls an API as below:

@Retryable(attempts = "5", delay = "20ms", multiplier = "2.0")
@Cacheable(value = ["my-cache"], parameters = ["id"])
@Get("/fetch/mydata")
fun fetchData() {
   ....required confs for calling the api
}

I need help understanding:

  1. What is the default TTL (I have gone through docs but not able to find one)
  2. What is meant as the retention-policy at docs having following enums: SOURCE, CLASS, RUNTIME
  3. Where are the cached-items stored?

Any help appreciated, thanks in advance!

答案1

得分: 1

如上面的评论中提到的,您需要选择一个缓存库,如Caffeine、Eh-Cache、Hazelcast等(请参阅Micronaut缓存文档中的可用缓存)。

如果您的类路径中只有micronaut-cache-core,则您的应用程序将不会具备缓存功能,因为这些功能由micronaut-cache-caffeine等提供。

默认的TTL是多少(我已经查阅了文档,但找不到一个)

这取决于您将要使用的缓存。Micronaut的缓存配置提供了以下两个持续时间选项:expireAfterWriteexpireAfterAccess(请参阅GitHub)。就我所知,Caffeine的默认行为是除非您配置它,否则不会过期。这意味着您的缓存项将永远存在。

缓存的项目存储在哪里?

对于Caffeine来说,严格来说是存储在内存中的。这意味着如果您选择Caffeine作为缓存,当您重新启动Micronaut应用程序时,您将丢失缓存项。

文档中的保留策略是什么意思,有以下枚举:SOURCE、CLASS、RUNTIME

这个问题有点离题,因为它与缓存无关。但我猜您是在问Java的枚举java.lang.annotation.RetentionPolicy。Java的枚举非常详细记录在文档中。请查看文档。

package java.lang.annotation;

/**
 * 注解保留策略。此枚举类的常量描述了用于保留注解的各种策略。它们与{@link Retention}元注解接口一起使用,以指定注解的保留时间。
 *
 * @author  Joshua Bloch
 * @since 1.5
 */
public enum RetentionPolicy {
    /**
     * 编译器应该丢弃注解。
     */
    SOURCE,

    /**
     * 编译器应该在类文件中记录注解,但不需要在运行时由VM保留。这是默认行为。
     */
    CLASS,

    /**
     * 编译器应该在类文件中记录注解,并在运行时由VM保留,以便可以通过反射读取。
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}
英文:

As mentioned in the comments above, you need to choose a caching library such as Caffeine, Eh-Cache, Hazelcast, … (see available caches in Micronaut Cache documentation)

If you simply have micronaut-cache-core in your classpath, your application is not gonna have an caching functionality, since these are provided e.g. by micronaut-cache-caffeine.

> What is the default TTL (I have gone through docs but not able to find one)

This depends on what cache you are going to use. Micronauts Cache configuration provides the following two durations expireAfterWrite and expireAfterAccess (see GitHub). As fair as I know Caffeine, the default behaviour is that nothing is going to expire unless you configure it. Means your cache items live for ever.

> Where are the cached-items stored?

For Caffeine it is strictly in memory. Meaning if you choose Caffeine as your cache you lose the cache items when you restart your Micronaut application.

> What is meant as the retention-policy at docs having following enums: SOURCE, CLASS, RUNTIME

That question is a bit off topic, since it has nothing to do with caching. But I assume you're asking about Java's enum java.lang.annotation.RetentionPolicy. The Java enum is very well documented. Please take a look at the documentation.

package java.lang.annotation;

/**
 * Annotation retention policy.  The constants of this enumerated class
 * describe the various policies for retaining annotations.  They are used
 * in conjunction with the {@link Retention} meta-annotation interface to
 * specify how long annotations are to be retained.
 *
 * @author  Joshua Bloch
 * @since 1.5
 */
public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

huangapple
  • 本文由 发表于 2023年5月24日 18:24:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定