chengkun
2025-09-19 d48eff069585e2be1bd02b1299e1fe7581cb6dad
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
 
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare(strict_types=1);
 
namespace think\db;
 
use DateInterval;
use DateTime;
use DateTimeInterface;
use think\db\exception\InvalidArgumentException;
 
/**
 * CacheItem实现类.
 */
class CacheItem
{
    /**
     * 缓存Key.
     *
     * @var string
     */
    protected $key;
 
    /**
     * 缓存内容.
     *
     * @var mixed
     */
    protected $value;
 
    /**
     * 过期时间.
     *
     * @var int|DateTimeInterface
     */
    protected $expire;
 
    /**
     * 缓存tag.
     *
     * @var string
     */
    protected $tag;
 
    /**
     * 缓存是否命中.
     *
     * @var bool
     */
    protected $isHit = false;
 
    public function __construct(?string $key = null)
    {
        $this->key = $key;
    }
 
    /**
     * 为此缓存项设置「键」.
     *
     * @param string $key
     *
     * @return $this
     */
    public function setKey(string $key)
    {
        $this->key = $key;
 
        return $this;
    }
 
    /**
     * 返回当前缓存项的「键」.
     *
     * @return string
     */
    public function getKey()
    {
        return $this->key;
    }
 
    /**
     * 返回当前缓存项的有效期
     *
     * @return DateTimeInterface|int|null
     */
    public function getExpire()
    {
        if ($this->expire instanceof DateTimeInterface) {
            return $this->expire;
        }
 
        return $this->expire ? $this->expire - time() : null;
    }
 
    /**
     * 获取缓存Tag.
     *
     * @return string|array
     */
    public function getTag()
    {
        return $this->tag;
    }
 
    /**
     * 凭借此缓存项的「键」从缓存系统里面取出缓存项.
     *
     * @return mixed
     */
    public function get()
    {
        return $this->value;
    }
 
    /**
     * 确认缓存项的检查是否命中.
     *
     * @return bool
     */
    public function isHit(): bool
    {
        return $this->isHit;
    }
 
    /**
     * 为此缓存项设置「值」.
     *
     * @param mixed $value
     *
     * @return $this
     */
    public function set($value)
    {
        $this->value = $value;
        $this->isHit = true;
 
        return $this;
    }
 
    /**
     * 为此缓存项设置所属标签.
     *
     * @param string|array $tag
     *
     * @return $this
     */
    public function tag($tag = null)
    {
        $this->tag = $tag;
 
        return $this;
    }
 
    /**
     * 设置缓存项的有效期
     *
     * @param mixed $expire
     *
     * @return $this
     */
    public function expire($expire)
    {
        if (is_null($expire)) {
            $this->expire = null;
        } elseif (is_numeric($expire) || $expire instanceof DateInterval) {
            $this->expiresAfter($expire);
        } elseif ($expire instanceof DateTimeInterface) {
            $this->expire = $expire;
        } else {
            throw new InvalidArgumentException('not support datetime');
        }
 
        return $this;
    }
 
    /**
     * 设置缓存项的准确过期时间点.
     *
     * @param DateTimeInterface $expiration
     *
     * @return $this
     */
    public function expiresAt(DateTimeInterface $expiration)
    {
        $this->expire = $expiration;
 
        return $this;
    }
 
    /**
     * 设置缓存项的过期时间.
     *
     * @param int|DateInterval $timeInterval
     *
     * @throws InvalidArgumentException
     *
     * @return $this
     */
    public function expiresAfter($timeInterval)
    {
        if ($timeInterval instanceof DateInterval) {
            $this->expire = (int) DateTime::createFromFormat('U', (string) time())->add($timeInterval)->format('U');
        } elseif (is_numeric($timeInterval)) {
            $this->expire = $timeInterval + time();
        } else {
            throw new InvalidArgumentException('not support datetime');
        }
 
        return $this;
    }
}