1
+ // Package mdcache implements a leveled memory/disk cache combination.
1
2
package mdcache
2
3
3
4
import (
@@ -9,9 +10,21 @@ import (
9
10
log "github.com/sirupsen/logrus"
10
11
)
11
12
13
+ // Options give room for finetuning the behavior of Memory/Disk cache.
12
14
type Options struct {
15
+ // MaxMemoryUsage of L1 in bytes
13
16
MaxMemoryUsage int64
14
- SwapDirectory string
17
+
18
+ // SwapDirectory specifies where L2 pages are stored.
19
+ // If empty, no l2 cache is used. Instead another l1 cache
20
+ // is used in its place, rendering MaxMemoryUsage useless.
21
+ // You have to set both for an effect.
22
+ SwapDirectory string
23
+
24
+ // L1CacheMissRefill will propagate
25
+ // data from L2 to L1 if it could be found
26
+ // successfully.
27
+ L1CacheMissRefill bool
15
28
16
29
// TODO: Those need to be still implemented.
17
30
@@ -23,17 +36,12 @@ type Options struct {
23
36
// on load. Reduces storage, but increases CPU usage if you're swapping.
24
37
// Since swapping is slow anyways this is recommended.
25
38
L2Compress bool
26
-
27
- // L1CacheMissRefill will propagate
28
- // data from L2 to L1 if it could be found
29
- // successfully.
30
- L1CacheMissRefill bool
31
39
}
32
40
33
41
type cacheLayer interface {
34
42
Get (pk pageKey ) (* page.Page , error )
35
43
Set (pk pageKey , p * page.Page ) error
36
- Del (pks []pageKey ) error
44
+ Del (pks []pageKey )
37
45
Close () error
38
46
}
39
47
@@ -62,7 +70,8 @@ func (pk pageKey) String() string {
62
70
return filepath .Join (string (s [:2 ]), string (s [2 :]))
63
71
}
64
72
65
- func NewDirCache (opts Options ) (* MDCache , error ) {
73
+ // New returns a new Memory/Disk cache
74
+ func New (opts Options ) (* MDCache , error ) {
66
75
l2 , err := newL2Cache (opts .SwapDirectory )
67
76
if err != nil {
68
77
return nil , err
@@ -89,6 +98,7 @@ func NewDirCache(opts Options) (*MDCache, error) {
89
98
}, nil
90
99
}
91
100
101
+ // Lookup implements pagecache.Cache
92
102
func (dc * MDCache ) Lookup (inode int64 , pageIdx uint32 ) (* page.Page , error ) {
93
103
dc .mu .Lock ()
94
104
defer dc .mu .Unlock ()
@@ -120,6 +130,7 @@ func (dc *MDCache) get(pk pageKey) (*page.Page, error) {
120
130
}
121
131
}
122
132
133
+ // Merge implements pagecache.Cache
123
134
func (dc * MDCache ) Merge (inode int64 , pageIdx , off uint32 , write []byte ) error {
124
135
dc .mu .Lock ()
125
136
defer dc .mu .Unlock ()
@@ -149,6 +160,7 @@ func (dc *MDCache) Merge(inode int64, pageIdx, off uint32, write []byte) error {
149
160
return dc .l1 .Set (pk , p )
150
161
}
151
162
163
+ // Evict implements pagecache.Cache
152
164
func (dc * MDCache ) Evict (inode , size int64 ) error {
153
165
dc .mu .Lock ()
154
166
defer dc .mu .Unlock ()
@@ -164,18 +176,12 @@ func (dc *MDCache) Evict(inode, size int64) error {
164
176
pks = append (pks , pageKey {inode : inode , pageIdx : pageIdx })
165
177
}
166
178
167
- if err := dc .l1 .Del (pks ); err != nil {
168
- log .WithError (err ).Warnf ("l1 delete failed for %v" , pks )
169
- }
170
-
171
- // TODO: This will spam logs in case of no page:
172
- if err := dc .l2 .Del (pks ); err != nil {
173
- log .WithError (err ).Warnf ("l2 delete failed for %v" , pks )
174
- }
175
-
179
+ dc .l1 .Del (pks )
180
+ dc .l2 .Del (pks )
176
181
return nil
177
182
}
178
183
184
+ // Close closes the cache contents and cleans up resources.
179
185
func (dc * MDCache ) Close () error {
180
186
dc .mu .Lock ()
181
187
defer dc .mu .Unlock ()
0 commit comments