How to handle the cache per user in REST Resource? I have a requirement to expose a specific content type data via REST end point. Below is my code:

public function get() {
    $current_user = $this-&gt;currentUser-&gt;id();
    $user = $this-&gt;entityTypeManager-&gt;getStorage('user')-&gt;load($current_user);
    $userLangId = $user-&gt;language()-&gt;getId();

    // You must to implement the logic of your REST Resource here.
    // Use current user after pass authentication to validate access.
    if (!$this-&gt;currentUser-&gt;hasPermission('access content')) {
      throw new AccessDeniedHttpException();
    }
    $news_values = [
      'langcode' =&gt; $userLangId,
      'type' =&gt; ['story'],
      'status' =&gt; 1,
    ];
    $news_data = $this-&gt;entityTypeManager-&gt;getStorage('node')-&gt;loadByProperties($news_values);
    $newsfeed = [];
    $newsfeed['stories'] = ['Story' =&gt; array_values($news_data)];
    if (empty($newsfeed)) {
      return new ResourceResponse('No Newsfeed exist for this user.', 400);
    }
    $response = new ResourceResponse($newsfeed, 200);
//    $response-&gt;addCacheableDependency($current_user);

    $response-&gt;addCacheableDependency(CacheableMetadata::createFromRenderArray([
      '#cache' =&gt; [
        'context' =&gt; ['user.roles'],
        'tags' =&gt; ['user:3', 'languages:' . $userLangId],
      ],
    ]));
    return $response;
  }


I am getting a proper response with this but I am facing issues in handling the cache.
This content is user specific. Based on user language content will popup when they access.

In Response Headers I am getting below things:

X-Drupal-Cache-Contexts: languages:language_interface theme url.site user.permissions
X-Drupal-Cache-Tags: config:filter.format.basic_html config:rest.resource.newsfeed_rest_resource config:rest.settings config:system.site http_response languages:fr node:7 node:9 user:3
X-Drupal-Dynamic-Cache: MISS/HIT


In Cache-tags i have not added node key but its automatically coming and its working once node updated. But if i try to login with different user via basic auth on postman client, content is not changing at all. Its showing cached content for previous user. How I can cache the content per user. Please help me in this regard.
@Leigh is right and user does exist. Cache contexts are hierarchical (see change record https://www.drupal.org/node/2451661) and user is a top level context.

So this should work (note the plural form of contexts):

  '#cache' =&gt; [
    'contexts' =&gt; ['user'],


You can also add the cache context by IDE autocompleting:

$response-&gt;getCacheableMetadata()-&gt;addCacheContexts(['user']);