cache combos between pages

This commit is contained in:
Ted Unangst 2019-09-24 13:40:20 -04:00
parent d26d4a972c
commit 9d43354c68
2 changed files with 78 additions and 4 deletions

65
cache.go Normal file
View File

@ -0,0 +1,65 @@
//
// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package main
import (
"reflect"
"sync"
)
type cacheFiller func(key interface{}) (interface{}, bool)
type Cache struct {
cache map[interface{}]interface{}
filler cacheFiller
lock sync.Mutex
}
func cacheNew(filler cacheFiller) *Cache {
c := new(Cache)
c.cache = make(map[interface{}]interface{})
c.filler = filler
return c
}
func (cache *Cache) Get(key interface{}, value interface{}) bool {
cache.lock.Lock()
defer cache.lock.Unlock()
v, ok := cache.cache[key]
if !ok {
v, ok = cache.filler(key)
if ok {
cache.cache[key] = v
}
}
if ok {
ptr := reflect.ValueOf(v)
reflect.ValueOf(value).Elem().Set(ptr)
}
return ok
}
func (cache *Cache) Clear(key interface{}) {
cache.lock.Lock()
defer cache.lock.Unlock()
delete(cache.cache, key)
}
func (cache *Cache) Flush() {
cache.lock.Lock()
defer cache.lock.Unlock()
cache.cache = make(map[interface{}]interface{})
}

17
web.go
View File

@ -1079,10 +1079,9 @@ func showhonkers(w http.ResponseWriter, r *http.Request) {
}
}
func showcombos(w http.ResponseWriter, r *http.Request) {
userinfo := login.GetUserInfo(r)
templinfo := getInfo(r)
honkers := gethonkers(userinfo.UserID)
var combocache = cacheNew(func(key interface{}) (interface{}, bool) {
userid := key.(int64)
honkers := gethonkers(userid)
var combos []string
for _, h := range honkers {
combos = append(combos, h.Combos...)
@ -1094,6 +1093,14 @@ func showcombos(w http.ResponseWriter, r *http.Request) {
}
combos = oneofakind(combos)
sort.Strings(combos)
return combos, true
})
func showcombos(w http.ResponseWriter, r *http.Request) {
userinfo := login.GetUserInfo(r)
var combos []string
combocache.Get(userinfo.UserID, &combos)
templinfo := getInfo(r)
templinfo["Combos"] = combos
err := readviews.Execute(w, "combos.html", templinfo)
if err != nil {
@ -1109,6 +1116,8 @@ func submithonker(w http.ResponseWriter, r *http.Request) {
combos := r.FormValue("combos")
honkerid, _ := strconv.ParseInt(r.FormValue("honkerid"), 10, 0)
defer combocache.Clear(u.UserID)
if honkerid > 0 {
goodbye := r.FormValue("goodbye")
if goodbye == "F" {