12345678910111213141516171819202122232425262728293031 |
- /*
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at https://mozilla.org/MPL/2.0/.
- */
- package main
- import "time"
- func NewCached[T any](
- value *T,
- lifetime time.Duration,
- ) Cached[T] {
- return Cached[T]{
- value: value,
- expiry: time.Now().UTC().Add(lifetime),
- }
- }
- type Cached[T any] struct {
- value *T
- expiry time.Time
- }
- func (c *Cached[T]) Value() *T {
- return c.value
- }
- func (c *Cached[T]) Expired() bool {
- return c.expiry.Before(time.Now().UTC())
- }
|