I am working on REST API and I would really like to be able to use the same mechanism for responses as Drupal uses for cached content.

Unfortunately there is no render cache, everything is JSON responses. So there are no #ccache setting or page cache policy to be applied out of the box.

So I wonder if it is even possible and if so how should I approach it?

What I'm after basically is to cache the responses in the same manner as entities for example. So if the context is specific user and I am displaying entity information I would like to get fresh information if the entity is changed but the same content if not and the user is the same..and so on.

1 Answer 1


You have to use a cacheable response and then you can add cacheable metadata from a render array or from an object directly:

use Drupal\Core\Cache\CacheableJsonResponse;
use Drupal\Core\Cache\CacheableMetadata;

  $response = new CacheableJsonResponse($json, 200);



If you use the core REST API this should already be implemented.

  • Oh, didn't know about that. Will check it out. Thanks.
    – user21641
    Commented Oct 31, 2016 at 9:45
  • @4k4 Why I Can not render html first to be able to put in a JSON example to be printed?
    – BOES
    Commented Jan 18, 2017 at 22:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.