github.com/blend/go-sdk@v1.20220411.3/cache/value.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package cache
     9  
    10  import (
    11  	"time"
    12  )
    13  
    14  // ValueOption is an option for a cache value.
    15  type ValueOption func(*Value)
    16  
    17  // OptValueTTL sets the ttl for the value.
    18  func OptValueTTL(d time.Duration) ValueOption {
    19  	return func(v *Value) {
    20  		v.Expires = v.Timestamp.Add(d)
    21  	}
    22  }
    23  
    24  // OptValueExpires sets the ttl for the value.
    25  func OptValueExpires(expires time.Time) ValueOption {
    26  	return func(v *Value) {
    27  		v.Expires = expires
    28  	}
    29  }
    30  
    31  // OptValueTimestamp sets the timestamp for the value.
    32  func OptValueTimestamp(t time.Time) ValueOption {
    33  	return func(v *Value) {
    34  		v.Timestamp = t
    35  	}
    36  }
    37  
    38  // OptValueOnRemove sets the on remove handler.
    39  func OptValueOnRemove(handler func(interface{}, RemovalReason)) ValueOption {
    40  	return func(v *Value) {
    41  		v.OnRemove = handler
    42  	}
    43  }
    44  
    45  // Value is a cached item.
    46  type Value struct {
    47  	Timestamp time.Time
    48  	Expires   time.Time
    49  	Key       interface{}
    50  	Value     interface{}
    51  	OnRemove  func(interface{}, RemovalReason)
    52  }