[chore] pull in latest go-cache, go-runners versions (#1306)

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

Signed-off-by: kim <grufwub@gmail.com>
This commit is contained in:
kim
2023-01-06 10:16:09 +00:00
committed by GitHub
parent 0dbe6c514f
commit adbc87700a
23 changed files with 329 additions and 865 deletions

View File

@@ -56,8 +56,8 @@ func New[Value any](lookups []Lookup, copy func(Value) Value, cap int) *Cache[Va
c.lookups = make([]structKey, len(lookups))
for i, lookup := range lookups {
// Generate keyed field info for lookup
c.lookups[i] = genStructKey(lookup, t)
// Create keyed field info for lookup
c.lookups[i] = newStructKey(lookup, t)
}
// Create and initialize underlying cache
@@ -159,7 +159,7 @@ func (c *Cache[Value]) Load(lookup string, load func() (Value, error), keyParts
keyInfo := c.lookups.get(lookup)
// Generate cache key string.
ckey := genKey(keyParts...)
ckey := keyInfo.genKey(keyParts)
// Acquire cache lock
c.cache.Lock()
@@ -248,17 +248,17 @@ func (c *Cache[Value]) Store(value Value, store func() error) error {
func (c *Cache[Value]) Has(lookup string, keyParts ...any) bool {
var res result[Value]
// Get lookup key type by name.
keyType := c.lookups.get(lookup)
// Get lookup key info by name.
keyInfo := c.lookups.get(lookup)
// Generate cache key string.
ckey := genKey(keyParts...)
ckey := keyInfo.genKey(keyParts)
// Acquire cache lock
c.cache.Lock()
// Look for primary key for cache key
pkey, ok := keyType.pkeys[ckey]
pkey, ok := keyInfo.pkeys[ckey]
if ok {
// Fetch the result for primary key
@@ -275,15 +275,15 @@ func (c *Cache[Value]) Has(lookup string, keyParts ...any) bool {
// Invalidate will invalidate any result from the cache found under given lookup and key parts.
func (c *Cache[Value]) Invalidate(lookup string, keyParts ...any) {
// Get lookup key type by name.
keyType := c.lookups.get(lookup)
// Get lookup key info by name.
keyInfo := c.lookups.get(lookup)
// Generate cache key string.
ckey := genKey(keyParts...)
ckey := keyInfo.genKey(keyParts)
// Look for primary key for cache key
c.cache.Lock()
pkey, ok := keyType.pkeys[ckey]
pkey, ok := keyInfo.pkeys[ckey]
c.cache.Unlock()
if !ok {

View File

@@ -1,6 +1,7 @@
package result
import (
"fmt"
"reflect"
"strings"
"sync"
@@ -51,10 +52,10 @@ func (sk structKeys) generate(a any) []cacheKey {
buf.B = buf.B[:0]
// Append each field value to buffer.
for _, idx := range sk[i].fields {
fv := v.Field(idx)
for _, field := range sk[i].fields {
fv := v.Field(field.index)
fi := fv.Interface()
buf.B = mangler.Append(buf.B, fi)
buf.B = field.mangle(buf.B, fi)
buf.B = append(buf.B, '.')
}
@@ -123,17 +124,58 @@ type structKey struct {
// fields is a slice of runtime struct field
// indices, of the fields encompassed by this key.
fields []int
fields []structField
// pkeys is a lookup of stored struct key values
// to the primary cache lookup key (int64).
pkeys map[string]int64
}
// genStructKey will generate a structKey{} information object for user-given lookup
type structField struct {
// index is the reflect index of this struct field.
index int
// mangle is the mangler function for
// serializing values of this struct field.
mangle mangler.Mangler
}
// genKey generates a cache key string for given key parts (i.e. serializes them using "go-mangler").
func (sk structKey) genKey(parts []any) string {
// Check this expected no. key parts.
if len(parts) != len(sk.fields) {
panic(fmt.Sprintf("incorrect no. key parts provided: want=%d received=%d", len(parts), len(sk.fields)))
}
// Acquire byte buffer
buf := getBuf()
defer putBuf(buf)
buf.Reset()
// Encode each key part
for i, part := range parts {
buf.B = sk.fields[i].mangle(buf.B, part)
buf.B = append(buf.B, '.')
}
// Drop last '.'
buf.Truncate(1)
// Return string copy
return string(buf.B)
}
// newStructKey will generate a structKey{} information object for user-given lookup
// key information, and the receiving generic paramter's type information. Panics on error.
func genStructKey(lk Lookup, t reflect.Type) structKey {
var zeros []any
func newStructKey(lk Lookup, t reflect.Type) structKey {
var (
sk structKey
zeros []any
)
// Set the lookup name
sk.name = lk.Name
// Split dot-separated lookup to get
// the individual struct field names
@@ -142,8 +184,8 @@ func genStructKey(lk Lookup, t reflect.Type) structKey {
panic("no key fields specified")
}
// Pre-allocate slice of expected length
fields := make([]int, len(names))
// Allocate the mangler and field indices slice.
sk.fields = make([]structField, len(names))
for i, name := range names {
// Get field info for given name
@@ -158,60 +200,30 @@ func genStructKey(lk Lookup, t reflect.Type) structKey {
}
// Set the runtime field index
fields[i] = ft.Index[0]
sk.fields[i].index = ft.Index[0]
// Allocate new instance of field
v := reflect.New(ft.Type)
v = v.Elem()
// Fetch mangler for field type.
sk.fields[i].mangle = mangler.Get(ft.Type)
if !lk.AllowZero {
// Append the zero value interface
zeros = append(zeros, v.Interface())
}
}
var zvalue string
if len(zeros) > 0 {
// Generate zero value string
zvalue = genKey(zeros...)
sk.zero = sk.genKey(zeros)
}
return structKey{
name: lk.Name,
zero: zvalue,
fields: fields,
pkeys: make(map[string]int64),
}
}
// Allocate primary lookup map
sk.pkeys = make(map[string]int64)
// genKey generates a cache key for given key values.
func genKey(parts ...any) string {
if len(parts) == 0 {
// Panic to prevent annoying usecase
// where user forgets to pass lookup
// and instead only passes a key part,
// e.g. cache.Get("key")
// which then always returns false.
panic("no key parts provided")
}
// Acquire byte buffer
buf := getBuf()
defer putBuf(buf)
buf.Reset()
// Encode each key part
for _, part := range parts {
buf.B = mangler.Append(buf.B, part)
buf.B = append(buf.B, '.')
}
// Drop last '.'
buf.Truncate(1)
// Return string copy
return string(buf.B)
return sk
}
// isExported checks whether function name is exported.

View File

@@ -6,15 +6,15 @@ import (
"codeberg.org/gruf/go-sched"
)
// scheduler is the global cache runtime scheduler
// for handling regular cache evictions.
// scheduler is the global cache runtime
// scheduler for handling cache evictions.
var scheduler sched.Scheduler
// schedule will given sweep routine to the global scheduler, and start global scheduler.
func schedule(sweep func(time.Time), freq time.Duration) func() {
if !scheduler.Running() {
// ensure running
_ = scheduler.Start()
// ensure sched running
_ = scheduler.Start(nil)
}
return scheduler.Schedule(sched.NewJob(sweep).Every(freq))
}