fix: expose the Offset property to allow external modifications

This commit is contained in:
kevinzeng 2022-06-28 12:45:37 +08:00
parent 7fa92f927a
commit 4f8e984df6

View File

@ -27,12 +27,12 @@ type RandomExpireCacheOption func(*RandomExpireCache)
// Cache random time offset expired // Cache random time offset expired
type RandomExpireCache struct { type RandomExpireCache struct {
Cache Cache
offset func() time.Duration Offset func() time.Duration
} }
// Put random time offset expired // Put random time offset expired
func (rec *RandomExpireCache) Put(ctx context.Context, key string, val interface{}, timeout time.Duration) error { func (rec *RandomExpireCache) Put(ctx context.Context, key string, val interface{}, timeout time.Duration) error {
timeout += rec.offset() timeout += rec.Offset()
return rec.Cache.Put(ctx, key, val, timeout) return rec.Cache.Put(ctx, key, val, timeout)
} }
@ -43,6 +43,9 @@ func NewRandomExpireCache(adapter Cache, opts ...RandomExpireCacheOption) Cache
for _, fn := range opts { for _, fn := range opts {
fn(&rec) fn(&rec)
} }
if rec.Offset == nil {
rec.Offset = defaultExpiredFunc
}
return &rec return &rec
} }