all repos — honk @ 86a2d571fe0e893ba60cc53ea8b884d30dc3870e

my fork of honk

cache combos between pages
Ted Unangst tedu@tedunangst.com
Tue, 24 Sep 2019 13:40:20 -0400
commit

86a2d571fe0e893ba60cc53ea8b884d30dc3870e

parent

a4ef2e745851363c76b09f43e60792f09e843fb9

2 files changed, 78 insertions(+), 4 deletions(-)

jump to
A cache.go

@@ -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{}) +}
M web.goweb.go

@@ -1079,10 +1079,9 @@ log.Print(err)

} } -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 @@ }

} 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 {

@@ -1108,6 +1115,8 @@ url := r.FormValue("url")

peep := r.FormValue("peep") combos := r.FormValue("combos") honkerid, _ := strconv.ParseInt(r.FormValue("honkerid"), 10, 0) + + defer combocache.Clear(u.UserID) if honkerid > 0 { goodbye := r.FormValue("goodbye")