| 35 | } |
| 36 | |
| 37 | func NewOpenFileCache(maxCount int) (*OpenFileCache, error) { |
| 38 | if maxCount <= 0 { |
| 39 | maxCount = 16384 |
| 40 | } |
| 41 | |
| 42 | var cache = &OpenFileCache{ |
| 43 | maxCount: maxCount, |
| 44 | poolMap: map[string]*OpenFilePool{}, |
| 45 | poolList: linkedlist.NewList[*OpenFilePool](), |
| 46 | capacitySize: (int64(memutils.SystemMemoryGB()) << 30) / 16, |
| 47 | } |
| 48 | |
| 49 | watcher, err := fsnotify.NewWatcher() |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | cache.watcher = watcher |
| 54 | |
| 55 | goman.New(func() { |
| 56 | for event := range watcher.Events { |
| 57 | if runtime.GOOS == "linux" || event.Op&fsnotify.Chmod != fsnotify.Chmod { |
| 58 | cache.Close(event.Name) |
| 59 | } |
| 60 | } |
| 61 | }) |
| 62 | |
| 63 | return cache, nil |
| 64 | } |
| 65 | |
| 66 | func (this *OpenFileCache) Get(filename string) *OpenFile { |
| 67 | filename = filepath.Clean(filename) |