Introduce survey and question cache
This commit is contained in:
parent
02a232b1fa
commit
ab88195607
4 changed files with 93 additions and 2 deletions
40
surveys.go
40
surveys.go
|
|
@ -6,13 +6,17 @@ import (
|
|||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var (
|
||||
_score_cache = map[int64]map[int64]*float64{}
|
||||
_score_cache = map[int64]map[int64]*float64{}
|
||||
_score_cache_mutex = sync.RWMutex{}
|
||||
_surveys_cache = map[int64]*Survey{}
|
||||
_surveys_cache_mutex = sync.RWMutex{}
|
||||
)
|
||||
|
||||
func declareAPISurveysRoutes(router *gin.RouterGroup) {
|
||||
|
|
@ -228,8 +232,19 @@ func getSurveys(cnd string, param ...interface{}) (surveys []*Survey, err error)
|
|||
}
|
||||
|
||||
func getSurvey(id int) (s *Survey, err error) {
|
||||
_surveys_cache_mutex.RLock()
|
||||
survey, ok := _surveys_cache[int64(id)]
|
||||
_surveys_cache_mutex.RUnlock()
|
||||
if ok {
|
||||
return survey, nil
|
||||
}
|
||||
|
||||
s = new(Survey)
|
||||
err = DBQueryRow("SELECT id_survey, title, promo, grp, shown, direct, corrected, start_availability, end_availability FROM surveys WHERE id_survey=?", id).Scan(&s.Id, &s.Title, &s.Promo, &s.Group, &s.Shown, &s.Direct, &s.Corrected, &s.StartAvailability, &s.EndAvailability)
|
||||
|
||||
_surveys_cache_mutex.Lock()
|
||||
_surveys_cache[int64(id)] = s
|
||||
_surveys_cache_mutex.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -244,17 +259,29 @@ func NewSurvey(title string, promo uint, group string, shown bool, direct *int64
|
|||
}
|
||||
|
||||
func (s Survey) GetScore(u *User) (score *float64, err error) {
|
||||
_score_cache_mutex.RLock()
|
||||
if _, ok := _score_cache[u.Id]; !ok {
|
||||
_score_cache_mutex.RUnlock()
|
||||
_score_cache_mutex.Lock()
|
||||
_score_cache[u.Id] = map[int64]*float64{}
|
||||
_score_cache_mutex.Unlock()
|
||||
_score_cache_mutex.RLock()
|
||||
}
|
||||
if v, ok := _score_cache[u.Id][s.Id]; ok {
|
||||
|
||||
v, ok := _score_cache[u.Id][s.Id]
|
||||
_score_cache_mutex.RUnlock()
|
||||
|
||||
if ok {
|
||||
score = v
|
||||
} else {
|
||||
err = DBQueryRow("SELECT SUM(score)/COUNT(*) FROM student_scores WHERE id_survey=? AND id_user=?", s.Id, u.Id).Scan(&score)
|
||||
if score != nil {
|
||||
*score = *score / 5.0
|
||||
}
|
||||
|
||||
_score_cache_mutex.Lock()
|
||||
_score_cache[u.Id][s.Id] = score
|
||||
_score_cache_mutex.Unlock()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
@ -288,6 +315,9 @@ func (s *Survey) Update() (*Survey, error) {
|
|||
if _, err := DBExec("UPDATE surveys SET title = ?, promo = ?, grp = ?, shown = ?, direct = ?, corrected = ?, start_availability = ?, end_availability = ? WHERE id_survey = ?", s.Title, s.Promo, s.Group, s.Shown, s.Direct, s.Corrected, s.StartAvailability, s.EndAvailability, s.Id); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
_surveys_cache_mutex.Lock()
|
||||
_surveys_cache[s.Id] = s
|
||||
_surveys_cache_mutex.Unlock()
|
||||
return s, err
|
||||
}
|
||||
}
|
||||
|
|
@ -298,6 +328,9 @@ func (s Survey) Delete() (int64, error) {
|
|||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
_surveys_cache_mutex.Lock()
|
||||
delete(_surveys_cache, s.Id)
|
||||
_surveys_cache_mutex.Unlock()
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
|
@ -308,6 +341,9 @@ func ClearSurveys() (int64, error) {
|
|||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
_surveys_cache_mutex.Lock()
|
||||
_surveys_cache = map[int64]*Survey{}
|
||||
_surveys_cache_mutex.Unlock()
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue