mirror of https://github.com/synctv-org/synctv
Feat: sync cache set expiraton
parent
62a7e8f255
commit
2111d3bc31
@ -1,12 +1,30 @@
|
|||||||
package synccache
|
package synccache
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type entry[V any] struct {
|
type entry[V any] struct {
|
||||||
expiration time.Time
|
expiration int64
|
||||||
value V
|
value V
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewEntry[V any](value V, expire time.Duration) *entry[V] {
|
||||||
|
return &entry[V]{
|
||||||
|
expiration: time.Now().Add(expire).UnixMilli(),
|
||||||
|
value: value,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (e *entry[V]) IsExpired() bool {
|
func (e *entry[V]) IsExpired() bool {
|
||||||
return time.Now().After(e.expiration)
|
return time.Now().After(time.UnixMilli(atomic.LoadInt64(&e.expiration)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *entry[V]) AddExpiration(d time.Duration) {
|
||||||
|
atomic.AddInt64(&e.expiration, int64(d))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *entry[V]) SetExpiration(t time.Time) {
|
||||||
|
atomic.StoreInt64(&e.expiration, t.UnixMilli())
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue