Answers.Body: <p>If you're running Craft CMS <a href="https://craftcms.com/changelog#build2783" rel="nofollow">2.6.2783</a>, you can pass in an <code>'expires'</code> key to the optional <code>$options</code> 2nd parameter of <a href="https://craftcms.com/classreference/controllers/BaseController#returnJson-detail" rel="nofollow"><code>returnJson</code></a> with the value set to the time in seconds that will be passed to the cache control expiration headers.  If you ignore it, it will have the current behavior of not caching at all.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-02T13:09:44.087Z
Answers.Text: Thanks Brad! Just saw this in the 2.6.2783 update: https://craftcms.com/classreference/controllers/BaseController#returnJson-detail
Answers.UserDisplayName: Aaron Berkowitz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-21T18:04:40.873Z
Answers.LastActivityDate: 2016-04-30T04:32:43.663Z
Answers.LastEditDate: 2016-04-30T04:32:43.663Z
Answers.UserDisplayName: Brad Bell
Body: <p>From a custom plugin controller, I am returning data as json using <a href="https://craftcms.com/classreference/controllers/BaseController#returnJson-detail" rel="nofollow"><code>$this-&gt;returnJson($data)</code></a>. I need to be able to set the Cache-Control headers for my forward cache proxy to handle caching correctly. </p>
  
  <p>I tried using <a href="https://craftcms.com/classreference/helpers/HeaderHelper#setExpires-detail" rel="nofollow"><code>HeaderHelper::setExpires(300)</code></a>, but <a href="https://craftcms.com/classreference/helpers/JsonHelper#sendJsonHeaders-detail" rel="nofollow"><code>JsonHelper::sendJsonHeaders()</code></a>, which is called by <a href="https://craftcms.com/classreference/controllers/BaseController#returnJson-detail" rel="nofollow"><code>$this-&gt;returnJson()</code></a> from the controller, overrides <code>setExpires</code> by calling <a href="https://craftcms.com/classreference/helpers/HeaderHelper#setNoCache-detail" rel="nofollow"><code>HeaderHelper::setNoCache()</code></a>. </p>
  
  <p>Does anyone have any ideas for working around this or configuring <code>returnJson</code>? I can do the following in my controller (or create a helper of my own):</p>
  
  <pre><code>HeaderHelper::setExpires(300);
  HeaderHelper::setContentTypeByExtension('json');
  ob_start();
  echo JsonHelper::encode($return);
  craft()-&gt;end();
  </code></pre>
  
  <p>but I'd rather use the super convenient <code>returnJson</code> function.</p>
  
Tags: <plugin-development><cache><json>
Title: Using setExpires on returnJson
ThreadText: # Using setExpires on returnJson
  
  (Asked by: username_0 on 2016-04-21)
  
  From a custom plugin controller, I am returning data as json using [`$this->returnJson($data)`](https://craftcms.com/classreference/controllers/BaseController#returnJson-detail). I need to be able to set the Cache-Control headers for my forward cache proxy to handle caching correctly.
  
  I tried using [`HeaderHelper::setExpires(300)`](https://craftcms.com/classreference/helpers/HeaderHelper#setExpires-detail), but [`JsonHelper::sendJsonHeaders()`](https://craftcms.com/classreference/helpers/JsonHelper#sendJsonHeaders-detail), which is called by [`$this->returnJson()`](https://craftcms.com/classreference/controllers/BaseController#returnJson-detail) from the controller, overrides `setExpires` by calling [`HeaderHelper::setNoCache()`](https://craftcms.com/classreference/helpers/HeaderHelper#setNoCache-detail).
  
  Does anyone have any ideas for working around this or configuring `returnJson`? I can do the following in my controller (or create a helper of my own):
  
  ```
  HeaderHelper::setExpires(300);
  HeaderHelper::setContentTypeByExtension('json');
  ob_start();
  echo JsonHelper::encode($return);
  craft()->end();
  
  ```
  
  but I'd rather use the super convenient `returnJson` function.
  
  **Tags:** plugin-development, cache, json
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-04-21. Score: 3 | Accepted answer
  
  If you're running Craft CMS [2.6.2783](https://craftcms.com/changelog#build2783), you can pass in an `'expires'` key to the optional `$options` 2nd parameter of [`returnJson`](https://craftcms.com/classreference/controllers/BaseController#returnJson-detail) with the value set to the time in seconds that will be passed to the cache control expiration headers. If you ignore it, it will have the current behavior of not caching at all.
  
  > **Comments:**
  > **username_0**: Thanks username_1! Just saw this in the 2.6.2783 update: https://craftcms.com/classreference/controllers/BaseController#returnJson-detail  
  
  ---
  
  