1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: hugh.li |
5
|
|
|
* Date: 2021/6/8 |
6
|
|
|
* Time: 11:02 下午. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace HughCube\Laravel\OTS\Cache; |
10
|
|
|
|
11
|
|
|
use Aliyun\OTS\Consts\ColumnTypeConst; |
12
|
|
|
use Carbon\Carbon; |
13
|
|
|
use HughCube\Laravel\OTS\Connection; |
14
|
|
|
use HughCube\Laravel\OTS\Ots; |
15
|
|
|
use Illuminate\Support\InteractsWithTime; |
16
|
|
|
|
17
|
|
|
trait Attribute |
18
|
|
|
{ |
19
|
|
|
use InteractsWithTime; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Connection |
23
|
|
|
*/ |
24
|
|
|
protected $ots; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $table; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var null|string |
33
|
|
|
*/ |
34
|
|
|
protected $prefix; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var null|string |
38
|
|
|
*/ |
39
|
|
|
protected $type; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var null|string |
43
|
|
|
*/ |
44
|
|
|
protected $owner; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string|null |
48
|
|
|
*/ |
49
|
|
|
public function getPrefix(): ?string |
50
|
|
|
{ |
51
|
|
|
return $this->prefix; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getTable(): string |
58
|
|
|
{ |
59
|
|
|
return $this->table; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return Connection |
64
|
|
|
*/ |
65
|
|
|
public function getOts(): Connection |
66
|
|
|
{ |
67
|
|
|
return $this->ots; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $key |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
protected function makePrimaryKey(string $key): array |
76
|
|
|
{ |
77
|
|
|
return [ |
78
|
|
|
['key', $key], |
79
|
|
|
['prefix', $this->getPrefix()], |
80
|
|
|
['type', ($this->type ?? 'cache')], |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param mixed $value |
86
|
|
|
* @param int|null $seconds |
87
|
|
|
* |
88
|
|
|
* @return array[] |
89
|
|
|
*/ |
90
|
|
|
protected function makeAttributeColumns($value, ?int $seconds = null): array |
91
|
|
|
{ |
92
|
|
|
$columns = []; |
93
|
|
|
|
94
|
|
|
$columns[] = ['created_at', Carbon::now()->toRfc3339String(true), ColumnTypeConst::CONST_STRING]; |
95
|
|
|
|
96
|
|
|
if (null !== $value) { |
97
|
|
|
$columns[] = ['value', $this->serialize($value), ColumnTypeConst::CONST_BINARY]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (null !== $seconds) { |
101
|
|
|
$columns[] = ['expiration', $this->availableAt($seconds), ColumnTypeConst::CONST_INTEGER]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (null !== $this->owner) { |
105
|
|
|
$columns[] = ['owner', $this->owner, ColumnTypeConst::CONST_STRING]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $columns; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param array $response |
113
|
|
|
* |
114
|
|
|
* @return mixed|null |
115
|
|
|
*/ |
116
|
|
|
protected function parseValueInOtsResponse(array $response) |
117
|
|
|
{ |
118
|
|
|
$columns = Ots::parseRow($response); |
119
|
|
|
|
120
|
|
|
if (!isset($columns['value'])) { |
121
|
|
|
return null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (isset($columns['expiration']) && $this->currentTime() >= $columns['expiration']) { |
125
|
|
|
return null; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this->unserialize($columns['value']); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param array $response |
133
|
|
|
* |
134
|
|
|
* @return string|null |
135
|
|
|
*/ |
136
|
|
|
protected function parseKeyInOtsResponse(array $response): ?string |
137
|
|
|
{ |
138
|
|
|
return Ots::parseRow($response)['key'] ?? null; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Serialize the value. |
143
|
|
|
* |
144
|
|
|
* @param mixed $value |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
protected function serialize($value): string |
149
|
|
|
{ |
150
|
|
|
if (is_numeric($value) && !in_array($value, [INF, -INF]) && !is_nan($value)) { |
151
|
|
|
return is_string($value) ? $value : strval($value); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return serialize($value); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Unserialize the value. |
159
|
|
|
* |
160
|
|
|
* @param mixed $value |
161
|
|
|
* |
162
|
|
|
* @return mixed |
163
|
|
|
*/ |
164
|
|
|
protected function unserialize($value) |
165
|
|
|
{ |
166
|
|
|
return is_numeric($value) ? $value : unserialize($value); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|