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))
}