Annotation Type Cacheable


@Retention(CLASS) @Target(METHOD) public @interface Cacheable

Answers a call from the cache when the cache holds an answer for its key, and otherwise runs the method and keeps what it returned -- Spring's @Cacheable.

@Cacheable(value = "products", key = "#id")
public Product product(long id) { ... }

The build rewrites the method: there is no proxy, so a call from inside the same class is cached too, which Spring's proxy would skip. The key expression is compiled when the application is built, and one the build cannot compile fails the build. It may name a parameter (#id, or #p0/#a0 by position), a property of one (#user.id, read through its getter or public field), a quoted literal ('all'), #root.methodName, and join them with +. Empty means every parameter, as Spring's SimpleKeyGenerator keys.

The value is kept in the server's key/value store -- Redis once cn1.kv.url names one, so every process shares it -- written as JSON by a codec the build generates for the return type. A null result is not kept, as Spring keeps none with unless = "#result == null". How long an entry lives is cn1.cache.<name>.ttl in seconds, then cn1.cache.ttl, then spring.cache.redis.time-to-live; unset, it lives until evicted.

  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    The same as value(), as Spring spells it.
    The key expression; see the class description.
    Accepted when it is #result == null, which is what happens anyway; any other expression fails the build.
    The cache's name; the first is used when several are given.
  • Element Details

    • value

      String[] value
      The cache's name; the first is used when several are given.
      Default:
      {}
    • cacheNames

      String[] cacheNames
      The same as value(), as Spring spells it.
      Default:
      {}
    • key

      String key
      The key expression; see the class description.
      Default:
      ""
    • unless

      String unless
      Accepted when it is #result == null, which is what happens anyway; any other expression fails the build.
      Default:
      ""