SessionCacheContext.php

Same filename in other branches
  1. 9 core/lib/Drupal/Core/Cache/Context/SessionCacheContext.php
  2. 10 core/lib/Drupal/Core/Cache/Context/SessionCacheContext.php
  3. 11.x core/lib/Drupal/Core/Cache/Context/SessionCacheContext.php

Namespace

Drupal\Core\Cache\Context

File

core/lib/Drupal/Core/Cache/Context/SessionCacheContext.php

View source
<?php

namespace Drupal\Core\Cache\Context;

use Drupal\Component\Utility\Crypt;

/**
 * Defines the SessionCacheContext service, for "per session" caching.
 *
 * Cache context ID: 'session'.
 */
class SessionCacheContext extends RequestStackCacheContextBase {
    
    /**
     * {@inheritdoc}
     */
    public static function getLabel() {
        return t('Session');
    }
    
    /**
     * {@inheritdoc}
     */
    public function getContext() {
        $request = $this->requestStack
            ->getCurrentRequest();
        if ($request->hasSession()) {
            return Crypt::hashBase64($request->getSession()
                ->getId());
        }
        return 'none';
    }

}

Classes

Title Deprecated Summary
SessionCacheContext Defines the SessionCacheContext service, for "per session" caching.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.