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

49
pkg/cache/cache.go vendored Normal file
View File

@@ -0,0 +1,49 @@
package cache
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
cache "github.com/patrickmn/go-cache"
)
// Cache is a abstraction over go-cache.
type Cache struct {
cache *cache.Cache
}
// NewCache creates a go-cache with expiration(ttl) time and cleanupInterval.
func NewCache(ttl time.Duration, cleanupInterval time.Duration) *Cache {
return &Cache{
cache.New(ttl, cleanupInterval),
}
}
// Set the value of the key "request" to "rersponse" with default expiration time.
func (c *Cache) Set(request string, response interface{}) {
c.cache.SetDefault(request, response)
}
// Get the value associated with request from the cache
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))
}

68
pkg/cache/cache_test.go vendored Normal file
View File

@@ -0,0 +1,68 @@
package cache
import (
"testing"
"github.com/grafana/grafana_plugin_model/go/datasource"
"gotest.tools/assert"
)
func TestHashDatasourceInfo(t *testing.T) {
tests := []struct {
name string
dsInfo *datasource.DatasourceInfo
want string
}{
{
name: "Normal Datasource Info",
dsInfo: &datasource.DatasourceInfo{
Id: 1,
OrgId: 1,
Name: "Zabbix",
Type: "alexanderzobnin-zabbix-datasource",
Url: "https://localhost:3306/zabbix/api_jsonrpc.php",
JsonData: "{}",
DecryptedSecureJsonData: map[string]string{
"username": "grafanaZabbixUser",
"password": "$uper$ecr3t!!!",
},
},
want: "ed161f89179c46d9a578e4d7e92ff95444222e0a",
},
// Can't find a case where the input causes the encoder to fail
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := HashDatasourceInfo(tt.dsInfo)
assert.Equal(t, tt.want, got)
})
}
}
func BenchmarkHashDatasourceInfo(b *testing.B) {
benches := []struct {
name string
dsInfo *datasource.DatasourceInfo
}{
{
"Normal Datasource Info",
&datasource.DatasourceInfo{
Id: 4,
OrgId: 6,
Name: "MyZabbixDatasource",
Type: "alexanderzobnin-zabbix-datasource",
Url: "https://localhost:3306/zabbix/api_jsonrpc.php",
JsonData: `{ "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)
})
}
}