-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgc.go
48 lines (41 loc) · 1.53 KB
/
gc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package filecache
import "time"
// GarbageCollector is a tool to remove expired cache items.
type GarbageCollector interface {
// OnInstanceInit is executed on the initialization of the FileCache instance.
OnInstanceInit()
// OnOperation is executed on the every item's operation in the FileCache instance.
OnOperation()
// Close closes the GarbageCollector.
Close() error
}
// NewNopGarbageCollector returns the GarbageCollector doing nothing.
func NewNopGarbageCollector() GarbageCollector {
return &gcNop{}
}
// NewProbabilityGarbageCollector returns the GarbageCollector running with the defined probability.
//
// Function arguments:
// - dir: the directory with the FileCache's instance files;
// - onInitDivisor: divisor for the probability on the OnInstanceInit() function call;
// - onOpDivisor: divisor for the probability on the OnOperation() function call.
//
// Divisor is a run probability divisor (e.g., divisor equals 100 is a 1/100 probability).
func NewProbabilityGarbageCollector(dir string, onInitDivisor uint, onOpDivisor uint) GarbageCollector {
return &gcProbability{
dir: dir,
onInitDivisor: onInitDivisor,
onOpDivisor: onOpDivisor,
}
}
// NewIntervalGarbageCollector returns the GarbageCollector running by the interval.
//
// Function arguments:
// - dir: the directory with the FileCache's instance files;
// - interval: the GC interval duration.
func NewIntervalGarbageCollector(dir string, interval time.Duration) GarbageCollector {
return &gcInterval{
dir: dir,
interval: interval,
}
}