move cache to datasource_cache

This commit is contained in:
Alexander Zobnin
2020-06-04 12:10:34 +03:00
parent 89dfb1e228
commit 95c8e9cfa6
6 changed files with 61 additions and 30 deletions

22
pkg/cache/cache.go vendored
View File

@@ -1,12 +1,8 @@
package cache
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
cache "github.com/patrickmn/go-cache"
)
@@ -19,6 +15,8 @@ const (
DefaultExpiration time.Duration = 0
)
// TODO: since all methods moved to datasource_cache, this can be removed and replaced by go-cache
// Cache is a abstraction over go-cache.
type Cache struct {
cache *cache.Cache
@@ -40,19 +38,3 @@ func (c *Cache) Set(request string, response interface{}) {
func (c *Cache) Get(request string) (interface{}, bool) {
return c.cache.Get(request)
}
// HashString converts the given text string to hash string
func HashString(text string) string {
hash := sha1.New()
hash.Write([]byte(text))
return hex.EncodeToString(hash.Sum(nil))
}
// HashDatasourceInfo converts the given datasource info to hash string
func HashDatasourceInfo(dsInfo *backend.DataSourceInstanceSettings) string {
digester := sha1.New()
if err := json.NewEncoder(digester).Encode(dsInfo); err != nil {
panic(err) // This shouldn't be possible but just in case DatasourceInfo changes
}
return hex.EncodeToString(digester.Sum(nil))
}

View File

@@ -1,100 +0,0 @@
package cache
import (
"testing"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"gotest.tools/assert"
)
func TestHashDatasourceInfo(t *testing.T) {
tests := []struct {
name string
dsInfoBefore *backend.DataSourceInstanceSettings
dsInfoAfter *backend.DataSourceInstanceSettings
equal bool
}{
{
name: "Same datasource settings",
dsInfoBefore: &backend.DataSourceInstanceSettings{
ID: 1,
Name: "Zabbix",
URL: "https://localhost:3306/zabbix/api_jsonrpc.php",
JSONData: []byte("{}"),
DecryptedSecureJSONData: map[string]string{
"username": "grafanaZabbixUser",
"password": "$uper$ecr3t!!!",
},
},
dsInfoAfter: &backend.DataSourceInstanceSettings{
ID: 1,
Name: "Zabbix",
URL: "https://localhost:3306/zabbix/api_jsonrpc.php",
JSONData: []byte("{}"),
DecryptedSecureJSONData: map[string]string{
"username": "grafanaZabbixUser",
"password": "$uper$ecr3t!!!",
},
},
equal: true,
},
{
name: "Password changed",
dsInfoBefore: &backend.DataSourceInstanceSettings{
ID: 1,
Name: "Zabbix",
URL: "https://localhost:3306/zabbix/api_jsonrpc.php",
JSONData: []byte("{}"),
DecryptedSecureJSONData: map[string]string{
"username": "grafanaZabbixUser",
"password": "$uper$ecr3t!!!",
},
},
dsInfoAfter: &backend.DataSourceInstanceSettings{
ID: 1,
Name: "Zabbix",
URL: "https://localhost:3306/zabbix/api_jsonrpc.php",
JSONData: []byte("{}"),
DecryptedSecureJSONData: map[string]string{
"username": "grafanaZabbixUser",
"password": "new$uper$ecr3t!!!",
},
},
equal: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
before := HashDatasourceInfo(tt.dsInfoBefore)
after := HashDatasourceInfo(tt.dsInfoAfter)
got := before == after
assert.Equal(t, tt.equal, got)
})
}
}
func BenchmarkHashDatasourceInfo(b *testing.B) {
benches := []struct {
name string
dsInfo *backend.DataSourceInstanceSettings
}{
{
"Normal Datasource Info",
&backend.DataSourceInstanceSettings{
ID: 4,
Name: "MyZabbixDatasource",
URL: "https://localhost:3306/zabbix/api_jsonrpc.php",
JSONData: []byte(`{ "addThresholds": true, "disableReadOnlyUsersAck": true }`),
DecryptedSecureJSONData: map[string]string{
"username": "grafanaZabbixUser",
"password": "$uper$ecr3t!!!",
},
},
},
}
for _, bt := range benches {
b.Run(bt.name, func(b *testing.B) {
HashDatasourceInfo(bt.dsInfo)
})
}
}