From 9d43354c68a52dd620d88d675a54ef2e489091a4 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Tue, 24 Sep 2019 13:40:20 -0400 Subject: [PATCH] cache combos between pages --- cache.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ web.go | 17 +++++++++++---- 2 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 cache.go diff --git a/cache.go b/cache.go new file mode 100644 index 0000000..f62407f --- /dev/null +++ b/cache.go @@ -0,0 +1,65 @@ +// +// Copyright (c) 2019 Ted Unangst +// +// 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{}) +} diff --git a/web.go b/web.go index b4bd180..f50bf5f 100644 --- a/web.go +++ b/web.go @@ -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" {