MCPcopy Create free account
hub / github.com/Azure/peerd / NewCache

Function NewCache

pkg/cache/filecache.go:152–187  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
152func 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}

Callers 5

NewFilesStoreFunction · 0.92
TestGetKeyFunction · 0.85
TestExistsFunction · 0.85
TestPutAndGetSizeFunction · 0.85
TestGetOrCreateFunction · 0.85

Calls 2

NewSyncMapFunction · 0.85
dropMethod · 0.80

Tested by 4

TestGetKeyFunction · 0.68
TestExistsFunction · 0.68
TestPutAndGetSizeFunction · 0.68
TestGetOrCreateFunction · 0.68