| 64 | } |
| 65 | |
| 66 | func (this *OpenFileCache) Get(filename string) *OpenFile { |
| 67 | filename = filepath.Clean(filename) |
| 68 | |
| 69 | this.locker.RLock() |
| 70 | pool, ok := this.poolMap[filename] |
| 71 | this.locker.RUnlock() |
| 72 | if ok { |
| 73 | file, consumed, consumedSize := pool.Get() |
| 74 | if consumed { |
| 75 | this.locker.Lock() |
| 76 | this.count-- |
| 77 | this.usedSize -= consumedSize |
| 78 | |
| 79 | // pool如果为空,也不需要从列表中删除,避免put时需要重新创建 |
| 80 | |
| 81 | this.locker.Unlock() |
| 82 | } |
| 83 | |
| 84 | return file |
| 85 | } |
| 86 | return nil |
| 87 | } |
| 88 | |
| 89 | func (this *OpenFileCache) Put(filename string, file *OpenFile) { |
| 90 | filename = filepath.Clean(filename) |