diff --git a/activity.go b/activity.go index ac6b853..74a4201 100644 --- a/activity.go +++ b/activity.go @@ -1167,7 +1167,11 @@ func honkworldwide(user *WhatAbout, honk *Honk) { } } -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 @@ func asjonker(user *WhatAbout) junk.Junk { 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) { diff --git a/cache.go b/cache.go index e89d039..c2250a4 100644 --- a/cache.go +++ b/cache.go @@ -18,18 +18,22 @@ package main 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 @@ func cacheNew(options cacheOptions) *Cache { 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) diff --git a/database.go b/database.go index a0b3d26..4787670 100644 --- a/database.go +++ b/database.go @@ -28,9 +28,9 @@ import ( "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 { diff --git a/web.go b/web.go index 16ec947..db798f1 100644 --- a/web.go +++ b/web.go @@ -531,9 +531,13 @@ func showuser(w http.ResponseWriter, r *http.Request) { 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)