[performance] update go-cache library (#1917)

* update go-cache library

Signed-off-by: kim <grufwub@gmail.com>

* fix broken test after cache library upgrade

Signed-off-by: kim <grufwub@gmail.com>

* fix the webfinger test

Signed-off-by: kim <grufwub@gmail.com>

---------

Signed-off-by: kim <grufwub@gmail.com>
This commit is contained in:
kim
2023-06-21 20:08:48 +01:00
committed by GitHub
parent 831ae09f8b
commit 8e0043104d
6 changed files with 58 additions and 28 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"reflect"
"time"
_ "unsafe"
"codeberg.org/gruf/go-cache/v3/ttl"
"codeberg.org/gruf/go-errors/v2"
@@ -385,7 +386,7 @@ func (c *Cache[Value]) store(res result[Value]) (evict func()) {
// Store main entry under primary key, using evict hook if needed
c.cache.Cache.SetWithHook(pnext, &ttl.Entry[int64, result[Value]]{
Expiry: time.Now().Add(c.cache.TTL),
Expiry: c.expiry(),
Key: pnext,
Value: res,
}, func(_ int64, item *ttl.Entry[int64, result[Value]]) {
@@ -395,6 +396,19 @@ func (c *Cache[Value]) store(res result[Value]) (evict func()) {
return evict
}
//go:linkname runtime_nanotime runtime.nanotime
func runtime_nanotime() uint64
// expiry returns an the next expiry time to use for an entry,
// which is equivalent to time.Now().Add(ttl), or zero if disabled.
func (c *Cache[Value]) expiry() uint64 {
if ttl := c.cache.TTL; ttl > 0 {
return runtime_nanotime() +
uint64(c.cache.TTL)
}
return 0
}
type result[Value any] struct {
// keys accessible under
Keys cacheKeys