Annotation Interface Cached


@Retention(RUNTIME) @Target(METHOD) public @interface Cached
Marks a method's return value for automatic caching, backed by GlobalCacheService. Applying this annotation is equivalent to hand-writing the check-cache / call-method / store-result boilerplate that the legacy code used to write out manually for every cached method.

Requirements for a method to be safely annotated:

  • It must not return void -- see CachedAspect for why.
  • Its return value should be Serializable (or null). Non-serializable results are simply not cached (a warning is logged), so this is a soft requirement, not a hard failure.
  • Its arguments should have correct, value-based equals()/ hashCode() implementations. See CachedAspect for how the cache key is built and what that implies.
  • It should be idempotent / side-effect-free for the given arguments -- a cached call means the method body may simply not run again until the entry expires.

 @Cached(ttl = 60_000) // 1 minute
 public Issue findIssue(String issueId) {
     return IssuePeer.doSelectSingle(...);
 }
 
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    long
    How long the result stays valid, in milliseconds.