@@ -228,20 +228,7 @@ func (dc *directoryCache) Get(key string, opts ...Option) (Reader, error) {
228228 opt = o (opt )
229229 }
230230
231- // 1. Try to get from hardlink first
232- if dc .hlManager != nil {
233- if linkPath , exists := dc .hlManager .GetLink (key ); exists {
234- if r , err := os .Open (linkPath ); err == nil {
235- return & reader {
236- ReaderAt : r ,
237- closeFunc : r .Close ,
238- }, nil
239- }
240- // If failed to open, continue with normal flow
241- }
242- }
243-
244- // 2. Try to get from memory cache
231+ // Try to get from memory cache
245232 if ! dc .direct && ! opt .direct {
246233 if b , done , ok := dc .cache .Get (key ); ok {
247234 return & reader {
@@ -265,15 +252,23 @@ func (dc *directoryCache) Get(key string, opts ...Option) (Reader, error) {
265252 }
266253 }
267254
268- // 3. Open the cache file and read the target region
255+ // Try to get from hardlink first
256+ filepath := BuildCachePath (dc .directory , key )
257+ if dc .hlManager != nil {
258+ if linkPath , exists := dc .hlManager .GetLink (key ); exists {
259+ filepath = linkPath
260+ }
261+ }
262+
263+ // Open the cache file and read the target region
269264 // TODO: If the target cache is write-in-progress, should we wait for the completion
270265 // or simply report the cache miss?
271- file , err := os .Open (dc . cachePath ( key ) )
266+ file , err := os .Open (filepath )
272267 if err != nil {
273268 return nil , fmt .Errorf ("failed to open blob file for %q: %w" , key , err )
274269 }
275270
276- // 4. If in direct mode, don't cache file descriptor
271+ // If in direct mode, don't cache file descriptor
277272 // This option is useful for preventing memory cache from being polluted by data
278273 // that won't be accessed immediately.
279274 if dc .direct || opt .direct {
@@ -288,7 +283,7 @@ func (dc *directoryCache) Get(key string, opts ...Option) (Reader, error) {
288283 }, nil
289284 }
290285
291- // 5. Cache file descriptor
286+ // Cache file descriptor
292287 // TODO: should we cache the entire file data on memory?
293288 // but making I/O (possibly huge) on every fetching
294289 // might be costly.
@@ -330,7 +325,7 @@ func (dc *directoryCache) Add(key string, opts ...Option) (Writer, error) {
330325 }
331326
332327 // Create temporary file
333- w , err := dc .wipFile ( key )
328+ w , err := WipFile ( dc .wipDirectory , key )
334329 if err != nil {
335330 return nil , err
336331 }
@@ -344,7 +339,7 @@ func (dc *directoryCache) Add(key string, opts ...Option) (Writer, error) {
344339 }
345340
346341 // Commit file
347- targetPath := dc .cachePath ( key )
342+ targetPath := BuildCachePath ( dc .directory , key )
348343 if err := os .MkdirAll (filepath .Dir (targetPath ), 0700 ); err != nil {
349344 return fmt .Errorf ("failed to create cache directory: %w" , err )
350345 }
@@ -400,14 +395,6 @@ func (dc *directoryCache) isClosed() bool {
400395 return closed
401396}
402397
403- func (dc * directoryCache ) cachePath (key string ) string {
404- return filepath .Join (dc .directory , key [:2 ], key )
405- }
406-
407- func (dc * directoryCache ) wipFile (key string ) (* os.File , error ) {
408- return os .CreateTemp (dc .wipDirectory , key + "-*" )
409- }
410-
411398func NewMemoryCache () BlobCache {
412399 return & MemoryCache {
413400 Membuf : map [string ]* bytes.Buffer {},
@@ -498,7 +485,7 @@ func (dc *directoryCache) CreateHardlink(key string) error {
498485 return nil
499486 }
500487 log .L .Debugf ("Creating hardlink for key %q" , key )
501- return dc .hlManager .CreateLink (key , dc .cachePath ( key ))
488+ return dc .hlManager .CreateLink (key , BuildCachePath ( dc .directory , key ))
502489}
503490
504491func (dc * directoryCache ) HasHardlink (key string ) bool {
0 commit comments