Add 添加条目 如果条目已经存在,则覆盖
(itemId uint64, expiresAt int64)
| 42 | // Add 添加条目 |
| 43 | // 如果条目已经存在,则覆盖 |
| 44 | func (this *List) Add(itemId uint64, expiresAt int64) { |
| 45 | this.mu.Lock() |
| 46 | defer this.mu.Unlock() |
| 47 | |
| 48 | if this.lastTimestamp == 0 || this.lastTimestamp > expiresAt { |
| 49 | this.lastTimestamp = expiresAt |
| 50 | } |
| 51 | |
| 52 | // 是否已经存在 |
| 53 | oldExpiresAt, ok := this.itemsMap[itemId] |
| 54 | if ok { |
| 55 | if oldExpiresAt == expiresAt { |
| 56 | return |
| 57 | } |
| 58 | delete(this.expireMap[oldExpiresAt], itemId) |
| 59 | if len(this.expireMap[oldExpiresAt]) == 0 { |
| 60 | delete(this.expireMap, oldExpiresAt) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | expireItemMap, ok := this.expireMap[expiresAt] |
| 65 | if ok { |
| 66 | expireItemMap[itemId] = zero.New() |
| 67 | } else { |
| 68 | this.expireMap[expiresAt] = ItemMap{ |
| 69 | itemId: zero.New(), |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | this.itemsMap[itemId] = expiresAt |
| 74 | } |
| 75 | |
| 76 | func (this *List) Remove(itemId uint64) { |
| 77 | this.mu.Lock() |