Tapestry
  1. Tapestry
  2. TAPESTRY-2244

Add @Cached annotation for caching method values

    Details

    • Type: New Feature New Feature
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 5.0.10
    • Fix Version/s: 5.0.11
    • Component/s: tapestry-core
    • Labels:
      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.

        Activity

        Hide
        Dan Adams added a comment -

        Correct, there is no rewind in T5. I was pretty certain this wouldn't be a problem but I added an integration test to prove it. Thanks for the heads-up on the possible problem.

        Show
        Dan Adams added a comment - Correct, there is no rewind in T5. I was pretty certain this wouldn't be a problem but I added an integration test to prove it. Thanks for the heads-up on the possible problem.
        Hide
        Andreas Andreou added a comment -

        So, does this mean that it suffers from the same issue/feature, or not?

        I've read that T5 doesn't rewind, so i dont know if getEntities() will ever get called
        BEFORE the form ends processing, but if it does, then the issue/feature i describe will exist.

        For reference, here's the T4 situation:
        Assuming a form that displays a list of entities and also allows me to add a new one
        and i do submit a new one, here's what will happen:

        • the new entity will be added to the db.
        • the rendering of the page will NOT show me the new entity
          because getEntities() will (probably - depends on how components are laid out)
          have been cached before
        Show
        Andreas Andreou added a comment - So, does this mean that it suffers from the same issue/feature, or not? I've read that T5 doesn't rewind, so i dont know if getEntities() will ever get called BEFORE the form ends processing, but if it does, then the issue/feature i describe will exist. For reference, here's the T4 situation: Assuming a form that displays a list of entities and also allows me to add a new one and i do submit a new one, here's what will happen: the new entity will be added to the db. the rendering of the page will NOT show me the new entity because getEntities() will (probably - depends on how components are laid out) have been cached before
        Hide
        Dan Adams added a comment -

        The cached value is erased when the page is detached.

        Show
        Dan Adams added a comment - The cached value is erased when the page is detached.
        Hide
        Andreas Andreou added a comment -

        I did Cached for T4.1 @ tacos... The only undocumented feature it has is that
        upon rewinding, if a value is cached, it'll also be cached during the forthcoming rendering
        (assuming no redirect-after-post)

        I don't know if this also happens in T5, but if it does, it's worth an explanation. For tacos, i'm
        considering adding a flag like @Cached(clearOnRender=true) if you have a better name, please share

        Show
        Andreas Andreou added a comment - I did Cached for T4.1 @ tacos... The only undocumented feature it has is that upon rewinding, if a value is cached, it'll also be cached during the forthcoming rendering (assuming no redirect-after-post) I don't know if this also happens in T5, but if it does, it's worth an explanation. For tacos, i'm considering adding a flag like @Cached(clearOnRender=true) if you have a better name, please share
        Hide
        Dan Adams added a comment -

        At Kevin's suggestion I'm going to change the name to 'Cached'.

        Show
        Dan Adams added a comment - At Kevin's suggestion I'm going to change the name to 'Cached'.
        Hide
        Kevin Menard added a comment -

        This seems very similar to the Tacos Cached annotation:

        http://tacos.sourceforge.net/tacos4.1/tacos-annotations/index.html

        Any chance we could maintain name parity?

        Show
        Kevin Menard added a comment - This seems very similar to the Tacos Cached annotation: http://tacos.sourceforge.net/tacos4.1/tacos-annotations/index.html Any chance we could maintain name parity?

          People

          • Assignee:
            Dan Adams
            Reporter:
            Dan Adams
          • Votes:
            1 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development