NewCache creates a new cache of files. cacheBlockSize is the fixed size of the cache block in bytes, and is used to evaluate the cost of each item in the cache.
(ctx context.Context, cacheBlockSize int64, path string)
| 150 | // NewCache creates a new cache of files. |
| 151 | // cacheBlockSize is the fixed size of the cache block in bytes, and is used to evaluate the cost of each item in the cache. |
| 152 | func NewCache(ctx context.Context, cacheBlockSize int64, path string) Cache { |
| 153 | log := zerolog.Ctx(ctx).With().Str("component", "cache").Logger() |
| 154 | |
| 155 | atomic.StoreInt32(&fdCnt, 0) |
| 156 | if err := os.MkdirAll(path, 0755); err != nil { |
| 157 | // This will call os.Exit(1) |
| 158 | log.Fatal().Err(err).Str("path", path).Msg("failed to initialize cache directory") |
| 159 | } |
| 160 | |
| 161 | cache := &fileCache{ |
| 162 | log: log, |
| 163 | path: path, |
| 164 | metadataCache: NewSyncMap(1e7), |
| 165 | } |
| 166 | |
| 167 | var err error |
| 168 | if cache.fileCache, err = ristretto.NewCache(&ristretto.Config{ |
| 169 | NumCounters: 1e7, |
| 170 | MaxCost: FilesCacheMaxCost, |
| 171 | BufferItems: 64, |
| 172 | |
| 173 | OnExit: func(val interface{}) { |
| 174 | item := val.(*item) |
| 175 | item.drop(log) |
| 176 | }, |
| 177 | |
| 178 | Cost: func(val interface{}) int64 { |
| 179 | return cacheBlockSize |
| 180 | }, |
| 181 | }); err != nil { |
| 182 | // This will call os.Exit(1) |
| 183 | log.Fatal().Err(err).Msg("failed to initialize file cache") |
| 184 | } |
| 185 | |
| 186 | return cache |
| 187 | } |