added caching of signed in user DB calls

This commit is contained in:
Torkel Ödegaard 2018-10-31 06:47:14 -07:00
parent 07d78da5ec
commit 93453c2d94
2 changed files with 18 additions and 7 deletions

View File

@ -16,6 +16,7 @@ import (
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/services/annotations"
"github.com/grafana/grafana/pkg/services/cache"
"github.com/grafana/grafana/pkg/services/sqlstore/migrations"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/grafana/grafana/pkg/services/sqlstore/sqlutil"
@ -47,8 +48,9 @@ func init() {
}
type SqlStore struct {
Cfg *setting.Cfg `inject:""`
Bus bus.Bus `inject:""`
Cfg *setting.Cfg `inject:""`
Bus bus.Bus `inject:""`
CacheService *cache.CacheService `inject:""`
dbCfg DatabaseConfig
engine *xorm.Engine
@ -148,9 +150,11 @@ func (ss *SqlStore) Init() error {
// Init repo instances
annotations.SetRepository(&SqlAnnotationRepo{})
ss.Bus.SetTransactionManager(ss)
// Register handlers
ss.addUserQueryAndCommandHandlers()
// ensure admin user
if ss.skipEnsureAdmin {
return nil

View File

@ -15,8 +15,9 @@ import (
"github.com/grafana/grafana/pkg/util"
)
func init() {
//bus.AddHandler("sql", CreateUser)
func (ss *SqlStore) addUserQueryAndCommandHandlers() {
ss.Bus.AddHandler(ss.GetSignedInUser)
bus.AddHandler("sql", GetUserById)
bus.AddHandler("sql", UpdateUser)
bus.AddHandler("sql", ChangeUserPassword)
@ -25,7 +26,6 @@ func init() {
bus.AddHandler("sql", SetUsingOrg)
bus.AddHandler("sql", UpdateUserLastSeenAt)
bus.AddHandler("sql", GetUserProfile)
bus.AddHandler("sql", GetSignedInUser)
bus.AddHandler("sql", SearchUsers)
bus.AddHandler("sql", GetUserOrgList)
bus.AddHandler("sql", DeleteUser)
@ -345,12 +345,18 @@ func GetUserOrgList(query *m.GetUserOrgListQuery) error {
return err
}
func GetSignedInUser(query *m.GetSignedInUserQuery) error {
func (ss *SqlStore) GetSignedInUser(query *m.GetSignedInUserQuery) error {
orgId := "u.org_id"
if query.OrgId > 0 {
orgId = strconv.FormatInt(query.OrgId, 10)
}
cacheKey := fmt.Sprintf("signed-in-user-%d-%s", query.UserId, query.OrgId)
if cached, found := ss.CacheService.Get(cacheKey); found {
query.Result = cached.(*m.SignedInUser)
return nil
}
var rawSql = `SELECT
u.id as user_id,
u.is_admin as is_grafana_admin,
@ -401,6 +407,7 @@ func GetSignedInUser(query *m.GetSignedInUserQuery) error {
}
query.Result = &user
ss.CacheService.Set(cacheKey, &user, time.Second*5)
return err
}