cache jonker for a bit
This commit is contained in:
parent
cd99584ffa
commit
8187aea4f2
16
activity.go
16
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) {
|
||||
|
|
20
cache.go
20
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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
10
web.go
10
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)
|
||||
|
|
Loading…
Reference in New Issue