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