| 58 | } |
| 59 | |
| 60 | func (this *MemoryList) Add(hash string, item *Item) error { |
| 61 | this.locker.Lock() |
| 62 | |
| 63 | prefix := this.prefix(hash) |
| 64 | itemMap, ok := this.itemMaps[prefix] |
| 65 | if !ok { |
| 66 | itemMap = map[string]*Item{} |
| 67 | this.itemMaps[prefix] = itemMap |
| 68 | } |
| 69 | |
| 70 | // 先删除,为了可以正确触发统计 |
| 71 | oldItem, ok := itemMap[hash] |
| 72 | if ok { |
| 73 | // 回调 |
| 74 | if this.onRemove != nil { |
| 75 | this.onRemove(oldItem) |
| 76 | } |
| 77 | } else { |
| 78 | atomic.AddInt64(&this.count, 1) |
| 79 | } |
| 80 | |
| 81 | // 添加 |
| 82 | if this.onAdd != nil { |
| 83 | this.onAdd(item) |
| 84 | } |
| 85 | |
| 86 | itemMap[hash] = item |
| 87 | |
| 88 | this.locker.Unlock() |
| 89 | return nil |
| 90 | } |
| 91 | |
| 92 | func (this *MemoryList) Exist(hash string) (bool, int64, error) { |
| 93 | this.locker.RLock() |