Answers.Body: <p>There're various ways of doing this. The simplest would be to just use the submitted slugs to generate a new cache key for each different search, like Jamie suggests. This is a very straightforward and readable solution, which in most cases would probably yield a "good enough" performance boost. You'd simply create a key based on the params that are used in your cached content, so something like (could be made a bit less verbose if you prefer):</p>
  
  <pre><code>{% set submittedTypesArray = craft.request.getParam('type') %}
  {% set submittedTypesString = submittedTypesArray | join('-') %}
  
  {% cache using key "restaurant-filter-type-" ~ submittedTypesString %}
      {% set typeList = craft.categories.group('restaurantType') %}
  
      {% for type in typeList %}
          &lt;div class="field field--checkbox"&gt;
              &lt;input type="checkbox" name="type[]" id="{{ type.slug }}" value="{{ type.slug }}"{% if type.slug in submittedTypesArray %} checked{% endif %}&gt;
              &lt;label for="{{ type.slug }}"&gt;{{ type.title }}&lt;/label&gt;
          &lt;/div&gt;
      {% endfor %}
  {% endcache %}
  </code></pre>
  
  <p>The backdraw of this solution is that it generates one cache entry for every different combination of the types. If this is "good enough" depends on your project. </p>
  
  <p>If you want to use the approach naboovalley describes in the linked answer, here is a rewrite for your usecase:</p>
  
  <pre><code>{% set typeCategories %}
      {% cache using key "restaurant-filter-type" %}
          {% set typeList = craft.categories.group('restaurantType') %}
  
          {% for type in typeList %}
              &lt;div class="field field--checkbox"&gt;
                  &lt;input type="checkbox" name="type[]" id="{{ type.slug }}" value="{{ type.slug }}" data-type-{{ type.slug }}&gt;
                  &lt;label for="{{ type.slug }}"&gt;{{ type.title }}&lt;/label&gt;
              &lt;/div&gt;
          {% endfor %}
      {% endcache %}
  {% endset %}
  
  {% set types = craft.request.getParam('type') %}    
  {% for type in types %}
      {% set typeCategories = typeCategories | replace('data-type-' ~ type, 'checked')  %}
  {% endfor %}
  
  {{ typeCategories }}
  </code></pre>
  
  <p>It would be possible to do this with several category groups inside one variable/cache entry, but you'd have to give them different data-names to make sure that you don't run into problems if two categories in different groups have the same slug.</p>
  
  <p>I'm sure there's a wicked elegant way to do the replace without the for loop, I just couldn't wrap my head around it right now. ;)</p>
  
  <p>And, just to add more options, if this was a really high traffic site (or the server that hosts the site is really crappy), I'd actually update the status of the checkboxes with javascript on the client, instead of doing it with Craft. Even though it would maybe annoy a user that has javascript turned off and has to check all the checkboxes for every search, nothing would break. And you can't beat the performance of doing something on the client instead of on the server.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-28T11:59:08.350Z
Answers.LastActivityDate: 2015-07-28T12:05:54.170Z
Answers.LastEditDate: 2015-07-28T12:05:54.170Z
Answers.UserDisplayName: André Elvan
Body: <p>I have created a search form which allows user to search multiple categories groups and multiple categories within those groups. </p>
  
  <p>So I have</p>
  
  <pre><code>Type 
  
  [] Category 1
  
  [x] Category 2
  
  [] Category 3
  
  ...
  
  
  Location
  
  [] Category 1
  
  [x] Category 2
  
  [] Category 3
  
  [] Category 4
  </code></pre>
  
  <p>I've chosen to use slugs as the search parameter for usability purposes but that means that I have to get the element of each slug and then pass it into the entries query. Long story short,lots of queries to my database before I even get started outputting the categories above.</p>
  
  <p>I'm interested in caching the category queries and I'm doing as follows</p>
  
  <pre><code>{% cache using key "restaurant-filter-type" %}
  
      {% set typeList = craft.categories.group('restaurantType') %}
  
      {% for type in typeList %}
  
          &lt;div class="field field--checkbox"&gt;
              &lt;input type="checkbox" name="type[]" id="{{ type.slug }}" value="{{ type.slug }}"&gt;
              &lt;label for="{{ type.slug }}"&gt;{{ type.title }}&lt;/label&gt;
          &lt;/div&gt;
  
     {% endfor %}
  
  {% endcache %}
  </code></pre>
  
  <p>The problem I run into is when the user submits the form and the checked form elements arent being persistent because of the caching I've set. </p>
  
  <p>I've tried looking at <a href="https://craftcms.stackexchange.com/questions/4782/adding-active-class-to-globally-cached-navigation">this SE question</a> to help but the stumbling block for this is that there is the possibility that the parameter holding my restaurant type slugs may hold multiple values, hence the <code>name="type[]"</code>. A simple <code>replace</code> filter doesn't look likely to work in that situation. </p>
  
  <p>Would anyone have any ideas how I could get around this?  </p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-07-25T07:00:41.923Z
Comments.Text: Are the parameters added to the URL as query strings when you submit the form (using GET method) ? If they are you should use the full URL in your cache key so that a different cache is created for each.
Comments.UserDisplayName: Jamie Pittock
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-07-25T11:47:23.570Z
Comments.Text: Yes it is, i just look it out for brevity.
Comments.UserDisplayName: Seán O'Grady
Tags: <form><cache><search><performance>
Title: Is it possible to cache a faceted search form?
ThreadText: # Is it possible to cache a faceted search form?
  
  (Asked by: username_0 on 2015-07-24)
  
  I have created a search form which allows user to search multiple categories groups and multiple categories within those groups.
  
  So I have
  
  ```
  Type 
  
  [] Category 1
  
  [x] Category 2
  
  [] Category 3
  
  ...
  
  Location
  
  [] Category 1
  
  [x] Category 2
  
  [] Category 3
  
  [] Category 4
  
  ```
  
  I've chosen to use slugs as the search parameter for usability purposes but that means that I have to get the element of each slug and then pass it into the entries query. Long story short,lots of queries to my database before I even get started outputting the categories above.
  
  I'm interested in caching the category queries and I'm doing as follows
  
  ```
  {% cache using key "restaurant-filter-type" %}
  
      {% set typeList = craft.categories.group('restaurantType') %}
  
      {% for type in typeList %}
  
          <div class="field field--checkbox">
              <input type="checkbox" name="type[]" id="{{ type.slug }}" value="{{ type.slug }}">
              <label for="{{ type.slug }}">{{ type.title }}</label>
          </div>
  
     {% endfor %}
  
  {% endcache %}
  
  ```
  
  The problem I run into is when the user submits the form and the checked form elements arent being persistent because of the caching I've set.
  
  I've tried looking at [this SE question](https://craftcms.stackexchange.com/questions/4782/adding-active-class-to-globally-cached-navigation) to help but the stumbling block for this is that there is the possibility that the parameter holding my restaurant type slugs may hold multiple values, hence the `name="type[]"`. A simple `replace` filter doesn't look likely to work in that situation.
  
  Would anyone have any ideas how I could get around this?
  
  **Tags:** form, cache, search, performance
  
  > **Comments:**
  > **username_1**: Are the parameters added to the URL as query strings when you submit the form (using GET method) ? If they are you should use the full URL in your cache key so that a different cache is created for each.  
  > **username_0**: Yes it is, i just look it out for brevity.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2015-07-28. Score: 4 | Accepted answer
  
  There're various ways of doing this. The simplest would be to just use the submitted slugs to generate a new cache key for each different search, like username_1 suggests. This is a very straightforward and readable solution, which in most cases would probably yield a "good enough" performance boost. You'd simply create a key based on the params that are used in your cached content, so something like (could be made a bit less verbose if you prefer):
  
  ```
  {% set submittedTypesArray = craft.request.getParam('type') %}
  {% set submittedTypesString = submittedTypesArray | join('-') %}
  
  {% cache using key "restaurant-filter-type-" ~ submittedTypesString %}
      {% set typeList = craft.categories.group('restaurantType') %}
  
      {% for type in typeList %}
          <div class="field field--checkbox">
              <input type="checkbox" name="type[]" id="{{ type.slug }}" value="{{ type.slug }}"{% if type.slug in submittedTypesArray %} checked{% endif %}>
              <label for="{{ type.slug }}">{{ type.title }}</label>
          </div>
      {% endfor %}
  {% endcache %}
  
  ```
  
  The backdraw of this solution is that it generates one cache entry for every different combination of the types. If this is "good enough" depends on your project.
  
  If you want to use the approach naboovalley describes in the linked answer, here is a rewrite for your usecase:
  
  ```
  {% set typeCategories %}
      {% cache using key "restaurant-filter-type" %}
          {% set typeList = craft.categories.group('restaurantType') %}
  
          {% for type in typeList %}
              <div class="field field--checkbox">
                  <input type="checkbox" name="type[]" id="{{ type.slug }}" value="{{ type.slug }}" data-type-{{ type.slug }}>
                  <label for="{{ type.slug }}">{{ type.title }}</label>
              </div>
          {% endfor %}
      {% endcache %}
  {% endset %}
  
  {% set types = craft.request.getParam('type') %}    
  {% for type in types %}
      {% set typeCategories = typeCategories | replace('data-type-' ~ type, 'checked')  %}
  {% endfor %}
  
  {{ typeCategories }}
  
  ```
  
  It would be possible to do this with several category groups inside one variable/cache entry, but you'd have to give them different data-names to make sure that you don't run into problems if two categories in different groups have the same slug.
  
  I'm sure there's a wicked elegant way to do the replace without the for loop, I just couldn't wrap my head around it right now. ;)
  
  And, just to add more options, if this was a really high traffic site (or the server that hosts the site is really crappy), I'd actually update the status of the checkboxes with javascript on the client, instead of doing it with Craft. Even though it would maybe annoy a user that has javascript turned off and has to check all the checkboxes for every search, nothing would break. And you can't beat the performance of doing something on the client instead of on the server.
  
  
  ---
  
  