Commit 1b161831 authored by zauberstuhl's avatar zauberstuhl

Add some caching to database models

parent 17e508da
......@@ -118,9 +118,9 @@ func generateSchema(version string) SchemaJson {
}
var (
user models.User
comment models.Comment
post models.Post
users models.Users
comments models.Comments
posts models.Posts
)
return SchemaJson{
......@@ -137,12 +137,12 @@ func generateSchema(version string) SchemaJson {
OpenRegistrations: true,
Usage: SchemaUsageJson{
Users: SchemaUsersJson{
Total: user.Count(),
ActiveHalfyear: user.ActiveHalfyear(),
ActiveMonth: user.ActiveMonth(),
Total: users.Count(),
ActiveHalfyear: users.ActiveHalfyear(),
ActiveMonth: users.ActiveMonth(),
},
LocalPosts: post.Count(),
LocalComments: comment.Count(),
LocalPosts: posts.CountLocal(),
LocalComments: comments.CountLocal(),
},
MetaData: SchemaMetaDataJson{
NodeName: appName,
......
......@@ -22,6 +22,8 @@ import (
"github.com/revel/revel"
"git.feneas.org/ganggo/gorm"
"git.feneas.org/ganggo/federation"
"github.com/patrickmn/go-cache"
"fmt"
)
type Comment struct {
......@@ -97,7 +99,15 @@ func (signature *CommentSignature) AfterFind(db *gorm.DB) error {
return db.Model(signature).Related(&signature.SignatureOrder).Error
}
func (c *Comment) Count() (count int) {
func (c *Comments) CountLocal() (count int) {
return c.count(true)
}
func (c *Comments) CountNonLocal() (count int) {
return c.count(false)
}
func (c *Comments) count(local bool) (count int) {
db, err := OpenDatabase()
if err != nil {
revel.AppLog.Error(err.Error())
......@@ -105,9 +115,25 @@ func (c *Comment) Count() (count int) {
}
defer db.Close()
operator := "="
if local {
operator = ">"
}
countInt, found := databaseCache.Get(
fmt.Sprintf("comment.count.%s", operator))
if found {
// return cached value
return countInt.(int)
}
db.Table("comments").Joins(
`left join people on comments.person_id = people.id`,
).Where("people.user_id > 0").Count(&count)
).Where(fmt.Sprintf("people.user_id %s 0", operator)).Count(&count)
// set caching
databaseCache.Set(fmt.Sprintf("comment.count.%s", operator), count,
cache.DefaultExpiration)
return
}
......
......@@ -22,6 +22,7 @@ import (
"errors"
"github.com/revel/revel"
"git.feneas.org/ganggo/ganggo/app/helpers"
"github.com/patrickmn/go-cache"
"git.feneas.org/ganggo/gorm"
_ "git.feneas.org/ganggo/gorm/dialects/postgres"
_ "git.feneas.org/ganggo/gorm/dialects/mssql"
......@@ -62,6 +63,8 @@ const (
var DB Database
var databaseCache = cache.New(5*time.Minute, 10*time.Minute)
func OpenDatabase() (*gorm.DB, error) {
db, err := gorm.Open(DB.Driver, DB.Url)
if err != nil {
......
......@@ -22,6 +22,8 @@ import (
"git.feneas.org/ganggo/gorm"
"github.com/revel/revel"
"git.feneas.org/ganggo/federation"
"github.com/patrickmn/go-cache"
"fmt"
)
type Post struct {
......@@ -161,7 +163,21 @@ func (p *Post) AfterDelete(db *gorm.DB) (err error) {
return
}
func (p *Post) Count() (count int) {
func (p *Posts) CountLocal() (count int) {
return p.count(true, 0)
}
func (p *Posts) CountNonLocal() (count int) {
return p.count(false, 0)
}
func (p *Posts) CountByUserID(id uint) (count int) {
// NOTE false is not intuitive here but
// we need the equal-operator
return p.count(false, id)
}
func (p *Posts) count(local bool, id uint) (count int) {
db, err := OpenDatabase()
if err != nil {
revel.AppLog.Error(err.Error())
......@@ -169,9 +185,24 @@ func (p *Post) Count() (count int) {
}
defer db.Close()
operator := "="
if local {
operator = ">"
}
key := fmt.Sprintf("post.count.%s.%d", operator, id)
countInt, found := databaseCache.Get(key)
if found {
// return cached value
return countInt.(int)
}
db.Table("posts").Joins(
`left join people on posts.person_id = people.id`,
).Where("people.user_id > 0").Count(&count)
).Where(fmt.Sprintf("people.user_id %s %d", operator, id)).Count(&count)
// set caching
databaseCache.Set(key, count, cache.DefaultExpiration)
return
}
......@@ -230,6 +261,20 @@ func (posts *Posts) FindAllPublic(offset uint) (err error) {
return query.Find(posts).Error
}
func (posts *Posts) FindAllNonLocalPublic(offset uint) (err error) {
db, err := OpenDatabase()
if err != nil {
return err
}
defer db.Close()
query := db.Offset(offset).Limit(10).
Joins(`left join people on posts.person_id = people.id`).
Where(`people.user_id = ? and public = ?`, 0, true).Order(`created_at desc`)
return query.Find(posts).Error
}
func (posts *Posts) FindAllPublicByPerson(person Person, offset uint) (err error) {
db, err := OpenDatabase()
if err != nil {
......
......@@ -30,6 +30,7 @@ import (
"encoding/pem"
"golang.org/x/crypto/bcrypt"
"git.feneas.org/ganggo/ganggo/app/helpers"
"github.com/patrickmn/go-cache"
)
type User struct {
......@@ -50,6 +51,8 @@ type User struct {
Aspects []Aspect `gorm:"AssociationForeignKey:UserID"`
}
type Users []User
type UserStream struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
......@@ -151,7 +154,7 @@ func (user *User) FindByUsername(name string) (err error) {
return db.Where("username = ?", name).Find(user).Error
}
func (user *User) Count() (count int) {
func (users *Users) Count() (count int) {
db, err := OpenDatabase()
if err != nil {
revel.AppLog.Error(err.Error())
......@@ -159,10 +162,16 @@ func (user *User) Count() (count int) {
}
defer db.Close()
if countInt, found := databaseCache.Get("users.count.all"); found {
return countInt.(int)
}
db.Table("users").Count(&count)
databaseCache.Set("users.count.all", count, cache.DefaultExpiration)
return
}
func (user *User) ActiveHalfyear() (count int) {
func (users *Users) ActiveHalfyear() (count int) {
db, err := OpenDatabase()
if err != nil {
revel.AppLog.Error(err.Error())
......@@ -170,12 +179,17 @@ func (user *User) ActiveHalfyear() (count int) {
}
defer db.Close()
if countInt, found := databaseCache.Get("users.count.halfyear"); found {
return countInt.(int)
}
halfYear := time.Now().AddDate(0, -6, 0)
db.Table("users").Where("last_seen >= ?", halfYear).Count(&count)
databaseCache.Set("users.count.halfyear", count, cache.DefaultExpiration)
return
}
func (user *User) ActiveMonth() (count int) {
func (users *Users) ActiveMonth() (count int) {
db, err := OpenDatabase()
if err != nil {
revel.AppLog.Error(err.Error())
......@@ -183,8 +197,13 @@ func (user *User) ActiveMonth() (count int) {
}
defer db.Close()
if countInt, found := databaseCache.Get("users.count.month"); found {
return countInt.(int)
}
month := time.Now().AddDate(0, -1, 0)
db.Table("users").Where("last_seen >= ?", month).Count(&count)
databaseCache.Set("users.count.month", count, cache.DefaultExpiration)
return
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment