Answers.Body: <p>One issue I'm seeing, is that you're currently using <code>craft.request.path</code> as your cache <code>key</code> – however, that property doesn't actually include the pagination segment (i.e. the <a href="https://docs.craftcms.com/v2/config-settings.html#urls" rel="nofollow noreferrer"><code>pageTrigger</code></a>; <code>p2</code>, <code>p3</code> or the like), which means that you're currently going to see the same, cached content across all of your pages, because you're caching <code>globally</code> using an identical key (i.e. the root URI of your paginated template).</p>
  
  <p>For Craft 2, AFAIK there's no single property or method on the <a href="https://docs.craftcms.com/api/v2/craft-httprequestservice.html" rel="nofollow noreferrer">HttpRequestService</a> that will give you the full URL including the pagination segment – so you'll need to append the pagination number using <code>craft.request.getPageNum()</code>:  </p>
  
  <pre><code>{% cache globally using key craft.request.path ~ craft.request.getPageNum() until cacheUntil %}
  </code></pre>
  
  <p>For Craft 3, there's <code>craft.app.request.getFullPath()</code>, which does include the pagination segment – so the following should do the trick:</p>
  
  <pre><code>{% cache globally using key craft.app.request.getFullPath() until cacheUntil %}
  </code></pre>
  
  <p>As far as your solution for breaking the cache whenever "pending" entries are published, I'm not sure, but I'm not seeing any problems with it, at a glance.</p>
  
  <p>The reason why you're not seeing the cache cleared when you create new entries, is probably because your second <code>craft.entries</code> query (the one that actually fetches the entries) is <em>outside</em> your <code>{% cache %}</code> tag (this would be a performance issue as well; your current code isn't really caching anything, except a bit of not too demanding Twig code). You should probably move the second <code>craft.entries</code> query, the <code>{% paginate %}</code> tag and the <code>{% include '_includes/partials/pagination' %}</code> statement <em>inside</em> the <code>{% cache %}</code> tag pair.</p>
  
  <p>Also, you might want to re-think having a default cache duration of 1 year – that's a <em>long</em> time, and if your site builds up a lot of pages, it could balloon your database and possibly cause issues with stalled <code>Deleting state template caches</code> tasks. My advice would be to keep the duration to something sane like 1 month, max – there's little point in keeping year-old caches around, normally :)</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-20T11:35:34.857Z
Answers.Text: Thanks for this Mats. Some very good and valid points. I have updated to a month cache and moved the code around to benefit from the caching and breaking of those items.
Answers.UserDisplayName: Terry Upton
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-19T19:38:45.900Z
Answers.LastActivityDate: 2018-07-19T19:48:10.613Z
Answers.LastEditDate: 2018-07-19T19:48:10.613Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>Any idea why this code wouldn’t update/break the cache when new items are created?</p>
  
  <p>Think perhaps the combination of my code for pagination, cache and the ability to also break for pending entries isn't correct and throwing it off...</p>
  
  <pre><code>{#
   # NEWS LISTING TEMPLATE
   # ---------------
   #}
  
  {% extends "_layouts/main" %}
  
  {# Get the most soonest upcoming pending entry for Cache breaking #}
  {% set firstPending = craft.entries({
    section: 'news',
    status: 'pending',
    order: 'postDate asc'
  }).first %}
  {% set cacheUntil = firstPending ? firstPending.postDate : now|date_modify('+1 year') %}
  
  {% set entries = craft.entries({
    section: 'news',
    order: 'postDate desc',
    limit: 6
  }) %}
  
  {# Get the articles and paginate them #}
  {% paginate entries as pageInfo, pageEntries %}
  
  {% block main %}
  {% cache globally using key craft.request.path until cacheUntil %}
  &lt;div class="sitecontainer"&gt;
    &lt;h1 class="border-bottom"&gt;{{ entry.title }}&lt;/h1&gt;
  
    {% for entry in pageEntries %}
    &lt;article class="leading-normal mb-8"&gt;
      &lt;div class="sm:flex sm:items-center mb-8 pb-6 {% if not loop.last %}border-b border-grey-light{% endif %}"&gt;
        &lt;div class="bg-petrol relative text-lime p-4 sm:w-40"&gt;
           &lt;time datetime="{{ entry.postDate|date('Y-m-d H:i:s') }}" class=" text-center mb-0 relative z-30 text-sm"&gt;
            &lt;div class="mx-auto fill-current w-6 h-6 mb-2"&gt;
              {% include '_includes/svg/icon-date.svg' %}
            &lt;/div&gt;
            &lt;div&gt;
              {{ helpers.dateFormatted(entry.postDate) }}
            &lt;/div&gt;
          &lt;/time&gt;
        &lt;/div&gt;
        &lt;div class="p-4 flex-1"&gt;
          &lt;h2 class="flex // flex-wrap items-center border-grey // text-base leading-normal font-semibold mb-1"&gt;
            &lt;a href="{{ entry.url }}"&gt;
              {{ entry.title }}
            &lt;/a&gt;
          &lt;/h2&gt;
          &lt;p class="mb-0 text-base"&gt;
            &lt;a href="{{ entry.url }}" class="arrow font-normal text-lime hover:text-blue"&gt;view news&lt;/a&gt;
          &lt;/p&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/article&gt;
    {% endfor %}
  &lt;/div&gt;
  {% endcache %}
  
  {% include '_includes/partials/pagination' %}
  {% endblock %}
  </code></pre>
  
Tags: <cache><pagination><pending>
Title: Cache not invalidating when using pagination with cache
ThreadText: # Cache not invalidating when using pagination with cache
  
  (Asked by: username_0 on 2018-07-19)
  
  Any idea why this code wouldn’t update/break the cache when new items are created?
  
  Think perhaps the combination of my code for pagination, cache and the ability to also break for pending entries isn't correct and throwing it off...
  
  ```
  {#
   # NEWS LISTING TEMPLATE
   # ---------------
   #}
  
  {% extends "_layouts/main" %}
  
  {# Get the most soonest upcoming pending entry for Cache breaking #}
  {% set firstPending = craft.entries({
    section: 'news',
    status: 'pending',
    order: 'postDate asc'
  }).first %}
  {% set cacheUntil = firstPending ? firstPending.postDate : now|date_modify('+1 year') %}
  
  {% set entries = craft.entries({
    section: 'news',
    order: 'postDate desc',
    limit: 6
  }) %}
  
  {# Get the articles and paginate them #}
  {% paginate entries as pageInfo, pageEntries %}
  
  {% block main %}
  {% cache globally using key craft.request.path until cacheUntil %}
  <div class="sitecontainer">
    <h1 class="border-bottom">{{ entry.title }}</h1>
  
    {% for entry in pageEntries %}
    <article class="leading-normal mb-8">
      <div class="sm:flex sm:items-center mb-8 pb-6 {% if not loop.last %}border-b border-grey-light{% endif %}">
        <div class="bg-petrol relative text-lime p-4 sm:w-40">
           <time datetime="{{ entry.postDate|date('Y-m-d H:i:s') }}" class=" text-center mb-0 relative z-30 text-sm">
            <div class="mx-auto fill-current w-6 h-6 mb-2">
              {% include '_includes/svg/icon-date.svg' %}
            </div>
            <div>
              {{ helpers.dateFormatted(entry.postDate) }}
            </div>
          </time>
        </div>
        <div class="p-4 flex-1">
          <h2 class="flex // flex-wrap items-center border-grey // text-base leading-normal font-semibold mb-1">
            <a href="{{ entry.url }}">
              {{ entry.title }}
            </a>
          </h2>
          <p class="mb-0 text-base">
            <a href="{{ entry.url }}" class="arrow font-normal text-lime hover:text-blue">view news</a>
          </p>
        </div>
      </div>
    </article>
    {% endfor %}
  </div>
  {% endcache %}
  
  {% include '_includes/partials/pagination' %}
  {% endblock %}
  
  ```
  
  **Tags:** cache, pagination, pending
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-07-19. Score: 2 | Accepted answer
  
  One issue I'm seeing, is that you're currently using `craft.request.path` as your cache `key` – however, that property doesn't actually include the pagination segment (i.e. the [`pageTrigger`](https://docs.craftcms.com/v2/config-settings.html#urls); `p2`, `p3` or the like), which means that you're currently going to see the same, cached content across all of your pages, because you're caching `globally` using an identical key (i.e. the root URI of your paginated template).
  
  For Craft 2, AFAIK there's no single property or method on the [HttpRequestService](https://docs.craftcms.com/api/v2/craft-httprequestservice.html) that will give you the full URL including the pagination segment – so you'll need to append the pagination number using `craft.request.getPageNum()`:
  
  ```
  {% cache globally using key craft.request.path ~ craft.request.getPageNum() until cacheUntil %}
  
  ```
  
  For Craft 3, there's `craft.app.request.getFullPath()`, which does include the pagination segment – so the following should do the trick:
  
  ```
  {% cache globally using key craft.app.request.getFullPath() until cacheUntil %}
  
  ```
  
  As far as your solution for breaking the cache whenever "pending" entries are published, I'm not sure, but I'm not seeing any problems with it, at a glance.
  
  The reason why you're not seeing the cache cleared when you create new entries, is probably because your second `craft.entries` query (the one that actually fetches the entries) is *outside* your `{% cache %}` tag (this would be a performance issue as well; your current code isn't really caching anything, except a bit of not too demanding Twig code). You should probably move the second `craft.entries` query, the `{% paginate %}` tag and the `{% include '_includes/partials/pagination' %}` statement *inside* the `{% cache %}` tag pair.
  
  Also, you might want to re-think having a default cache duration of 1 year – that's a *long* time, and if your site builds up a lot of pages, it could balloon your database and possibly cause issues with stalled `Deleting state template caches` tasks. My advice would be to keep the duration to something sane like 1 month, max – there's little point in keeping year-old caches around, normally :)
  
  > **Comments:**
  > **username_0**: Thanks for this username_1. Some very good and valid points. I have updated to a month cache and moved the code around to benefit from the caching and breaking of those items.  
  
  ---
  
  