Set 设置缓存
(key string, data []byte, ttl time.Duration)
| 46 | |
| 47 | // Set 设置缓存 |
| 48 | func (c *EnhancedTwoLevelCache) Set(key string, data []byte, ttl time.Duration) error { |
| 49 | // 获取当前时间作为最后修改时间 |
| 50 | now := time.Now() |
| 51 | |
| 52 | // 先设置内存缓存(这是快速操作,直接在当前goroutine中执行) |
| 53 | c.memory.SetWithTimestamp(key, data, ttl, now) |
| 54 | |
| 55 | // 异步设置磁盘缓存(这是IO操作,可能较慢) |
| 56 | go func(k string, d []byte, t time.Duration) { |
| 57 | // 使用独立的goroutine写入磁盘,避免阻塞调用者 |
| 58 | _ = c.disk.Set(k, d, t) |
| 59 | }(key, data, ttl) |
| 60 | |
| 61 | return nil |
| 62 | } |
| 63 | |
| 64 | // SetMemoryOnly 仅更新内存缓存 |
| 65 | func (c *EnhancedTwoLevelCache) SetMemoryOnly(key string, data []byte, ttl time.Duration) error { |
no test coverage detected