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

Voir le fichier

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

Voir le fichier

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

Voir le fichier

@ -36,6 +36,7 @@ func searchByQuery(r *http.Request, pagenum int, countAll bool) (
} }
search.Max = uint(max) search.Max = uint(max)
search.Page = pagenum
search.Query = r.URL.Query().Get("q") search.Query = r.URL.Query().Get("q")
switch s := r.URL.Query().Get("s"); s { switch s := r.URL.Query().Get("s"); s {
@ -94,7 +95,7 @@ func searchByQuery(r *http.Request, pagenum int, countAll bool) (
orderBy += "desc" 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{ parameters := torrentService.WhereParams{
Params: make([]interface{}, 0, 64), Params: make([]interface{}, 0, 64),
} }
@ -144,9 +145,9 @@ func searchByQuery(r *http.Request, pagenum int, countAll bool) (
parameters.Conditions = strings.Join(conditions[:], " AND ") parameters.Conditions = strings.Join(conditions[:], " AND ")
log.Infof("SQL query is :: %s\n", parameters.Conditions) log.Infof("SQL query is :: %s\n", parameters.Conditions)
if countAll { 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 { } 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 return