Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

cache: Add total torrent count and fixes

Cette révision appartient à :
bakape 2017-05-10 12:32:49 +03:00
Parent 5d40d02820
révision 58ca34132c
4 fichiers modifiés avec 22 ajouts et 14 suppressions

24
cache/cache.go externe
Voir le fichier

@ -35,13 +35,13 @@ type store struct {
lastFetched time.Time
key common.SearchParam
data []model.Torrent
size int
count, size int
}
// Check the cache for and existing record. If miss, run fn to retrieve fresh
// values.
func Get(key common.SearchParam, fn func() ([]model.Torrent, error)) (
[]model.Torrent, error,
func Get(key common.SearchParam, fn func() ([]model.Torrent, int, error)) (
data []model.Torrent, count int, err error,
) {
s := getStore(key)
@ -51,15 +51,15 @@ func Get(key common.SearchParam, fn func() ([]model.Torrent, error)) (
defer s.Unlock()
if s.isFresh() {
return s.data, nil
return s.data, s.count, nil
}
data, err := fn()
data, count, err = fn()
if err != nil {
return nil, err
return
}
s.update(data)
return data, nil
s.update(data, count)
return
}
// Retrieve a store from the cache or create a new one
@ -103,17 +103,21 @@ func updateUsedSize(delta int) {
// Return, if the data can still be considered fresh, without querying the DB
func (s *store) isFresh() bool {
return s.lastFetched.Add(expiryTime).Before(time.Now())
if s.lastFetched.IsZero() { // New store
return false
}
return s.lastFetched.Add(expiryTime).After(time.Now())
}
// Stores the new values of s. Calculates and stores the new size. Passes the
// delta to the central cache to fire eviction checks.
func (s *store) update(data []model.Torrent) {
func (s *store) update(data []model.Torrent, count int) {
newSize := 0
for _, d := range data {
newSize += d.Size()
}
s.data = data
s.count = count
delta := newSize - s.size
s.size = newSize
s.lastFetched = time.Now()

Voir le fichier

@ -41,6 +41,7 @@ type SearchParam struct {
Status Status
Sort SortMode
Category Category
Page int
Max uint
Query string
}

Voir le fichier

@ -52,7 +52,9 @@ func (t Torrent) Size() (s int) {
2*2 // array pointers
s *= 8 // Assume 64 bit OS
s += t.Uploader.Size()
if t.Uploader != nil {
s += t.Uploader.Size()
}
for _, c := range t.OldComments {
s += c.Size()
}

Voir le fichier

@ -36,6 +36,7 @@ func searchByQuery(r *http.Request, pagenum int, countAll bool) (
}
search.Max = uint(max)
search.Page = pagenum
search.Query = r.URL.Query().Get("q")
switch s := r.URL.Query().Get("s"); s {
@ -94,7 +95,7 @@ func searchByQuery(r *http.Request, pagenum int, countAll bool) (
orderBy += "desc"
}
tor, err = cache.Get(search, func() (tor []model.Torrent, err error) {
tor, count, err = cache.Get(search, func() (tor []model.Torrent, count int, err error) {
parameters := torrentService.WhereParams{
Params: make([]interface{}, 0, 64),
}
@ -144,9 +145,9 @@ func searchByQuery(r *http.Request, pagenum int, countAll bool) (
parameters.Conditions = strings.Join(conditions[:], " AND ")
log.Infof("SQL query is :: %s\n", parameters.Conditions)
if countAll {
tor, count, err = torrentService.GetTorrentsOrderBy(&parameters, orderBy, int(search.Max), int(search.Max)*(pagenum-1))
tor, count, err = torrentService.GetTorrentsOrderBy(&parameters, orderBy, int(search.Max), int(search.Max)*(search.Page-1))
} else {
tor, err = torrentService.GetTorrentsOrderByNoCount(&parameters, orderBy, int(search.Max), int(search.Max)*(pagenum-1))
tor, err = torrentService.GetTorrentsOrderByNoCount(&parameters, orderBy, int(search.Max), int(search.Max)*(search.Page-1))
}
return