|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace yrc\redis; |
|
4
|
|
|
|
|
5
|
|
|
use yii\redis\ActiveRecord as YiiRedisActiveRecord; |
|
6
|
|
|
use Yii; |
|
7
|
|
|
use yii\helpers\Json; |
|
8
|
|
|
|
|
9
|
|
|
abstract class ActiveRecord extends YiiRedisActiveRecord |
|
10
|
|
|
{ |
|
11
|
|
|
public $isExpired = false; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* After find reconstitute the keypairs |
|
15
|
|
|
*/ |
|
16
|
|
|
public function afterFind() |
|
17
|
|
|
{ |
|
18
|
|
|
// If the object is expired, delete it |
|
19
|
|
|
if ($this->isExpired()) { |
|
20
|
|
|
$this->isExpired = true; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
if ($this->hasAttribute('data')) { |
|
24
|
|
|
$this->data = Json::decode($this->data); |
|
|
|
|
|
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
return parent::afterFind(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Return true if the token is expired |
|
32
|
|
|
* @return boolean |
|
33
|
|
|
*/ |
|
34
|
|
|
public function isExpired() |
|
35
|
|
|
{ |
|
36
|
|
|
if ($this->isExpired) { |
|
37
|
|
|
return true; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if (empty($this->expires_at)) { |
|
|
|
|
|
|
41
|
|
|
return false; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// Handle token expiration by actually deleting the token |
|
45
|
|
|
if ($this->expires_at < time()) { |
|
|
|
|
|
|
46
|
|
|
$this->delete(); |
|
47
|
|
|
|
|
48
|
|
|
return true; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @inheritdoc |
|
56
|
|
|
*/ |
|
57
|
|
|
public function afterSave($insert, $changedAttributes) |
|
58
|
|
|
{ |
|
59
|
|
|
parent::afterSave($insert, $changedAttributes); |
|
60
|
|
|
|
|
61
|
|
|
// If an expiration date is set in the model attributes, tell Redis to automatically handle it |
|
62
|
|
|
if (!empty($this->expires_at)) { |
|
|
|
|
|
|
63
|
|
|
$db = static::getDb(); |
|
64
|
|
|
$pk = []; |
|
65
|
|
|
foreach ($this->primaryKey() as $key) { |
|
66
|
|
|
$pk[$key] = $values[$key] = $this->getAttribute($key); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$pk = static::buildKey($pk); |
|
70
|
|
|
$key = static::keyPrefix() . ':a:' . $pk; |
|
71
|
|
|
$result = $db->executeCommand('EXPIREAT', [$key, $this->expires_at]); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @inheritdoc |
|
77
|
|
|
*/ |
|
78
|
|
|
public function beforeSave($insert) |
|
79
|
|
|
{ |
|
80
|
|
|
if (parent::beforeSave($insert)) { |
|
81
|
|
|
if ($this->hasAttribute('data')) { |
|
82
|
|
|
$this->data = Json::encode($this->data); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return true; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @inheritdoc |
|
93
|
|
|
*/ |
|
94
|
|
|
public function beforeDelete() |
|
95
|
|
|
{ |
|
96
|
|
|
if (parent::beforeDelete()) { |
|
97
|
|
|
// Log that the code was deleted |
|
98
|
|
|
Yii::info([ |
|
99
|
|
|
'message' => 'Deleting redis object', |
|
100
|
|
|
'code_id' => $this->id |
|
|
|
|
|
|
101
|
|
|
]); |
|
102
|
|
|
|
|
103
|
|
|
return true; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
} |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.