Completed
Push — master ( 30b7fc...ecefc2 )
by Dmitry
56:02 queued 52:35
created

CacheSession   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 84
ccs 18
cts 18
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A readSession() 0 6 2
A writeSession() 0 4 1
A getUseCustomStorage() 0 4 1
A destroySession() 0 9 2
A calculateKey() 0 4 1
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\web;
9
10
use Yii;
11
use yii\caching\Cache;
12
use yii\di\Instance;
13
14
/**
15
 * CacheSession implements a session component using cache as storage medium.
16
 *
17
 * The cache being used can be any cache application component.
18
 * The ID of the cache application component is specified via [[cache]], which defaults to 'cache'.
19
 *
20
 * Beware, by definition cache storage are volatile, which means the data stored on them
21
 * may be swapped out and get lost. Therefore, you must make sure the cache used by this component
22
 * is NOT volatile. If you want to use database as storage medium, [[DbSession]] is a better choice.
23
 *
24
 * The following example shows how you can configure the application to use CacheSession:
25
 * Add the following to your application config under `components`:
26
 *
27
 * ```php
28
 * 'session' => [
29
 *     'class' => 'yii\web\CacheSession',
30
 *     // 'cache' => 'mycache',
31
 * ]
32
 * ```
33
 *
34
 * @property bool $useCustomStorage Whether to use custom storage. This property is read-only.
35
 *
36
 * @author Qiang Xue <[email protected]>
37
 * @since 2.0
38
 */
39
class CacheSession extends Session
40
{
41
    /**
42
     * @var Cache|array|string the cache object or the application component ID of the cache object.
43
     * The session data will be stored using this cache object.
44
     *
45
     * After the CacheSession object is created, if you want to change this property,
46
     * you should only assign it with a cache object.
47
     *
48
     * Starting from version 2.0.2, this can also be a configuration array for creating the object.
49
     */
50
    public $cache = 'cache';
51
52
53
    /**
54
     * Initializes the application component.
55
     */
56 3
    public function init()
57
    {
58 3
        parent::init();
59 3
        $this->cache = Instance::ensure($this->cache, Cache::className());
60 2
    }
61
62
    /**
63
     * Returns a value indicating whether to use custom session storage.
64
     * This method overrides the parent implementation and always returns true.
65
     * @return bool whether to use custom storage.
66
     */
67 1
    public function getUseCustomStorage()
68
    {
69 1
        return true;
70
    }
71
72
    /**
73
     * Session read handler.
74
     * @internal Do not call this method directly.
75
     * @param string $id session ID
76
     * @return string the session data
77
     */
78 8
    public function readSession($id)
79
    {
80 8
        $data = $this->cache->get($this->calculateKey($id));
81
82 8
        return $data === false ? '' : $data;
83
    }
84
85
    /**
86
     * Session write handler.
87
     * @internal Do not call this method directly.
88
     * @param string $id session ID
89
     * @param string $data session data
90
     * @return bool whether session write is successful
91
     */
92 8
    public function writeSession($id, $data)
93
    {
94 8
        return $this->cache->set($this->calculateKey($id), $data, $this->getTimeout());
95
    }
96
97
    /**
98
     * Session destroy handler.
99
     * @internal Do not call this method directly.
100
     * @param string $id session ID
101
     * @return bool whether session is destroyed successfully
102
     */
103 3
    public function destroySession($id)
104
    {
105 3
        $cacheId = $this->calculateKey($id);
106 3
        if ($this->cache->exists($cacheId) === false) {
107 1
            return true;
108
        }
109
110 2
        return $this->cache->delete($cacheId);
111
    }
112
113
    /**
114
     * Generates a unique key used for storing session data in cache.
115
     * @param string $id session variable name
116
     * @return mixed a safe cache key associated with the session variable name
117
     */
118 9
    protected function calculateKey($id)
119
    {
120 9
        return [__CLASS__, $id];
121
    }
122
}
123