CacheSession
API reference › yii › web › CacheSession
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, 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',]Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$allFlashes read-only | array | Flash messages (key => message or key => [message1, message2]). | Session |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$cache | yii\| | The cache object or the application component ID of the cache object. Default: 'cache'. | CacheSession |
$cacheLimiter | string | Current cache limiter. | Session |
$cookieParams | array | The session cookie parameters. | Session |
$count read-only | int | The number of session variables. | Session |
$flash write-only | string | The key identifying the flash message. | Session |
$flashParam | string | The name of the session variable that stores the flash message data. Default: '__flash'. | Session |
$gCProbability | float | The probability (percentage) that the GC (garbage collection) process is started on every session initialization. | Session |
$handler | SessionHandlerInterface| | An object implementing the SessionHandlerInterface or a configuration array. Default: null. | Session |
$hasSessionId | bool | Whether the current request has sent the session ID. | Session |
$id | string | The current session ID. | Session |
$isActive read-only | bool | Whether the session has started. | Session |
$iterator read-only | SessionIterator | An iterator for traversing the session variables. | Session |
$name | string | The current session name. | Session |
$savePath | string | The current session save path, defaults to ‘/tmp’. | Session |
$timeout | int | The number of seconds after which data will be seen as ‘garbage’ and cleaned up. | Session |
$useCookies | bool| | The value indicating whether cookies should be used to store session IDs. | Session |
$useCustomStorage read-only | bool | Whether to use custom storage. | CacheSession |
$useStrictMode | bool | Whether strict mode is enabled or not. | Session |
$useTransparentSessionID | bool | Whether transparent sid support is enabled or not, defaults to false. | Session |
$_forceRegenerateId protected | string| | Holds the session id in case useStrictMode is enabled and the session id needs to be regenerated Default: null. | Session |
$_originalSessionModule protected static | string| | 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. Default: null. | Session |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
addFlash() | Adds a flash message. | Session |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
close() | Ends the current session and store session data. | Session |
count() | Returns the number of items in the session. | Session |
destroy() | Frees all session variables and destroys all data registered to a session. | Session |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
get() | Returns the session variable value with the session variable name. | Session |
getAllFlashes() | Returns all flash messages. | Session |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getCacheLimiter() | Returns current cache limiter | Session |
getCookieParams() | Session | |
getCount() | Returns the number of items in the session. | Session |
getFlash() | Returns a flash message. | Session |
getGCProbability() | Session | |
getHasSessionId() | Returns a value indicating whether the current request has sent the session ID. | Session |
getId() | Gets the session ID. | Session |
getIsActive() | Session | |
getIterator() | Returns an iterator for traversing the session variables. | Session |
getName() | Gets the name of the current session. | Session |
getSavePath() | Gets the current session save path. | Session |
getTimeout() | Session | |
getUseCookies() | Returns the value indicating whether cookies should be used to store session IDs. | Session |
getUseCustomStorage() | Returns a value indicating whether to use custom session storage. | CacheSession |
getUseStrictMode() | Session | |
getUseTransparentSessionID() | Session | |
has() | Session | |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasFlash() | Returns a value indicating whether there are flash messages associated with the specified key. | Session |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the application component. | CacheSession |
off() | Detaches an existing event handler from this component. | yii\ |
offsetExists() | This method is required by the interface ArrayAccess. | Session |
offsetGet() | This method is required by the interface ArrayAccess. | Session |
offsetSet() | This method is required by the interface ArrayAccess. | Session |
offsetUnset() | This method is required by the interface ArrayAccess. | Session |
on() | Attaches an event handler to an event. | yii\ |
open() | Starts the session. | Session |
regenerateID() | Updates the current session ID with a newly generated one. | Session |
remove() | Removes a session variable. | Session |
removeAll() | Removes all session variables. | Session |
removeAllFlashes() | Removes all flash messages. | Session |
removeFlash() | Removes a flash message. | Session |
set() | Adds a session variable. | Session |
setCacheLimiter() | Set cache limiter | Session |
setCookieParams() | Sets the session cookie parameters. | Session |
setFlash() | Sets a flash message. | Session |
setGCProbability() | Session | |
setHasSessionId() | Sets the value indicating whether the current request has sent the session ID. | Session |
setId() | Sets the session ID. | Session |
setName() | Sets the name for the current session. | Session |
setSavePath() | Sets the current session save path. | Session |
setTimeout() | Session | |
setUseCookies() | Sets the value indicating whether cookies should be used to store session IDs. | Session |
setUseStrictMode() | Session | |
setUseTransparentSessionID() | Session | |
trigger() | Triggers an event. | yii\ |
calculateKey() protected | Generates a unique key used for storing session data in cache. | CacheSession |
freeze() protected | If session is started it’s not possible to edit session ini settings. In PHP7.2+ it throws exception. | Session |
registerSessionHandler() protected | Registers session handler. | Session |
unfreeze() protected | Starts session and restores data from temporary variable | Session |
updateFlashCounters() protected | Updates the counters for flash messages and removes outdated flash messages. | Session |
Property details
Section titled “Property details”$cache
Section titled “$cache ”public yii\caching\CacheInterface|array|string $cache = 'cache'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
Section titled “Method details”getUseCustomStorage()
Section titled “getUseCustomStorage() ”public function getUseCustomStorage(): boolReturns a value indicating whether to use custom session storage.
This method overrides the parent implementation and always returns true.
Returns
bool: Whether to use custom storage.
init()
Section titled “init() ”public function init(): mixedInitializes the application component.
Returns
mixed
calculateKey()
Section titled “calculateKey() ”protected function calculateKey(string $id): mixedGenerates a unique key used for storing session data in cache.
Parameters
| Parameter | Type | Description |
|---|---|---|
$id | string | Session variable name |
Returns
mixed: A safe cache key associated with the session variable name