master

laravel/framework

Last updated at: 29/12/2023 09:24

CacheBasedSessionHandler.php

TLDR

This file CacheBasedSessionHandler.php is part of the Illuminate\Session namespace and implements the SessionHandlerInterface. It provides a cache-based session handler that stores session data in a cache repository.

Methods

open

This method is called when a session is being opened. It takes two parameters, $savePath and $sessionName, but it always returns true indicating that the session was opened successfully.

close

This method is called when a session is being closed. It does not take any parameters and always returns true indicating that the session was closed successfully.

read

This method is called to read the session data from storage. It takes the $sessionId parameter and returns the session data as a string. It uses the cache repository to retrieve the session data, and if the data is not found, it returns an empty string.

write

This method is called to write the session data to storage. It takes the $sessionId and $data parameters and returns a boolean indicating whether the data was written successfully. It uses the cache repository to store the session data with an expiration time based on the configured number of minutes.

destroy

This method is called to destroy a session. It takes the $sessionId parameter and returns a boolean indicating whether the session was destroyed successfully. It uses the cache repository to remove the session data.

gc

This method is not used in this implementation. It is called by PHP's garbage collector and takes the $lifetime parameter, but it always returns 0 indicating that the garbage collection was not performed.

getCache

This method returns the underlying cache repository instance used by the session handler.

Classes

There are no additional classes in this file.

<?php

namespace Illuminate\Session;

use Illuminate\Contracts\Cache\Repository as CacheContract;
use SessionHandlerInterface;

class CacheBasedSessionHandler implements SessionHandlerInterface
{
    /**
     * The cache repository instance.
     *
     * @var \Illuminate\Contracts\Cache\Repository
     */
    protected $cache;

    /**
     * The number of minutes to store the data in the cache.
     *
     * @var int
     */
    protected $minutes;

    /**
     * Create a new cache driven handler instance.
     *
     * @param  \Illuminate\Contracts\Cache\Repository  $cache
     * @param  int  $minutes
     * @return void
     */
    public function __construct(CacheContract $cache, $minutes)
    {
        $this->cache = $cache;
        $this->minutes = $minutes;
    }

    /**
     * {@inheritdoc}
     *
     * @return bool
     */
    public function open($savePath, $sessionName): bool
    {
        return true;
    }

    /**
     * {@inheritdoc}
     *
     * @return bool
     */
    public function close(): bool
    {
        return true;
    }

    /**
     * {@inheritdoc}
     *
     * @return string
     */
    public function read($sessionId): string
    {
        return $this->cache->get($sessionId, '');
    }

    /**
     * {@inheritdoc}
     *
     * @return bool
     */
    public function write($sessionId, $data): bool
    {
        return $this->cache->put($sessionId, $data, $this->minutes * 60);
    }

    /**
     * {@inheritdoc}
     *
     * @return bool
     */
    public function destroy($sessionId): bool
    {
        return $this->cache->forget($sessionId);
    }

    /**
     * {@inheritdoc}
     *
     * @return int
     */
    public function gc($lifetime): int
    {
        return 0;
    }

    /**
     * Get the underlying cache repository.
     *
     * @return \Illuminate\Contracts\Cache\Repository
     */
    public function getCache()
    {
        return $this->cache;
    }
}