move cache to the separate package

This commit is contained in:
Alexander Zobnin
2020-06-03 10:24:32 +03:00
parent d8d83484af
commit 1e04c310df
6 changed files with 13 additions and 10 deletions

2
go.mod
View File

@@ -9,7 +9,7 @@ require (
github.com/grafana/grafana-plugin-sdk-go v0.65.0
github.com/grafana/grafana_plugin_model v0.0.0-20180518082423-84176c64269d
github.com/hashicorp/go-hclog v0.9.2
github.com/hashicorp/go-plugin v1.2.2
github.com/hashicorp/go-plugin v1.2.2 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.8.1 // indirect

View File

@@ -1,4 +1,4 @@
package main
package cache
import (
"crypto/sha1"

View File

@@ -1,4 +1,4 @@
package main
package cache
import (
"testing"

View File

@@ -7,6 +7,7 @@ import (
"fmt"
"time"
"github.com/alexanderzobnin/grafana-zabbix/pkg/cache"
"github.com/alexanderzobnin/grafana-zabbix/pkg/gtime"
"github.com/alexanderzobnin/grafana-zabbix/pkg/zabbixapi"
@@ -16,7 +17,7 @@ import (
)
type ZabbixDatasource struct {
datasourceCache *Cache
datasourceCache *cache.Cache
logger log.Logger
}
@@ -26,7 +27,7 @@ type ZabbixDatasourceInstance struct {
zabbixAPI *zabbixapi.ZabbixAPI
dsInfo *backend.DataSourceInstanceSettings
Settings *ZabbixDatasourceSettings
queryCache *Cache
queryCache *cache.Cache
logger log.Logger
}
@@ -46,7 +47,7 @@ func NewZabbixDatasourceInstance(dsInfo *backend.DataSourceInstanceSettings) (*Z
dsInfo: dsInfo,
zabbixAPI: zabbixAPI,
Settings: zabbixSettings,
queryCache: NewCache(zabbixSettings.CacheTTL, 10*time.Minute),
queryCache: cache.NewCache(zabbixSettings.CacheTTL, 10*time.Minute),
logger: log.New(),
}, nil
}
@@ -113,11 +114,11 @@ func (ds *ZabbixDatasource) GetDatasource(pluginContext backend.PluginContext) (
dsSettings := pluginContext.DataSourceInstanceSettings
dsKey := fmt.Sprintf("%d-%d", pluginContext.OrgID, dsSettings.ID)
// Get hash to check if settings changed
dsInfoHash := HashDatasourceInfo(dsSettings)
dsInfoHash := cache.HashDatasourceInfo(dsSettings)
if cachedData, ok := ds.datasourceCache.Get(dsKey); ok {
if cachedDS, ok := cachedData.(*ZabbixDatasourceInstance); ok {
cachedDSHash := HashDatasourceInfo(cachedDS.dsInfo)
cachedDSHash := cache.HashDatasourceInfo(cachedDS.dsInfo)
if cachedDSHash == dsInfoHash {
return cachedDS, nil
}

View File

@@ -5,6 +5,7 @@ import (
"os"
"time"
"github.com/alexanderzobnin/grafana-zabbix/pkg/cache"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana-plugin-sdk-go/backend/resource/httpadapter"
@@ -43,7 +44,7 @@ func Init(logger log.Logger, mux *http.ServeMux) *ZabbixDatasource {
ds := &ZabbixDatasource{
logger: logger,
datasourceCache: NewCache(10*time.Minute, 10*time.Minute),
datasourceCache: cache.NewCache(10*time.Minute, 10*time.Minute),
}
mux.HandleFunc("/", ds.rootHandler)

View File

@@ -6,6 +6,7 @@ import (
"regexp"
"time"
"github.com/alexanderzobnin/grafana-zabbix/pkg/cache"
"github.com/alexanderzobnin/grafana-zabbix/pkg/zabbix"
"github.com/alexanderzobnin/grafana-zabbix/pkg/zabbixapi"
simplejson "github.com/bitly/go-simplejson"
@@ -28,7 +29,7 @@ var CachedMethods = map[string]bool{
func (ds *ZabbixDatasourceInstance) ZabbixQuery(ctx context.Context, apiReq *ZabbixAPIRequest) (*simplejson.Json, error) {
var resultJson *simplejson.Json
var err error
requestHash := HashString(apiReq.String())
requestHash := cache.HashString(apiReq.String())
cachedResult, queryExistInCache := ds.queryCache.Get(requestHash)
if !queryExistInCache {