Details
-
New Feature
-
Status: Closed
-
Major
-
Resolution: Fixed
-
5.0.10
-
None
Description
The @Once annotation can be used on methods to cache the value or cache the value based on some other value changing (great for loops). We've been using it in our internal CMS library and it's immensly useful, especially when getting things out of any kind of database or doing queries. Here's a typical usage:
class:
@Once
public List<Entities> getEntities() {
// get and return the objects from the database or something
}
in the template:
<t:if test="!entities.empty">
do something
</t:if>
<t:loop source="entities">
loop through and do something
</t:loop>
In this example, getEntities gets cached on the if test and then the value is cached so it doesn't get called again on the loop. You can also use it this way:
@Property
private Entity loopValue;
@Once(watch="loopValue")
public Object getSomeDependentValue() {
// do some expensive operation based on loopValue
}
which will only execute the method each time loopValue changes.