Class yii\web\CacheSession

Inheritanceyii\web\CacheSession » yii\web\Session » yii\base\Component » yii\base\BaseObject
ImplementsArrayAccess, Countable, IteratorAggregate, yii\base\Configurable
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/web/CacheSession.php

CacheSession implements a session component using cache as storage medium.

The cache being used can be any cache application component. The ID of the cache application component is specified via $cache, which defaults to 'cache'.

Beware, by definition cache storage are volatile, which means the data stored on them may be swapped out and get lost. Therefore, you must make sure the cache used by this component is NOT volatile. If you want to use database as storage medium, yii\web\DbSession is a better choice.

The following example shows how you can configure the application to use CacheSession: Add the following to your application config under components:

'session' => [
    'class' => 'yii\web\CacheSession',
    // 'cache' => 'mycache',
]

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$cache yii\caching\CacheInterface|array|string The cache object or the application component ID of the cache object. yii\web\CacheSession
$flashParam string The name of the session variable that stores the flash message data. yii\web\Session
$handler SessionHandlerInterface|array An object implementing the SessionHandlerInterface or a configuration array. yii\web\Session

Protected Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$_forceRegenerateId string|null Holds the session id in case useStrictMode is enabled and the session id needs to be regenerated yii\web\Session
$_originalSessionModule string|null Holds the original session module (before a custom handler is registered) so that it can be restored when a Session component without custom handler is used after one that has. yii\web\Session

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of a component property. yii\base\Component
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Component
__set() Sets the value of a component property. yii\base\Component
__unset() Sets a component property to be null. yii\base\Component
addFlash() Adds a flash message. yii\web\Session
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\BaseObject
close() Ends the current session and store session data. yii\web\Session
closeSession() Session close handler. yii\web\Session
count() Returns the number of items in the session. yii\web\Session
destroy() Frees all session variables and destroys all data registered to a session. yii\web\Session
destroySession() Session destroy handler. yii\web\CacheSession
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
gcSession() Session GC (garbage collection) handler. yii\web\Session
get() Returns the session variable value with the session variable name. yii\web\Session
getAllFlashes() Returns all flash messages. yii\web\Session
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getCacheLimiter() Returns current cache limiter yii\web\Session
getCookieParams() yii\web\Session
getCount() Returns the number of items in the session. yii\web\Session
getFlash() Returns a flash message. yii\web\Session
getGCProbability() yii\web\Session
getHasSessionId() Returns a value indicating whether the current request has sent the session ID. yii\web\Session
getId() Gets the session ID. yii\web\Session
getIsActive() yii\web\Session
getIterator() Returns an iterator for traversing the session variables. yii\web\Session
getName() Gets the name of the current session. yii\web\Session
getSavePath() Gets the current session save path. yii\web\Session
getTimeout() yii\web\Session
getUseCookies() Returns the value indicating whether cookies should be used to store session IDs. yii\web\Session
getUseCustomStorage() Returns a value indicating whether to use custom session storage. yii\web\CacheSession
getUseStrictMode() yii\web\Session
getUseTransparentSessionID() yii\web\Session
has() yii\web\Session
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasFlash() Returns a value indicating whether there are flash messages associated with the specified key. yii\web\Session
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
init() Initializes the application component. yii\web\CacheSession
off() Detaches an existing event handler from this component. yii\base\Component
offsetExists() This method is required by the interface ArrayAccess. yii\web\Session
offsetGet() This method is required by the interface ArrayAccess. yii\web\Session
offsetSet() This method is required by the interface ArrayAccess. yii\web\Session
offsetUnset() This method is required by the interface ArrayAccess. yii\web\Session
on() Attaches an event handler to an event. yii\base\Component
open() Starts the session. yii\web\Session
openSession() Session open handler. yii\web\CacheSession
readSession() Session read handler. yii\web\CacheSession
regenerateID() Updates the current session ID with a newly generated one. yii\web\Session
remove() Removes a session variable. yii\web\Session
removeAll() Removes all session variables. yii\web\Session
removeAllFlashes() Removes all flash messages. yii\web\Session
removeFlash() Removes a flash message. yii\web\Session
set() Adds a session variable. yii\web\Session
setCacheLimiter() Set cache limiter yii\web\Session
setCookieParams() Sets the session cookie parameters. yii\web\Session
setFlash() Sets a flash message. yii\web\Session
setGCProbability() yii\web\Session
setHasSessionId() Sets the value indicating whether the current request has sent the session ID. yii\web\Session
setId() Sets the session ID. yii\web\Session
setName() Sets the name for the current session. yii\web\Session
setSavePath() Sets the current session save path. yii\web\Session
setTimeout() yii\web\Session
setUseCookies() Sets the value indicating whether cookies should be used to store session IDs. yii\web\Session
setUseStrictMode() yii\web\Session
setUseTransparentSessionID() yii\web\Session
trigger() Triggers an event. yii\base\Component
writeSession() Session write handler. yii\web\CacheSession

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
calculateKey() Generates a unique key used for storing session data in cache. yii\web\CacheSession
freeze() If session is started it's not possible to edit session ini settings. In PHP7.2+ it throws exception. yii\web\Session
registerSessionHandler() Registers session handler. yii\web\Session
unfreeze() Starts session and restores data from temporary variable yii\web\Session
updateFlashCounters() Updates the counters for flash messages and removes outdated flash messages. yii\web\Session

Property Details

$cache public property

The cache object or the application component ID of the cache object. The session data will be stored using this cache object.

After the CacheSession object is created, if you want to change this property, you should only assign it with a cache object.

Starting from version 2.0.2, this can also be a configuration array for creating the object.

Method Details

calculateKey() protected method

Generates a unique key used for storing session data in cache.

protected mixed calculateKey ( $id )
$id string

Session variable name

return mixed

A safe cache key associated with the session variable name

destroySession() public method

Session destroy handler.

public boolean destroySession ( $id )
$id string

Session ID

return boolean

Whether session is destroyed successfully

getUseCustomStorage() public method

Returns a value indicating whether to use custom session storage.

This method overrides the parent implementation and always returns true.

public boolean getUseCustomStorage ( )
return boolean

Whether to use custom storage.

init() public method

Initializes the application component.

public void init ( )
openSession() public method

Session open handler.

public boolean openSession ( $savePath, $sessionName )
$savePath string

Session save path

$sessionName string

Session name

return boolean

Whether session is opened successfully

readSession() public method

Session read handler.

public string readSession ( $id )
$id string

Session ID

return string

The session data

writeSession() public method

Session write handler.

public boolean writeSession ( $id, $data )
$id string

Session ID

$data string

Session data

return boolean

Whether session write is successful