mirror of https://github.com/grafana/grafana
parent
1dc8b898bb
commit
cfb061ddab
@ -0,0 +1,14 @@ |
||||
package middleware |
||||
|
||||
import ( |
||||
m "github.com/grafana/grafana/pkg/models" |
||||
macaron "gopkg.in/macaron.v1" |
||||
) |
||||
|
||||
const HeaderNameNoBackendCache = "X-Grafana-NoCache" |
||||
|
||||
func HandleNoCacheHeader() macaron.Handler { |
||||
return func(ctx *m.ReqContext) { |
||||
ctx.SkipCache = ctx.Req.Header.Get(HeaderNameNoBackendCache) == "true" |
||||
} |
||||
} |
@ -0,0 +1,17 @@ |
||||
package cache |
||||
|
||||
import ( |
||||
"time" |
||||
|
||||
gocache "github.com/patrickmn/go-cache" |
||||
) |
||||
|
||||
type CacheService struct { |
||||
*gocache.Cache |
||||
} |
||||
|
||||
func New(defaultExpiration, cleanupInterval time.Duration) *CacheService { |
||||
return &CacheService{ |
||||
Cache: gocache.New(defaultExpiration, cleanupInterval), |
||||
} |
||||
} |
@ -0,0 +1,49 @@ |
||||
package datasources |
||||
|
||||
import ( |
||||
"fmt" |
||||
"time" |
||||
|
||||
"github.com/grafana/grafana/pkg/bus" |
||||
m "github.com/grafana/grafana/pkg/models" |
||||
"github.com/grafana/grafana/pkg/registry" |
||||
"github.com/grafana/grafana/pkg/services/cache" |
||||
) |
||||
|
||||
type CacheService interface { |
||||
GetDatasource(datasourceID int64, user *m.SignedInUser, skipCache bool) (*m.DataSource, error) |
||||
} |
||||
|
||||
type CacheServiceImpl struct { |
||||
Bus bus.Bus `inject:""` |
||||
CacheService *cache.CacheService `inject:""` |
||||
} |
||||
|
||||
func init() { |
||||
registry.RegisterService(&CacheServiceImpl{}) |
||||
} |
||||
|
||||
func (dc *CacheServiceImpl) Init() error { |
||||
return nil |
||||
} |
||||
|
||||
func (dc *CacheServiceImpl) GetDatasource(datasourceID int64, user *m.SignedInUser, skipCache bool) (*m.DataSource, error) { |
||||
cacheKey := fmt.Sprintf("ds-%d", datasourceID) |
||||
|
||||
if !skipCache { |
||||
if cached, found := dc.CacheService.Get(cacheKey); found { |
||||
ds := cached.(*m.DataSource) |
||||
if ds.OrgId == user.OrgId { |
||||
return ds, nil |
||||
} |
||||
} |
||||
} |
||||
|
||||
query := m.GetDataSourceByIdQuery{Id: datasourceID, OrgId: user.OrgId} |
||||
if err := dc.Bus.Dispatch(&query); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
dc.CacheService.Set(cacheKey, query.Result, time.Second*5) |
||||
return query.Result, nil |
||||
} |
Loading…
Reference in new issue