Annotation Type Cacheable
-
@Documented @Retention(RUNTIME) @Target(METHOD) public @interface Cacheable
Makes a method response cacheable in memory for some time.For example, this
load()method loads some data from the network and we want it to cache loaded data for 5 seconds (to avoid delays):@Cacheable(lifetime = 5, unit = TimeUnit.SECONDS) String load(String resource) throws IOException { return "something"; }You can cache them forever, which means that once calculated and cached value will never expire (may be a nice alternative to static initializers):
@Cacheable(forever = true) String load(String resource) throws IOException { return "something"; }Since version 0.7.14 you can also annotate methods that should flush cache of the object.
Since 0.7.18 you can control when exactly flushing happens, with
Cacheable.FlushBeforeandCacheable.FlushAfterannotations (Cacheable.Flushis deprecated), for example:public class Page { @Cacheable public String get() { // load data from external source, e.g. the network return data; } @Cacheable.FlushBefore public void set(String data) { // save data to the network } }- Since:
- 0.7.7
- See Also:
- http://aspects.jcabi.com/
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description Class<?>[]afterAfter-flushing trigger(s).booleanasyncUpdateReturns the current store after the expiration, and then asynchronously update the data.Class<?>[]beforeBefore-flushing trigger(s).booleanforeverKeep in cache forever.intlifetimeLifetime of an object in cache, in time units.TimeUnitunitTime units of object lifetime.
-
-
-
-
unit
TimeUnit unit
Time units of object lifetime.The minimum unit you can use is a second. We simply can't cache for less than a second, because cache is being cleaned every second.
- Returns:
- The time unit
- Default:
- java.util.concurrent.TimeUnit.MINUTES
-
-
-
before
Class<?>[] before
Before-flushing trigger(s).Before calling the method, call static method
flushBefore()in this class and, according to its result, either flush or not. For example:class Foo { @Cacheable(before = Foo.class) int read() { // return some number } public static boolean flushBefore() { // if TRUE is returned, flushing will happen before // the call to #read() } }- Returns:
- The array of types
- Since:
- 0.21
- Default:
- {}
-
-
-
after
Class<?>[] after
After-flushing trigger(s).After calling the method, call static method
flushAfter()in this class and, according to its result, either flush or not. For example:class Foo { @Cacheable(after = Foo.class) int read() { // return some number } public static boolean flushAfter() { // if TRUE is returned, flushing will happen after // the call to #read() } }- Returns:
- The array of types
- Since:
- 0.21
- Default:
- {}
-
-