-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstats.go
More file actions
34 lines (29 loc) · 733 Bytes
/
Copy pathstats.go
File metadata and controls
34 lines (29 loc) · 733 Bytes
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
package fastpass
import "time"
//halving parameters
const (
NextHalfInc = time.Hour * 24 * 7
)
func maxNextHalf() time.Time {
return time.Now().AddDate(0, 2, 0)
}
//Stats contains stats about an entry's access.
type Stats struct {
//Activity should not be incremented directly
Activity int
//NextHalfInc is added to NextHalf every hit, up to maxNextHalf
//It's goal is to prevent entries that are no longer frequently accessed from
//being actively selected
NextHalf time.Time
}
//Hit adds a hit to s
func (s *Stats) Hit() {
if time.Now().After(s.NextHalf) {
s.NextHalf = time.Now()
s.Activity = s.Activity / 2
}
s.Activity++
if s.NextHalf.Before(maxNextHalf()) {
s.NextHalf = s.NextHalf.Add(NextHalfInc)
}
}