File tree Expand file tree Collapse file tree 1 file changed +6
-7
lines changed Expand file tree Collapse file tree 1 file changed +6
-7
lines changed Original file line number Diff line number Diff line change 5
5
"context"
6
6
"io/fs"
7
7
"sync"
8
- "sync/atomic"
9
8
10
9
"github.com/google/uuid"
11
10
"golang.org/x/xerrors"
@@ -47,7 +46,8 @@ type Cache struct {
47
46
}
48
47
49
48
type cacheEntry struct {
50
- refCount * atomic.Int64
49
+ // refCount must only be accessed while the Cache lock is held.
50
+ refCount int
51
51
value * lazy.ValueWithError [fs.FS ]
52
52
}
53
53
@@ -70,19 +70,18 @@ func (c *Cache) prepare(ctx context.Context, fileID uuid.UUID) *lazy.ValueWithEr
70
70
71
71
entry , ok := c .data [fileID ]
72
72
if ! ok {
73
- var refCount atomic.Int64
74
73
value := lazy .NewWithError (func () (fs.FS , error ) {
75
74
return c .fetcher (ctx , fileID )
76
75
})
77
76
78
77
entry = & cacheEntry {
79
78
value : value ,
80
- refCount : & refCount ,
79
+ refCount : 0 ,
81
80
}
82
81
c .data [fileID ] = entry
83
82
}
84
83
85
- entry .refCount . Add ( 1 )
84
+ entry .refCount += 1
86
85
return entry .value
87
86
}
88
87
@@ -99,8 +98,8 @@ func (c *Cache) Release(fileID uuid.UUID) {
99
98
// this function with an incorrect ID. Should this function return an error?
100
99
return
101
100
}
102
- refCount := entry .refCount . Add ( - 1 )
103
- if refCount > 0 {
101
+ entry .refCount -= 1
102
+ if entry . refCount > 0 {
104
103
return
105
104
}
106
105
delete (c .data , fileID )
You can’t perform that action at this time.
0 commit comments