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)
|
about := markitzero(user.About)
|
||||||
|
|
||||||
j := junk.New()
|
j := junk.New()
|
||||||
|
@ -1193,7 +1197,15 @@ func asjonker(user *WhatAbout) junk.Junk {
|
||||||
k["publicKeyPem"] = user.Key
|
k["publicKeyPem"] = user.Key
|
||||||
j["publicKey"] = k
|
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) {
|
var handfull = cacheNew(cacheOptions{Filler: func(name string) (string, bool) {
|
||||||
|
|
20
cache.go
20
cache.go
|
@ -18,18 +18,22 @@ package main
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type cacheFiller func(key interface{}) (interface{}, bool)
|
type cacheFiller func(key interface{}) (interface{}, bool)
|
||||||
|
|
||||||
type cacheOptions struct {
|
type cacheOptions struct {
|
||||||
Filler interface{}
|
Filler interface{}
|
||||||
|
Duration time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
type Cache struct {
|
type Cache struct {
|
||||||
cache map[interface{}]interface{}
|
cache map[interface{}]interface{}
|
||||||
filler cacheFiller
|
filler cacheFiller
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
|
stale time.Time
|
||||||
|
duration time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func cacheNew(options cacheOptions) *Cache {
|
func cacheNew(options cacheOptions) *Cache {
|
||||||
|
@ -49,12 +53,20 @@ func cacheNew(options cacheOptions) *Cache {
|
||||||
rv := vfn.Call(args)
|
rv := vfn.Call(args)
|
||||||
return rv[0].Interface(), rv[1].Bool()
|
return rv[0].Interface(), rv[1].Bool()
|
||||||
}
|
}
|
||||||
|
if options.Duration != 0 {
|
||||||
|
c.duration = options.Duration
|
||||||
|
c.stale = time.Now().Add(c.duration)
|
||||||
|
}
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cache *Cache) Get(key interface{}, value interface{}) bool {
|
func (cache *Cache) Get(key interface{}, value interface{}) bool {
|
||||||
cache.lock.Lock()
|
cache.lock.Lock()
|
||||||
defer cache.lock.Unlock()
|
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]
|
v, ok := cache.cache[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
v, ok = cache.filler(key)
|
v, ok = cache.filler(key)
|
||||||
|
|
|
@ -28,9 +28,9 @@ import (
|
||||||
"humungus.tedunangst.com/r/webs/login"
|
"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)
|
row := stmtWhatAbout.QueryRow(name)
|
||||||
user := new (WhatAbout)
|
user := new(WhatAbout)
|
||||||
var options string
|
var options string
|
||||||
err := row.Scan(&user.ID, &user.Name, &user.Display, &user.About, &user.Key, &options)
|
err := row.Scan(&user.ID, &user.Name, &user.Display, &user.About, &user.Key, &options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
10
web.go
10
web.go
|
@ -531,9 +531,13 @@ func showuser(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if friendorfoe(r.Header.Get("Accept")) {
|
if friendorfoe(r.Header.Get("Accept")) {
|
||||||
j := asjonker(user)
|
j, ok := asjonker(name)
|
||||||
w.Header().Set("Content-Type", theonetruename)
|
if ok {
|
||||||
j.Write(w)
|
w.Header().Set("Content-Type", theonetruename)
|
||||||
|
w.Write(j)
|
||||||
|
} else {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
u := login.GetUserInfo(r)
|
u := login.GetUserInfo(r)
|
||||||
|
|
Loading…
Reference in New Issue