all repos — honk @ 4f19ceba2f75702dfeea5ab1604eef55b4e2ddd1

my fork of honk

cache jonker for a bit
Ted Unangst tedu@tedunangst.com
Mon, 07 Oct 2019 15:47:37 -0400
commit

4f19ceba2f75702dfeea5ab1604eef55b4e2ddd1

parent

ed3081fef27ccf14f0282dccad9bbb8cac1ae70e

4 files changed, 39 insertions(+), 11 deletions(-)

jump to
M activity.goactivity.go

@@ -1167,7 +1167,11 @@ go deliverate(0, user.Name, a, msg)

} } -func asjonker(user *WhatAbout) junk.Junk { +var oldjonkers = cacheNew(cacheOptions{Filler: func(name string) ([]byte, bool) { + user, err := butwhatabout(name) + if err != nil { + return nil, false + } about := markitzero(user.About) j := junk.New()

@@ -1193,7 +1197,15 @@ k["owner"] = user.URL

k["publicKeyPem"] = user.Key j["publicKey"] = k - return j + var buf bytes.Buffer + j.Write(&buf) + return buf.Bytes(), true +}, Duration: 1 * time.Minute}) + +func asjonker(name string) ([]byte, bool) { + var j []byte + ok := oldjonkers.Get(name, &j) + return j, ok } var handfull = cacheNew(cacheOptions{Filler: func(name string) (string, bool) {
M cache.gocache.go

@@ -18,18 +18,22 @@

import ( "reflect" "sync" + "time" ) type cacheFiller func(key interface{}) (interface{}, bool) type cacheOptions struct { - Filler interface{} + Filler interface{} + Duration time.Duration } type Cache struct { - cache map[interface{}]interface{} - filler cacheFiller - lock sync.Mutex + cache map[interface{}]interface{} + filler cacheFiller + lock sync.Mutex + stale time.Time + duration time.Duration } func cacheNew(options cacheOptions) *Cache {

@@ -49,12 +53,20 @@ args := []reflect.Value{reflect.ValueOf(key)}

rv := vfn.Call(args) return rv[0].Interface(), rv[1].Bool() } + if options.Duration != 0 { + c.duration = options.Duration + c.stale = time.Now().Add(c.duration) + } return c } func (cache *Cache) Get(key interface{}, value interface{}) bool { cache.lock.Lock() defer cache.lock.Unlock() + if !cache.stale.IsZero() && cache.stale.Before(time.Now()) { + cache.stale = time.Now().Add(cache.duration) + cache.cache = make(map[interface{}]interface{}) + } v, ok := cache.cache[key] if !ok { v, ok = cache.filler(key)
M database.godatabase.go

@@ -28,9 +28,9 @@

"humungus.tedunangst.com/r/webs/login" ) -var someusers = cacheNew(cacheOptions { Filler: func(name string) (*WhatAbout, bool) { +var someusers = cacheNew(cacheOptions{Filler: func(name string) (*WhatAbout, bool) { row := stmtWhatAbout.QueryRow(name) - user := new (WhatAbout) + user := new(WhatAbout) var options string err := row.Scan(&user.ID, &user.Name, &user.Display, &user.About, &user.Key, &options) if err != nil {
M web.goweb.go

@@ -531,9 +531,13 @@ http.NotFound(w, r)

return } if friendorfoe(r.Header.Get("Accept")) { - j := asjonker(user) - w.Header().Set("Content-Type", theonetruename) - j.Write(w) + j, ok := asjonker(name) + if ok { + w.Header().Set("Content-Type", theonetruename) + w.Write(j) + } else { + http.NotFound(w, r) + } return } u := login.GetUserInfo(r)