Go TTL Cache

A Simple Cache That Expires Items.
When getting an item from the cache, the ttl is updated and reset to the current time. This helps prevent frequently accessed items from expiring.
This package uses the haxmap package for better performance.
Installation
go get github.com/AspieSoft/go-ttlcache
Usage
import (
"time"
"github.com/AspieSoft/go-ttlcache"
)
var cache *ttlcache.Cache[string, interface{}]
func main(){
// create a new cache with a time to live
cache = ttlcache.New[string, interface{}](2 * time.Hour)
// optional auto deletion interval (default: 1 hour)
cache = ttlcache.New[string, interface{}](2 * time.Hour, 4 * time.Hour)
cache.Set("Item1", 10)
cache.Set("Item2", 20)
if value, ok := cache.Get("Item1"); ok {
// value = 10
}
cache.Len() // returns the number of items that have not expired
cache.MapLen() // returns the total number of items actually stored in the cache (expired items may still be stored, but the ok value will return false if expired)
if value, ok := cache.Get("Item2"); !ok {
// Item2 has expired, but the cache may still hold this value and return it
}
// reset the cache expire time to keep this value longer
cache.Touch("Item2")
// change the time to live
cache.TTL(1 * time.Hour)
// optional also change auto deletion interval
cache.TTL(12 * time.Hour, 24 * time.Hour)
}