Answers.Body: <p>Easy! Get the most soonest upcoming pending entry, and use <code>until</code>:</p>
  
  <pre><code>{% set firstPending = craft.entries({
    section: 'articles',
    status: 'pending',
    order: 'postDate asc'
  }).first %}
  {% set cacheUntil = firstPending ? firstPending.postDate : now|date_modify('+1 year') %}
  
  {% set content %}
      {% cache until cacheUntil %}
          ...
      {% endcache %}
  {% endset %}
  </code></pre>
  
  <p>Note that this solution requires <a href="http://buildwithcraft.com/updates#build2598" rel="noreferrer">Craft 2.2.2598</a> or later.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-18T16:12:33.333Z
Answers.Text: Not sure if there's a way to include the `until` statement conditionally all-together, that way you wouldn't need to do `now|date_modify('+1 year')`
Answers.UserDisplayName: Tim Kelty
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-18T16:13:22.313Z
Answers.Text: `if craft.config.enableCache` is just a custom config var I'm using, doesn't have anything to do with the question.
Answers.UserDisplayName: Tim Kelty
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-18T16:29:45.077Z
Answers.Text: Just FYI... It's not necessary to specify "limit: 1" when you're using `.first`.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-18T18:23:28.260Z
Answers.Text: Changed my code to reflect that!
Answers.UserDisplayName: Tim Kelty
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-22T21:39:01.847Z
Answers.Text: Your original solution will work with a small tweak to the way cache clearing works in Craft. The next update will include that tweak.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-23T13:32:40.817Z
Answers.Text: Awesome, looking forward to it. Thanks, B!
Answers.UserDisplayName: Tim Kelty
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-28T09:50:51.200Z
Answers.Text: @TimKelty As of [Craft 2.2.2598](http://buildwithcraft.com/updates#build2598) your original solution should work. Give it a shot!
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-18T16:11:28.400Z
Answers.LastActivityDate: 2016-05-02T08:26:39.390Z
Answers.LastEditDate: 2016-05-02T08:26:39.390Z
Answers.UserDisplayName: Tim Kelty
Body: <p>I have a listing page of entries, some of which may have future-dated postDates. I want to cache the output, but only until one of the pending entries goes live.</p>
  
Tags: <postdate><pending>
Title: How can I cache a list of entries with pending postDates and have it automatically invalidate the cache appropriately?
ThreadText: # How can I cache a list of entries with pending postDates and have it automatically invalidate the cache appropriately?
  
  (Asked by: username_0 on 2014-09-18)
  
  I have a listing page of entries, some of which may have future-dated postDates. I want to cache the output, but only until one of the pending entries goes live.
  
  **Tags:** postdate, pending
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2014-09-18. Score: 18 | Accepted answer
  
  Easy! Get the most soonest upcoming pending entry, and use `until`:
  
  ```
  {% set firstPending = craft.entries({
    section: 'articles',
    status: 'pending',
    order: 'postDate asc'
  }).first %}
  {% set cacheUntil = firstPending ? firstPending.postDate : now|date_modify('+1 year') %}
  
  {% set content %}
      {% cache until cacheUntil %}
          ...
      {% endcache %}
  {% endset %}
  
  ```
  
  Note that this solution requires [Craft 2.2.2598](http://buildwithcraft.com/updates#build2598) or later.
  
  > **Comments:**
  > **username_0**: Not sure if there's a way to include the `until` statement conditionally all-together, that way you wouldn't need to do `now|date_modify('+1 year')`  
  > **username_0**: `if craft.config.enableCache` is just a custom config var I'm using, doesn't have anything to do with the question.  
  > **username_1**: Just FYI... It's not necessary to specify "limit: 1" when you're using `.first`.  
  > **username_0**: Changed my code to reflect that!  
  > **username_2**: Your original solution will work with a small tweak to the way cache clearing works in Craft. The next update will include that tweak.  
  > **username_0**: Awesome, looking forward to it. Thanks, B!  
  > **username_2**: @username_0 As of [Craft 2.2.2598](http://buildwithcraft.com/updates#build2598) your original solution should work. Give it a shot!  
  
  ---
  
  