Cache front page results
This commit is contained in:
parent
f4ca9e2dbc
commit
d40dc21627
5 changed files with 82 additions and 89 deletions
|
@ -5,8 +5,6 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/go-pg/pg"
|
||||
|
||||
log "github.com/cihub/seelog"
|
||||
)
|
||||
|
||||
// Book metadata
|
||||
|
@ -90,9 +88,6 @@ func (db *pgDB) getBooks(active bool, query string, length int, start int) (book
|
|||
Offset(start).
|
||||
Limit(length).
|
||||
SelectAndCountEstimate(1000)
|
||||
if query == "" && active {
|
||||
num = db.bookCount
|
||||
}
|
||||
return books, num, err
|
||||
}
|
||||
|
||||
|
@ -170,28 +165,6 @@ func (db *pgDB) IsBookActive(id string) bool {
|
|||
return active[0]
|
||||
}
|
||||
|
||||
func (db *pgDB) updateCounter() {
|
||||
var count int
|
||||
err := db.sql.Model(&Book{}).
|
||||
ColumnExpr("count(*)").
|
||||
Where("active = true").
|
||||
Select(&count)
|
||||
if err != nil {
|
||||
log.Error("Error updating count: ", err)
|
||||
} else {
|
||||
db.bookCount = count
|
||||
}
|
||||
}
|
||||
|
||||
func (db *pgDB) countUpdater() {
|
||||
periodicity := 61 * time.Minute
|
||||
|
||||
for true {
|
||||
db.updateCounter()
|
||||
time.Sleep(periodicity)
|
||||
}
|
||||
}
|
||||
|
||||
type columnq struct {
|
||||
column string
|
||||
value string
|
||||
|
|
|
@ -26,19 +26,25 @@ type DB interface {
|
|||
GetNews(num int, days int) (news []New, err error)
|
||||
IncViews(ID string) error
|
||||
IncDownloads(ID string) error
|
||||
GetVisitedBooks(num int) (books []Book, err error)
|
||||
GetDownloadedBooks(num int) (books []Book, err error)
|
||||
GetTags() ([]string, error)
|
||||
GetFrontPage() FrontPage
|
||||
}
|
||||
|
||||
const (
|
||||
tagsDisplay = 50
|
||||
tagsDisplay = 50
|
||||
booksFrontPage = 6
|
||||
)
|
||||
|
||||
type pgDB struct {
|
||||
sql *pg.DB
|
||||
tags []string
|
||||
bookCount int
|
||||
frontPage FrontPage
|
||||
}
|
||||
|
||||
type FrontPage struct {
|
||||
Count int
|
||||
Last []Book
|
||||
Visited []Book
|
||||
Download []Book
|
||||
Tags []string
|
||||
}
|
||||
|
||||
// Options for the database
|
||||
|
@ -66,8 +72,7 @@ func Init(options Options) (DB, error) {
|
|||
|
||||
var db pgDB
|
||||
db.sql = sql
|
||||
go db.tagUpdater()
|
||||
go db.countUpdater()
|
||||
go db.frontPageUpdater()
|
||||
|
||||
return &db, nil
|
||||
}
|
||||
|
|
|
@ -85,14 +85,6 @@ func (db *roDB) IncDownloads(ID string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (db *roDB) GetVisitedBooks(num int) (books []Book, err error) {
|
||||
return db.db.GetVisitedBooks(num)
|
||||
}
|
||||
|
||||
func (db *roDB) GetDownloadedBooks(num int) (books []Book, err error) {
|
||||
return db.db.GetDownloadedBooks(num)
|
||||
}
|
||||
|
||||
func (db *roDB) GetTags() ([]string, error) {
|
||||
return db.db.GetTags()
|
||||
func (db *roDB) GetFrontPage() FrontPage {
|
||||
return db.db.GetFrontPage()
|
||||
}
|
||||
|
|
|
@ -40,9 +40,54 @@ func (db *pgDB) IncDownloads(ID string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
/* GetVisitedBooks get the most visited books
|
||||
*/
|
||||
func (db *pgDB) GetVisitedBooks(num int) (books []Book, err error) {
|
||||
func (db *pgDB) GetFrontPage() FrontPage {
|
||||
return db.frontPage
|
||||
}
|
||||
|
||||
func (db *pgDB) frontPageUpdater() {
|
||||
periodicity := 5 * time.Minute
|
||||
|
||||
for true {
|
||||
tags, err := db.getTags(tagsDisplay)
|
||||
if err != nil {
|
||||
log.Error("Error updating tags: ", err)
|
||||
} else {
|
||||
db.frontPage.Tags = tags
|
||||
}
|
||||
|
||||
count, err := db.getCount()
|
||||
if err != nil {
|
||||
log.Error("Error updating count: ", err)
|
||||
} else {
|
||||
db.frontPage.Count = count
|
||||
}
|
||||
|
||||
last, _, err := db.GetBooks("", booksFrontPage, 0)
|
||||
if err != nil {
|
||||
log.Error("Error updating last books: ", err)
|
||||
} else {
|
||||
db.frontPage.Last = last
|
||||
}
|
||||
|
||||
visited, err := db.getVisitedBooks(booksFrontPage)
|
||||
if err != nil {
|
||||
log.Error("Error updating visited books: ", err)
|
||||
} else {
|
||||
db.frontPage.Visited = visited
|
||||
}
|
||||
|
||||
download, err := db.getDownloadedBooks(booksFrontPage)
|
||||
if err != nil {
|
||||
log.Error("Error updating download books: ", err)
|
||||
} else {
|
||||
db.frontPage.Download = download
|
||||
}
|
||||
|
||||
time.Sleep(periodicity)
|
||||
}
|
||||
}
|
||||
|
||||
func (db *pgDB) getVisitedBooks(num int) (books []Book, err error) {
|
||||
err = db.sql.Model(&books).
|
||||
Column("Visit").
|
||||
Order("views DESC NULLS LAST").
|
||||
|
@ -51,9 +96,7 @@ func (db *pgDB) GetVisitedBooks(num int) (books []Book, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
/* GetDownloadedBooks the most downloaded books
|
||||
*/
|
||||
func (db *pgDB) GetDownloadedBooks(num int) (books []Book, err error) {
|
||||
func (db *pgDB) getDownloadedBooks(num int) (books []Book, err error) {
|
||||
err = db.sql.Model(&books).
|
||||
Column("Visit").
|
||||
Order("downloads DESC NULLS LAST").
|
||||
|
@ -62,31 +105,21 @@ func (db *pgDB) GetDownloadedBooks(num int) (books []Book, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func (db *pgDB) GetTags() ([]string, error) {
|
||||
return db.tags, nil
|
||||
}
|
||||
|
||||
func (db *pgDB) updateTags() {
|
||||
var tags []string
|
||||
err := db.sql.Model(&Book{}).
|
||||
func (db *pgDB) getTags(num int) (tags []string, err error) {
|
||||
err = db.sql.Model(&Book{}).
|
||||
ColumnExpr("unnest(tags) as tag").
|
||||
Where("active = true").
|
||||
Group("tag").
|
||||
OrderExpr("count(*) DESC").
|
||||
Limit(tagsDisplay).
|
||||
Limit(num).
|
||||
Select(&tags)
|
||||
if err != nil {
|
||||
log.Error("Error updating tags: ", err)
|
||||
} else {
|
||||
db.tags = tags
|
||||
}
|
||||
return tags, err
|
||||
}
|
||||
|
||||
func (db *pgDB) tagUpdater() {
|
||||
periodicity := 57 * time.Minute
|
||||
|
||||
for true {
|
||||
db.updateTags()
|
||||
time.Sleep(periodicity)
|
||||
}
|
||||
func (db *pgDB) getCount() (count int, err error) {
|
||||
err = db.sql.Model(&Book{}).
|
||||
ColumnExpr("count(*)").
|
||||
Where("active = true").
|
||||
Select(&count)
|
||||
return count, err
|
||||
}
|
||||
|
|
|
@ -14,7 +14,6 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
booksFrontPage = 6
|
||||
daysNewsIndexpage = 15
|
||||
cacheMaxAge = 1800
|
||||
|
||||
|
@ -110,27 +109,18 @@ type indexData struct {
|
|||
|
||||
func indexHandler(h handler) {
|
||||
var data indexData
|
||||
var err error
|
||||
|
||||
data.Tags, err = h.db.GetTags()
|
||||
if err != nil {
|
||||
log.Warn("Problem getting tags: ", err)
|
||||
}
|
||||
data.S = GetStatus(h)
|
||||
data.S.Home = true
|
||||
data.Books, data.Count, err = h.db.GetBooks("", booksFrontPage, 0)
|
||||
if err != nil {
|
||||
log.Warn("Problem getting front books: ", err)
|
||||
}
|
||||
data.VisitedBooks, err = h.db.GetVisitedBooks(booksFrontPage)
|
||||
if err != nil {
|
||||
log.Warn("Problem getting visited books: ", err)
|
||||
}
|
||||
data.DownloadedBooks, err = h.db.GetDownloadedBooks(booksFrontPage)
|
||||
if err != nil {
|
||||
log.Warn("Problem getting downloaded books: ", err)
|
||||
}
|
||||
data.News = getNews(1, daysNewsIndexpage, h.db)
|
||||
|
||||
frontPage := h.db.GetFrontPage()
|
||||
data.Tags = frontPage.Tags
|
||||
data.Books = frontPage.Last
|
||||
data.Count = frontPage.Count
|
||||
data.VisitedBooks = frontPage.Visited
|
||||
data.DownloadedBooks = frontPage.Download
|
||||
|
||||
h.template.load(h, "index", data)
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue