github.com/phuslu/lru@v1.0.16-0.20240421170520-46288a2fd47c/ttl_clock.go (about) 1 // Copyright 2023-2024 Phus Lu. All rights reserved. 2 3 package lru 4 5 import ( 6 "sync" 7 "sync/atomic" 8 "time" 9 ) 10 11 // clock is the number of seconds since January 1, 2024 UTC 12 // always use `atomic.LoadUint32(&clock)` for accessing clock value. 13 var clock uint32 14 15 var clockOnce sync.Once 16 17 const clockBase = 1704067200 // 2024-01-01T00:00:00Z 18 19 func clocking() { 20 clockOnce.Do(func() { 21 atomic.StoreUint32(&clock, uint32(time.Now().Unix()-clockBase)) 22 go func(clock *uint32) { 23 for { 24 for i := 0; i < 9; i++ { 25 time.Sleep(100 * time.Millisecond) 26 atomic.StoreUint32(clock, uint32(time.Now().Unix()-clockBase)) 27 } 28 time.Sleep(100 * time.Millisecond) 29 atomic.StoreUint32(clock, uint32(time.Now().Unix()-clockBase)) 30 } 31 }(&clock) 32 }) 33 }