<p>Answering my own question:</p>

<p>It turns our the image transforms weren't generating as the 'Generating Pending Image Transforms' task would get stuck. I pushed the task through by restarting, and the cache hit ratio went to 98-99%.</p>

<p>I've installed Imagemagick to hopefully help the transforms task to be more successful going forward.</p>

CC BY-SA 4.0
2019-12-20T18:58:48.737Z
2019-12-20T18:58:48.737Z
BrianV
<p>I am working on a Craft 2 site that is exhibiting some rather poor performance characteristics. </p>

<p>On production, we use memcache for caching, and monitoring on the server shows disappointing cache hit ratios on the memcache server - typically ~10% for the first while after a manual cache clear, rising to and stabilizing near 80% after a few days. Given that traffic is concentrated on a handful of pages, this is not the profile we would expect.</p>

<p>To confirm this, I have been doing some testing on my local using the <code>db</code> cache method, and I am seeing a similar profile. For instance, on a category page, we have the following cached elements:</p>

<ol>
<li>A list of sub categories</li>
<li>A list of articles within this category</li>
<li>A list of related events</li>
<li>A list of people related to this category</li>
</ol>

<p>Each of these is cached. The <code>{% cache %}</code>/<code>{% endcache %}</code> block for each of these contains both the <code>craft.entries</code> or <code>craft.categories</code> queries used to fetch the content as well as the Twig code to generate the markup.</p>

<p>However, on a page load, only the list of subcategories gets populated in the <code>templatecaches</code> database table, with corresponding criteria in <code>templatecachecriteria</code>.</p>

<p>This patterns is repeated on all pages of the site, where only one or two cached chunks will actually get stored in the cache, leading to a local cache hit ratio in line with what we are seeing on production.</p>

<p>The bottom line question is this: What would cause Craft to not store the contents of a <code>{% cache %}</code>/<code>{% endcache %}</code> block?</p>

<p>Edit: Here is a copy of the category template described above:</p>

<pre><code>{% extends "_layout" %}

{% set title = category.title %}
{% set heroImage = category.heroImage.first ?? null %}


{% block content %}
  &lt;article&gt;
    &lt;header&gt;
      {% if heroImage %}
        {% include '_shared/hero' with { showAttribution: false } %}
      {% endif %}

      &lt;div class="article__header {% if heroImage %}-overlap{% endif %} -blue wrapper -extra-large"&gt;

        {% include '_shared/image-attribution' %}

        {% if category.level &gt; 1 %}
          &lt;p class="overlap-label margin-bottom"&gt;
            &lt;span class="overlap-label__underline"&gt;Part of&lt;/span&gt;
            &lt;a href="{{ category.parent.url }}"&gt;
              &lt;span class="chevron -left -gray"&gt;&lt;/span&gt;
              {{ category.parent }}
            &lt;/a&gt;
          &lt;/p&gt;
        {% endif %}

        {% cache globally using key category.title ~ ':subCategories'%}
        &lt;h1 class="page-title"&gt;{{ category.title }}&lt;/h1&gt;
        {% set subCategories = craft.categories({
        group: 'researchArea',
        descendantOf: category,
        descendantDist: 1
        }) %}
        {% if subCategories | length and category.level &lt; 3 %}
          &lt;p class="overlap-label margin-top"&gt;
            &lt;span class="overlap-label__underline"&gt;Within {{ category.title }}&lt;/span&gt;
          &lt;/p&gt;

          {% include '_shared/category-list-columns' with { categories: subCategories } %}
        {% endif %}
        {% endcache %}
      &lt;/div&gt;
    &lt;/header&gt;

    {% if category.level == 1 %}
      &lt;div class="wrapper wysiwyg margin-vertical"&gt;
        {% if category.categoryDescription | length %}
          &lt;p&gt;{{ category.categoryDescription }}&lt;/p&gt;
        {% endif %}
      &lt;/div&gt;

      &lt;p class="center"&gt;&lt;a href="{{ general.donationPage }}" class="button -solid"&gt;Support ACME&lt;/a&gt;&lt;/p&gt;
    {% endif %}

    {% if category.featuredPublications | length %}
      &lt;section class="featured-block margin-vertical-2x"&gt;
        &lt;h2 class="featured-block__title"&gt;Highlights&lt;/h2&gt;

        {% include '_shared/featured-listing' with { entries: category.featuredPublications } %}
      &lt;/section&gt;
    {% endif %}

    {% if category.level == 2 %}
      &lt;div class="wrapper wysiwyg margin-vertical"&gt;
        {% if category.categoryDescription | length %}
          &lt;p&gt;{{ category.categoryDescription }}&lt;/p&gt;
        {% endif %}

        &lt;p class="center margin-vertical-2x"&gt;&lt;a href="{{ general.donationPage }}" class="button -solid"&gt;Support ACME&lt;/a&gt;&lt;/p&gt;
      &lt;/div&gt;
    {% endif %}
  &lt;/article&gt;

  {% cache globally using key category.title ~ ':recentPublications' %}
  {% set recentPublications = craft.entries({
  relatedTo: category,
  section: ['reports', 'articlesMultimedia'],
  limit: 5,
  with: [
  'heroImage',
  'titleImage',
  'associatedPeople',
  'source'
  ]
  }) %}
  {% if recentPublications | length %}
    &lt;section class="wrapper -extra-large margin-vertical-2x"&gt;
      &lt;h2 class="section-title margin-bottom-half-em"&gt;Recent Publications&lt;/h2&gt;

      &lt;div class="wrapper"&gt;
          {% include '_shared/entry-listing' with { entries: recentPublications } %}
        &lt;p class="margin-top-1em"&gt;
          &lt;a href="/reports?area={{ category.slug }}" class="button"&gt;View All Reports&lt;/a&gt;
          &lt;a href="/articles-multimedia?area={{ category.slug }}" class="button"&gt;View All Articles &amp; Multimedia&lt;/a&gt;
        &lt;/p&gt;
      &lt;/div&gt;
    &lt;/section&gt;
  {% endif %}
  {% endcache %}

  {% cache globally using key category.title ~ ':relatedEvents'%}
    {% set relatedEvents = craft.entries({
    relatedTo: category,
    section: 'events',
    limit: 3,
    with: [
    'heroImage',
    'researchAreas'
    ]
    }) %}
    {% if relatedEvents | length %}
      &lt;section class="wrapper -extra-large margin-vertical-2x"&gt;
        &lt;h2 class="section-title margin-bottom-half-em"&gt;Related Events&lt;/h2&gt;

        &lt;div class="wrapper -extra-large"&gt;
            {% include '_shared/blocks/photoListingMultiple' with {
              featuredEntries: relatedEvents,
              columns: '3'
            } %}
          &lt;p class="margin-top-1em"&gt;
            &lt;a href="/events?area={{ category.slug }}" class="button"&gt;View All Events&lt;/a&gt;
          &lt;/p&gt;
        &lt;/div&gt;
      &lt;/section&gt;
    {% endif %}
  {% endcache %}


  {% cache globally using key category.title ~ ':researchTeam'%}
    {% set researchTeam = craft.entries({
    relatedTo: category,
    section: 'people',
    limit: category.level == 1 ? 15 : 10,
    with: [
    'thumbnailImage'
    ]
    }) %}
    {% if researchTeam | length %}
      &lt;section class="wrapper -extra-large margin-vertical-2x"&gt;
        &lt;h2 class="section-title margin-bottom-half-em"&gt;Research Team&lt;/h2&gt;

        &lt;div class="wrapper -large"&gt;
          &lt;ul class="person-list"&gt;
              {% for person in researchTeam %}
                &lt;li&gt;
                  {% set image = person.thumbnailImage[0] ?? null %}

                  {% if image %}
                    &lt;img
                      src="{{ img.spacer }}"
                      srcset="{{ image.url({ width: 120 }) }} 1x,
                              {{ image.url({ width: 240 }) }} 2x"
                      width="120"
                      height="120"
                      class="desaturate"
                      alt=""&gt;
                  {% else %}
                    &lt;img src="/assets/images/thumbnail.jpg" width="120" height="120" alt=""&gt;
                  {% endif %}

                  &lt;h3 class="h4 margin-vertical-half-em person-list__name"&gt;
                    &lt;a href="{{ person.url }}"&gt;{{ person.title }}&lt;/a&gt;
                  &lt;/h3&gt;

                  {% if person.personsTitle or person.personsAffiliation %}
                    &lt;p class="person-list__title"&gt;{{ person.personsTitle }}{% if person.personsTitle and person.personsAffiliation %}, {% endif %}{{ person.personsAffiliation }}&lt;/p&gt;
                  {% endif %}

                &lt;/li&gt;
              {% endfor %}
          &lt;/ul&gt;
        &lt;/div&gt;
      &lt;/section&gt;
    {% endif %}
  {% endcache %}
{% endblock %}
</code></pre>

<cache><craft2>
Craft 2: What would prevent most cached template chunks from showing up in the `templatecaches` table?
# Craft 2: What would prevent most cached template chunks from showing up in the `templatecaches` table?

(Asked by: username_0 on 2019-12-17)

I am working on a Craft 2 site that is exhibiting some rather poor performance characteristics. 
On production, we use memcache for caching, and monitoring on the server shows disappointing cache hit ratios on the memcache server - typically ~10% for the first while after a manual cache clear, rising to and stabilizing near 80% after a few days. Given that traffic is concentrated on a handful of pages, this is not the profile we would expect.
To confirm this, I have been doing some testing on my local using the db cache method, and I am seeing a similar profile. For instance, on a category page, we have the following cached elements:

A list of sub categories
A list of articles within this category
A list of related events
A list of people related to this category

Each of these is cached. The {% cache %}/{% endcache %} block for each of these contains both the craft.entries or craft.categories queries used to fetch the content as well as the Twig code to generate the markup.
However, on a page load, only the list of subcategories gets populated in the templatecaches database table, with corresponding criteria in templatecachecriteria.
This patterns is repeated on all pages of the site, where only one or two cached chunks will actually get stored in the cache, leading to a local cache hit ratio in line with what we are seeing on production.
The bottom line question is this: What would cause Craft to not store the contents of a {% cache %}/{% endcache %} block?
Edit: Here is a copy of the category template described above:
{% extends "_layout" %}

{% set title = category.title %}
{% set heroImage = category.heroImage.first ?? null %}


{% block content %}
  
    
      {% if heroImage %}
        {% include '_shared/hero' with { showAttribution: false } %}
      {% endif %}

      

        {% include '_shared/image-attribution' %}

        {% if category.level > 1 %}
          
            Part of
            
              
              {{ category.parent }}
            
          
        {% endif %}

        {% cache globally using key category.title ~ ':subCategories'%}
        {{ category.title }}
        {% set subCategories = craft.categories({
        group: 'researchArea',
        descendantOf: category,
        descendantDist: 1
        }) %}
        {% if subCategories | length and category.level < 3 %}
          
            Within {{ category.title }}
          

          {% include '_shared/category-list-columns' with { categories: subCategories } %}
        {% endif %}
        {% endcache %}
      
    

    {% if category.level == 1 %}
      
        {% if category.categoryDescription | length %}
          {{ category.categoryDescription }}
        {% endif %}
      

      Support ACME
    {% endif %}

    {% if category.featuredPublications | length %}
      
        Highlights

        {% include '_shared/featured-listing' with { entries: category.featuredPublications } %}
      
    {% endif %}

    {% if category.level == 2 %}
      
        {% if category.categoryDescription | length %}
          {{ category.categoryDescription }}
        {% endif %}

        Support ACME
      
    {% endif %}
  

  {% cache globally using key category.title ~ ':recentPublications' %}
  {% set recentPublications = craft.entries({
  relatedTo: category,
  section: ['reports', 'articlesMultimedia'],
  limit: 5,
  with: [
  'heroImage',
  'titleImage',
  'associatedPeople',
  'source'
  ]
  }) %}
  {% if recentPublications | length %}
    
      Recent Publications

      
          {% include '_shared/entry-listing' with { entries: recentPublications } %}
        
          View All Reports
          View All Articles & Multimedia
        
      
    
  {% endif %}
  {% endcache %}

  {% cache globally using key category.title ~ ':relatedEvents'%}
    {% set relatedEvents = craft.entries({
    relatedTo: category,
    section: 'events',
    limit: 3,
    with: [
    'heroImage',
    'researchAreas'
    ]
    }) %}
    {% if relatedEvents | length %}
      
        Related Events

        
            {% include '_shared/blocks/photoListingMultiple' with {
              featuredEntries: relatedEvents,
              columns: '3'
            } %}
          
            View All Events
          
        
      
    {% endif %}
  {% endcache %}


  {% cache globally using key category.title ~ ':researchTeam'%}
    {% set researchTeam = craft.entries({
    relatedTo: category,
    section: 'people',
    limit: category.level == 1 ? 15 : 10,
    with: [
    'thumbnailImage'
    ]
    }) %}
    {% if researchTeam | length %}
      
        Research Team

        
          
              {% for person in researchTeam %}
                
                  {% set image = person.thumbnailImage[0] ?? null %}

                  {% if image %}
                    
                  {% else %}
                    
                  {% endif %}

                  
                    {{ person.title }}
                  

                  {% if person.personsTitle or person.personsAffiliation %}
                    {{ person.personsTitle }}{% if person.personsTitle and person.personsAffiliation %}, {% endif %}{{ person.personsAffiliation }}
                  {% endif %}

                
              {% endfor %}
          
        
      
    {% endif %}
  {% endcache %}
{% endblock %}

**Tags:** cache, craft2

---

## 1 answers

### Answer by username_0 on 2019-12-20. Score: 1 | Accepted answer

Answering my own question:

It turns our the image transforms weren't generating as the 'Generating Pending Image Transforms' task would get stuck. I pushed the task through by restarting, and the cache hit ratio went to 98-99%.

I've installed Imagemagick to hopefully help the transforms task to be more successful going forward.


---

