-
Notifications
You must be signed in to change notification settings - Fork 56
/
ARedisSession.php
executable file
·117 lines (109 loc) · 3 KB
/
ARedisSession.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* A simple redis based session handler
* @author Charles Pick
* @package packages.redis
*/
class ARedisSession extends CHttpSession
{
/**
* The prefix to use when storing and retrieving sessions
* @var string
*/
public $keyPrefix = "Yii.ARedisSession.";
/**
* The suffix to use when storing and retrieving sessions
* @var string
*/
public $keySuffix = '';
/**
* Holds the redis connection
* @var ARedisConnection
*/
protected $_connection;
/**
* Initializes the application component.
* This method overrides the parent implementation by checking if redis is available.
*/
public function init()
{
$this->getConnection();
parent::init();
}
/**
* Returns a value indicating whether to use custom session storage.
* @return boolean whether to use custom storage.
*/
public function getUseCustomStorage()
{
return true;
}
/**
* Session read handler.
* Do not call this method directly.
* @param string $id session ID
* @return string the session data
*/
public function readSession($id)
{
$data=$this->_connection->getClient()->get($this->calculateKey($id));
return $data===false?'':$data;
}
/**
* Session write handler.
* Do not call this method directly.
* @param string $id session ID
* @param string $data session data
* @return boolean whether session write is successful
*/
public function writeSession($id, $data)
{
$key = $this->calculateKey($id);
$this->_connection->getClient()->set($key,$data);
$this->_connection->getClient()->expire($key,$this->getTimeout());
return true;
}
/**
* Session destroy handler.
* Do not call this method directly.
* @param string $id session ID
* @return boolean whether session is destroyed successfully
*/
public function destroySession($id)
{
$this->_connection->getClient()->delete($this->calculateKey($id));
return true;
}
/**
* Sets the redis connection to use for this session handler
* @param ARedisConnection|string $connection the redis connection, if a string is provided, it is presumed to be a the name of an applciation component
*/
public function setConnection($connection)
{
if (is_string($connection))
$connection = Yii::app()->{$connection};
$this->_connection = $connection;
}
/**
* Gets the redis connection to use for this session handler
* @return ARedisConnection
*/
public function getConnection()
{
if ($this->_connection === null) {
if (!isset(Yii::app()->redis))
throw new CException(get_class($this)." expects a 'redis' application component");
$this->_connection = Yii::app()->redis;
}
return $this->_connection;
}
/**
* Generates a unique key used for storing session data in cache.
* @param string $id session variable name
* @return string a safe cache key associated with the session variable name
*/
protected function calculateKey($id)
{
return $this->keyPrefix.$id.$this->keySuffix;
}
}