all repos — honk @ 6c605142b3c6e6afd791d357909ea315a9934b08

my fork of honk

more reflection for nicer cache filling types
Ted Unangst tedu@tedunangst.com
Fri, 04 Oct 2019 14:08:40 -0400
commit

6c605142b3c6e6afd791d357909ea315a9934b08

parent

eb8a5ac5408008667e5ee0d471d1aff6a63004c7

2 files changed, 15 insertions(+), 4 deletions(-)

jump to
M cache.gocache.go

@@ -28,10 +28,22 @@ filler cacheFiller

lock sync.Mutex } -func cacheNew(filler cacheFiller) *Cache { +func cacheNew(fillfn interface{}) *Cache { c := new(Cache) c.cache = make(map[interface{}]interface{}) - c.filler = filler + ftype := reflect.TypeOf(fillfn) + if ftype.Kind() != reflect.Func { + panic("cache filler is not function") + } + if ftype.NumIn() != 1 || ftype.NumOut() != 2 { + panic("cache filler has wrong argument count") + } + c.filler = func(key interface{}) (interface{}, bool) { + vfn := reflect.ValueOf(fillfn) + args := []reflect.Value{reflect.ValueOf(key)} + rv := vfn.Call(args) + return rv[0].Interface(), rv[1].Bool() + } return c }
M web.goweb.go

@@ -1141,8 +1141,7 @@ log.Print(err)

} } -var combocache = cacheNew(func(key interface{}) (interface{}, bool) { - userid := key.(int64) +var combocache = cacheNew(func(userid int64) ([]string, bool) { honkers := gethonkers(userid) var combos []string for _, h := range honkers {