newFilesSystemCache returns new cache for the given cfg.
(cfg config.Cache, graceTime time.Duration)
| 35 | |
| 36 | // newFilesSystemCache returns new cache for the given cfg. |
| 37 | func newFilesSystemCache(cfg config.Cache, graceTime time.Duration) (*fileSystemCache, error) { |
| 38 | if len(cfg.FileSystem.Dir) == 0 { |
| 39 | return nil, fmt.Errorf("`dir` cannot be empty") |
| 40 | } |
| 41 | if cfg.FileSystem.MaxSize <= 0 { |
| 42 | return nil, fmt.Errorf("`max_size` must be positive") |
| 43 | } |
| 44 | if cfg.Expire <= 0 { |
| 45 | return nil, fmt.Errorf("`expire` must be positive") |
| 46 | } |
| 47 | |
| 48 | c := &fileSystemCache{ |
| 49 | name: cfg.Name, |
| 50 | |
| 51 | dir: cfg.FileSystem.Dir, |
| 52 | maxSize: uint64(cfg.FileSystem.MaxSize), |
| 53 | expire: time.Duration(cfg.Expire), |
| 54 | grace: graceTime, |
| 55 | stopCh: make(chan struct{}), |
| 56 | } |
| 57 | |
| 58 | if err := os.MkdirAll(c.dir, 0700); err != nil { |
| 59 | return nil, fmt.Errorf("cannot create %q: %w", c.dir, err) |
| 60 | } |
| 61 | |
| 62 | c.wg.Add(1) |
| 63 | go func() { |
| 64 | log.Debugf("cache %q: cleaner start", c.Name()) |
| 65 | c.cleaner() |
| 66 | log.Debugf("cache %q: cleaner stop", c.Name()) |
| 67 | c.wg.Done() |
| 68 | }() |
| 69 | |
| 70 | return c, nil |
| 71 | } |
| 72 | |
| 73 | func (f *fileSystemCache) Name() string { |
| 74 | return f.name |