-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathttl_clock.go
More file actions
33 lines (27 loc) · 751 Bytes
/
ttl_clock.go
File metadata and controls
33 lines (27 loc) · 751 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
// Copyright 2023-2024 Phus Lu. All rights reserved.
package lru
import (
"sync"
"sync/atomic"
"time"
)
// clock is the number of seconds since January 1, 2024 UTC
// always use `atomic.LoadUint32(&clock)` for accessing clock value.
var clock uint32
var clockOnce sync.Once
const clockBase = 1704067200 // 2024-01-01T00:00:00Z
func clocking() {
clockOnce.Do(func() {
atomic.StoreUint32(&clock, uint32(time.Now().Unix()-clockBase))
go func(clock *uint32) {
for {
for i := 0; i < 9; i++ {
time.Sleep(100 * time.Millisecond)
atomic.StoreUint32(clock, uint32(time.Now().Unix()-clockBase))
}
time.Sleep(100 * time.Millisecond)
atomic.StoreUint32(clock, uint32(time.Now().Unix()-clockBase))
}
}(&clock)
})
}