Get 获取缓存
(key string)
| 171 | |
| 172 | // Get 获取缓存 |
| 173 | func (c *DiskCache) Get(key string) ([]byte, bool, error) { |
| 174 | c.mutex.RLock() |
| 175 | meta, exists := c.metadata[key] |
| 176 | c.mutex.RUnlock() |
| 177 | |
| 178 | if !exists { |
| 179 | return nil, false, nil |
| 180 | } |
| 181 | |
| 182 | // 检查是否过期 |
| 183 | if time.Now().After(meta.Expiry) { |
| 184 | c.Delete(key) |
| 185 | return nil, false, nil |
| 186 | } |
| 187 | |
| 188 | // 获取文件路径 |
| 189 | filePath := filepath.Join(c.path, c.getFilename(key)) |
| 190 | |
| 191 | // 读取文件 |
| 192 | data, err := ioutil.ReadFile(filePath) |
| 193 | if err != nil { |
| 194 | // 如果文件不存在,删除元数据 |
| 195 | if os.IsNotExist(err) { |
| 196 | c.Delete(key) |
| 197 | } |
| 198 | return nil, false, err |
| 199 | } |
| 200 | |
| 201 | // 更新最后使用时间 |
| 202 | c.mutex.Lock() |
| 203 | meta.LastUsed = time.Now() |
| 204 | c.saveMetadata(key, meta) |
| 205 | c.mutex.Unlock() |
| 206 | |
| 207 | return data, true, nil |
| 208 | } |
| 209 | |
| 210 | // Delete 删除缓存 |
| 211 | func (c *DiskCache) Delete(key string) error { |
no test coverage detected