cache: Integrate with home handler and add size flag
Cette révision appartient à :
Parent
58ca34132c
révision
7af373b849
3 fichiers modifiés avec 26 ajouts et 18 suppressions
7
cache/cache.go
externe
7
cache/cache.go
externe
|
@ -9,15 +9,14 @@ import (
|
||||||
"github.com/ewhal/nyaa/model"
|
"github.com/ewhal/nyaa/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const expiryTime = time.Minute
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cache = make(map[common.SearchParam]*list.Element, 10)
|
cache = make(map[common.SearchParam]*list.Element, 10)
|
||||||
ll = list.New()
|
ll = list.New()
|
||||||
totalUsed int
|
totalUsed int
|
||||||
mu sync.Mutex
|
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 sets the maximum size of the cache before evicting unread data in MB
|
||||||
Size float64 = 1 << 10
|
Size float64 = 1 << 10
|
||||||
)
|
)
|
||||||
|
@ -94,7 +93,7 @@ func updateUsedSize(delta int) {
|
||||||
|
|
||||||
totalUsed += delta
|
totalUsed += delta
|
||||||
|
|
||||||
for totalUsed > int(Size)*(1<<20) {
|
for totalUsed > int(Size)<<20 {
|
||||||
s := ll.Remove(ll.Back()).(*store)
|
s := ll.Remove(ll.Back()).(*store)
|
||||||
delete(cache, s.key)
|
delete(cache, s.key)
|
||||||
totalUsed -= s.size
|
totalUsed -= s.size
|
||||||
|
|
14
main.go
14
main.go
|
@ -3,20 +3,19 @@ package main
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"flag"
|
"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/config"
|
||||||
"github.com/ewhal/nyaa/db"
|
"github.com/ewhal/nyaa/db"
|
||||||
"github.com/ewhal/nyaa/network"
|
"github.com/ewhal/nyaa/network"
|
||||||
"github.com/ewhal/nyaa/router"
|
"github.com/ewhal/nyaa/router"
|
||||||
"github.com/ewhal/nyaa/util/log"
|
"github.com/ewhal/nyaa/util/log"
|
||||||
"github.com/ewhal/nyaa/util/signals"
|
"github.com/ewhal/nyaa/util/signals"
|
||||||
|
"github.com/nicksnyder/go-i18n/i18n"
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func initI18N() {
|
func initI18N() {
|
||||||
|
@ -51,6 +50,7 @@ func main() {
|
||||||
conf := config.New()
|
conf := config.New()
|
||||||
processFlags := conf.BindFlags()
|
processFlags := conf.BindFlags()
|
||||||
defaults := flag.Bool("print-defaults", false, "print the default configuration file on stdout")
|
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()
|
flag.Parse()
|
||||||
if *defaults {
|
if *defaults {
|
||||||
stdout := bufio.NewWriter(os.Stdout)
|
stdout := bufio.NewWriter(os.Stdout)
|
||||||
|
|
|
@ -1,15 +1,18 @@
|
||||||
package router
|
package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"html"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/ewhal/nyaa/cache"
|
||||||
|
"github.com/ewhal/nyaa/common"
|
||||||
"github.com/ewhal/nyaa/model"
|
"github.com/ewhal/nyaa/model"
|
||||||
"github.com/ewhal/nyaa/service/torrent"
|
"github.com/ewhal/nyaa/service/torrent"
|
||||||
"github.com/ewhal/nyaa/util"
|
"github.com/ewhal/nyaa/util"
|
||||||
"github.com/ewhal/nyaa/util/languages"
|
"github.com/ewhal/nyaa/util/languages"
|
||||||
"github.com/ewhal/nyaa/util/log"
|
"github.com/ewhal/nyaa/util/log"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"html"
|
|
||||||
"net/http"
|
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func HomeHandler(w http.ResponseWriter, r *http.Request) {
|
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))
|
search := common.SearchParam{
|
||||||
if !log.CheckError(err) {
|
Max: uint(maxPerPage),
|
||||||
util.SendError(w, err, 400)
|
Page: pagenum,
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
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)
|
b := model.TorrentsToJSON(torrents)
|
||||||
|
|
||||||
|
|
Référencer dans un nouveau ticket