34 lines
562 B
Go
34 lines
562 B
Go
package memory
|
|
|
|
import "time"
|
|
|
|
// Item represents an item stored in the cache.
|
|
type Item struct {
|
|
Value any
|
|
Expiration int64
|
|
}
|
|
|
|
// Expired returns true if the item has expired.
|
|
func (i *Item) Expired() bool {
|
|
if i.Expiration == 0 {
|
|
return false
|
|
}
|
|
|
|
return time.Now().UnixNano() > i.Expiration
|
|
}
|
|
|
|
// NewCache returns a new cache.
|
|
func NewCache(opts ...Option) *memCache {
|
|
options := NewOptions(opts...)
|
|
items := make(map[string]Item)
|
|
|
|
if len(options.Items) > 0 {
|
|
items = options.Items
|
|
}
|
|
|
|
return &memCache{
|
|
opts: options,
|
|
items: items,
|
|
}
|
|
}
|