Data sources: Use SQL store directly, not via bus (#27000)

* Server: Make it possible to avoid getting SQL store via bus

Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>
This commit is contained in:
Arve Knudsen
2020-08-21 09:30:06 +02:00
committed by GitHub
co-authored by Emil Tullstedt
parent a73e6f728d
commit 45adfe7732
4 changed files with 41 additions and 16 deletions
+12 -7
View File
@@ -4,10 +4,10 @@ import (
"fmt"
"time"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/localcache"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/services/sqlstore"
)
type CacheService interface {
@@ -15,8 +15,8 @@ type CacheService interface {
}
type CacheServiceImpl struct {
Bus bus.Bus `inject:""`
CacheService *localcache.CacheService `inject:""`
SQLStore *sqlstore.SqlStore `inject:""`
}
func init() {
@@ -31,7 +31,11 @@ func (dc *CacheServiceImpl) Init() error {
return nil
}
func (dc *CacheServiceImpl) GetDatasource(datasourceID int64, user *models.SignedInUser, skipCache bool) (*models.DataSource, error) {
func (dc *CacheServiceImpl) GetDatasource(
datasourceID int64,
user *models.SignedInUser,
skipCache bool,
) (*models.DataSource, error) {
cacheKey := fmt.Sprintf("ds-%d", datasourceID)
if !skipCache {
@@ -43,11 +47,12 @@ func (dc *CacheServiceImpl) GetDatasource(datasourceID int64, user *models.Signe
}
}
query := models.GetDataSourceByIdQuery{Id: datasourceID, OrgId: user.OrgId}
if err := dc.Bus.Dispatch(&query); err != nil {
plog.Debug("Querying for data source via SQL store", "id", datasourceID, "orgId", user.OrgId)
ds, err := dc.SQLStore.GetDataSourceByID(datasourceID, user.OrgId)
if err != nil {
return nil, err
}
dc.CacheService.Set(cacheKey, query.Result, time.Second*5)
return query.Result, nil
dc.CacheService.Set(cacheKey, ds, time.Second*5)
return ds, nil
}