| 87 | } |
| 88 | |
| 89 | func (this *OpenFileCache) Put(filename string, file *OpenFile) { |
| 90 | filename = filepath.Clean(filename) |
| 91 | |
| 92 | if file.size > maxOpenFileSize { |
| 93 | return |
| 94 | } |
| 95 | |
| 96 | this.locker.Lock() |
| 97 | defer this.locker.Unlock() |
| 98 | |
| 99 | // 如果超过当前容量,则关闭最早的 |
| 100 | if this.count >= this.maxCount || this.usedSize+file.size >= this.capacitySize { |
| 101 | this.consumeHead() |
| 102 | return |
| 103 | } |
| 104 | |
| 105 | pool, ok := this.poolMap[filename] |
| 106 | var success bool |
| 107 | if ok { |
| 108 | success = pool.Put(file) |
| 109 | } else { |
| 110 | _ = this.watcher.Add(filename) |
| 111 | pool = NewOpenFilePool(filename) |
| 112 | pool.version = file.version |
| 113 | this.poolMap[filename] = pool |
| 114 | success = pool.Put(file) |
| 115 | } |
| 116 | this.poolList.Push(pool.linkItem) |
| 117 | |
| 118 | // 检查长度 |
| 119 | if success { |
| 120 | this.count++ |
| 121 | this.usedSize += file.size |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func (this *OpenFileCache) Close(filename string) { |
| 126 | filename = filepath.Clean(filename) |