You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
synctv/utils/syncCache/item.go

35 lines
637 B
Go

package synccache
import (
"sync/atomic"
"time"
)
type Entry[V any] struct {
expiration int64
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]) Value() V {
return e.value
}
func (e *Entry[V]) IsExpired() bool {
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())
}