Init 初始化
()
| 89 | |
| 90 | // Init 初始化 |
| 91 | func (this *MemoryStorage) Init() error { |
| 92 | _ = this.list.Init() |
| 93 | |
| 94 | this.list.OnAdd(func(item *Item) { |
| 95 | atomic.AddInt64(&this.usedSize, item.TotalSize()) |
| 96 | }) |
| 97 | this.list.OnRemove(func(item *Item) { |
| 98 | atomic.AddInt64(&this.usedSize, -item.TotalSize()) |
| 99 | }) |
| 100 | |
| 101 | this.initPurgeTicker() |
| 102 | |
| 103 | // 启动定时Flush memory to disk任务 |
| 104 | if this.parentStorage != nil { |
| 105 | var threads = 2 |
| 106 | var numCPU = runtime.NumCPU() |
| 107 | if fsutils.DiskIsExtremelyFast() { |
| 108 | if numCPU >= 8 { |
| 109 | threads = 8 |
| 110 | } else { |
| 111 | threads = 4 |
| 112 | } |
| 113 | } else if fsutils.DiskIsFast() { |
| 114 | if numCPU >= 4 { |
| 115 | threads = 4 |
| 116 | } |
| 117 | } |
| 118 | for i := 0; i < threads; i++ { |
| 119 | goman.New(func() { |
| 120 | this.startFlush() |
| 121 | }) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return nil |
| 126 | } |
| 127 | |
| 128 | // OpenReader 读取缓存 |
| 129 | func (this *MemoryStorage) OpenReader(key string, useStale bool, isPartial bool) (Reader, error) { |