From 7af373b849cc900be474fd097f4d9bf9d31ba932 Mon Sep 17 00:00:00 2001 From: bakape Date: Wed, 10 May 2017 13:06:32 +0300 Subject: [PATCH] cache: Integrate with home handler and add size flag --- cache/cache.go | 7 +++---- main.go | 14 +++++++------- router/homeHandler.go | 23 ++++++++++++++++------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/cache/cache.go b/cache/cache.go index 9faa167f..66c77c89 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -9,15 +9,14 @@ import ( "github.com/ewhal/nyaa/model" ) +const expiryTime = time.Minute + var ( cache = make(map[common.SearchParam]*list.Element, 10) ll = list.New() totalUsed int mu sync.Mutex - // Mutable for quicker testing - expiryTime = time.Second * 60 - // Size sets the maximum size of the cache before evicting unread data in MB Size float64 = 1 << 10 ) @@ -94,7 +93,7 @@ func updateUsedSize(delta int) { totalUsed += delta - for totalUsed > int(Size)*(1<<20) { + for totalUsed > int(Size)<<20 { s := ll.Remove(ll.Back()).(*store) delete(cache, s.key) totalUsed -= s.size diff --git a/main.go b/main.go index dbec6dae..a0c8a3a4 100644 --- a/main.go +++ b/main.go @@ -3,20 +3,19 @@ package main import ( "bufio" "flag" + "net/http" + "os" + "path/filepath" + "time" - "github.com/nicksnyder/go-i18n/i18n" - + "github.com/ewhal/nyaa/cache" "github.com/ewhal/nyaa/config" "github.com/ewhal/nyaa/db" "github.com/ewhal/nyaa/network" "github.com/ewhal/nyaa/router" "github.com/ewhal/nyaa/util/log" "github.com/ewhal/nyaa/util/signals" - - "net/http" - "os" - "path/filepath" - "time" + "github.com/nicksnyder/go-i18n/i18n" ) func initI18N() { @@ -51,6 +50,7 @@ func main() { conf := config.New() processFlags := conf.BindFlags() defaults := flag.Bool("print-defaults", false, "print the default configuration file on stdout") + flag.Float64Var(&cache.Size, "c", cache.Size, "size of the search cache in MB") flag.Parse() if *defaults { stdout := bufio.NewWriter(os.Stdout) diff --git a/router/homeHandler.go b/router/homeHandler.go index 55b16d90..ef2156e0 100644 --- a/router/homeHandler.go +++ b/router/homeHandler.go @@ -1,15 +1,18 @@ package router import ( + "html" + "net/http" + "strconv" + + "github.com/ewhal/nyaa/cache" + "github.com/ewhal/nyaa/common" "github.com/ewhal/nyaa/model" "github.com/ewhal/nyaa/service/torrent" "github.com/ewhal/nyaa/util" "github.com/ewhal/nyaa/util/languages" "github.com/ewhal/nyaa/util/log" "github.com/gorilla/mux" - "html" - "net/http" - "strconv" ) func HomeHandler(w http.ResponseWriter, r *http.Request) { @@ -36,11 +39,17 @@ func HomeHandler(w http.ResponseWriter, r *http.Request) { } } - torrents, nbTorrents, err := torrentService.GetAllTorrents(maxPerPage, maxPerPage*(pagenum-1)) - if !log.CheckError(err) { - util.SendError(w, err, 400) - return + search := common.SearchParam{ + Max: uint(maxPerPage), + Page: pagenum, } + torrents, nbTorrents, err := cache.Get(search, func() ([]model.Torrent, int, error) { + torrents, nbTorrents, err := torrentService.GetAllTorrents(maxPerPage, maxPerPage*(pagenum-1)) + if !log.CheckError(err) { + util.SendError(w, err, 400) + } + return torrents, nbTorrents, err + }) b := model.TorrentsToJSON(torrents)