14
14
use Magento \Framework \Filesystem ;
15
15
use Magento \Framework \App \Filesystem \DirectoryList ;
16
16
17
- class Redis extends \ Cm \ RedisSession \Handler
17
+ class Redis implements \SessionHandlerInterface
18
18
{
19
+ /**
20
+ * @var ConfigInterface
21
+ */
22
+ private $ config ;
23
+
24
+ /**
25
+ * @var LoggerInterface
26
+ */
27
+ private $ logger ;
28
+
19
29
/**
20
30
* @var Filesystem
21
31
*/
22
32
private $ filesystem ;
23
33
34
+ /**
35
+ * @var \Cm\RedisSession\Handler[]
36
+ */
37
+ private $ connection ;
38
+
24
39
/**
25
40
* @param ConfigInterface $config
26
41
* @param LoggerInterface $logger
@@ -29,23 +44,116 @@ class Redis extends \Cm\RedisSession\Handler
29
44
*/
30
45
public function __construct (ConfigInterface $ config , LoggerInterface $ logger , Filesystem $ filesystem )
31
46
{
47
+ $ this ->config = $ config ;
48
+ $ this ->logger = $ logger ;
32
49
$ this ->filesystem = $ filesystem ;
33
- try {
34
- parent ::__construct ($ config , $ logger );
35
- } catch (ConnectionFailedException $ e ) {
36
- throw new SessionException (new Phrase ($ e ->getMessage ()));
50
+ }
51
+
52
+ /**
53
+ * Get connection
54
+ *
55
+ * @return \Cm\RedisSession\Handler
56
+ * @throws SessionException
57
+ */
58
+ private function getConnection ()
59
+ {
60
+ $ pid = getmypid ();
61
+ if (!isset ($ this ->connection [$ pid ])) {
62
+ try {
63
+ $ this ->connection [$ pid ] = new \Cm \RedisSession \Handler ($ this ->config , $ this ->logger );
64
+ } catch (ConnectionFailedException $ e ) {
65
+ throw new SessionException (new Phrase ($ e ->getMessage ()));
66
+ }
37
67
}
68
+ return $ this ->connection [$ pid ];
38
69
}
39
70
40
71
/**
41
- * {@inheritdoc}
72
+ * Open session
73
+ *
74
+ * @param string $savePath ignored
75
+ * @param string $sessionName ignored
76
+ * @return bool
77
+ * @throws SessionException
78
+ */
79
+ public function open ($ savePath , $ sessionName )
80
+ {
81
+ return $ this ->getConnection ()->open ($ savePath , $ sessionName );
82
+ }
83
+
84
+ /**
85
+ * Fetch session data
86
+ *
87
+ * @param string $sessionId
88
+ * @return string
89
+ * @throws ConcurrentConnectionsExceededException
90
+ * @throws SessionException
42
91
*/
43
92
public function read ($ sessionId )
44
93
{
45
94
try {
46
- return parent :: read ($ sessionId );
95
+ return $ this -> getConnection ()-> read ($ sessionId );
47
96
} catch (ConcurrentConnectionsExceededException $ e ) {
48
97
require $ this ->filesystem ->getDirectoryRead (DirectoryList::PUB )->getAbsolutePath ('errors/503.php ' );
49
98
}
50
99
}
100
+
101
+ /**
102
+ * Update session
103
+ *
104
+ * @param string $sessionId
105
+ * @param string $sessionData
106
+ * @return boolean
107
+ * @throws SessionException
108
+ */
109
+ public function write ($ sessionId , $ sessionData )
110
+ {
111
+ return $ this ->getConnection ()->write ($ sessionId , $ sessionData );
112
+ }
113
+
114
+ /**
115
+ * Destroy session
116
+ *
117
+ * @param string $sessionId
118
+ * @return boolean
119
+ * @throws SessionException
120
+ */
121
+ public function destroy ($ sessionId )
122
+ {
123
+ return $ this ->getConnection ()->destroy ($ sessionId );
124
+ }
125
+
126
+ /**
127
+ * Overridden to prevent calling getLifeTime at shutdown
128
+ *
129
+ * @return bool
130
+ * @throws SessionException
131
+ */
132
+ public function close ()
133
+ {
134
+ return $ this ->getConnection ()->close ();
135
+ }
136
+
137
+ /**
138
+ * Garbage collection
139
+ *
140
+ * @param int $maxLifeTime ignored
141
+ * @return boolean
142
+ * @throws SessionException
143
+ */
144
+ public function gc ($ maxLifeTime )
145
+ {
146
+ return $ this ->getConnection ()->gc ($ maxLifeTime );
147
+ }
148
+
149
+ /**
150
+ * Get the number of failed lock attempts
151
+ *
152
+ * @return int
153
+ * @throws SessionException
154
+ */
155
+ public function getFailedLockAttempts ()
156
+ {
157
+ return $ this ->getConnection ()->getFailedLockAttempts ();
158
+ }
51
159
}
0 commit comments