The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/pkg/infra/distcache/distcache.go

83 lines
1.6 KiB

package distcache
import (
"bytes"
"encoding/gob"
"errors"
"time"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/registry"
)
var (
ErrCacheItemNotFound = errors.New("cache item not found")
)
func init() {
registry.RegisterService(&DistributedCache{})
}
// Init initializes the service
func (ds *DistributedCache) Init() error {
ds.log = log.New("distributed.cache")
ds.Client = createClient(CacheOpts{}, ds.SQLStore)
return nil
}
type CacheOpts struct {
name string
}
func createClient(opts CacheOpts, sqlstore *sqlstore.SqlStore) cacheStorage {
if opts.name == "redis" {
return newRedisStorage(nil)
}
if opts.name == "memcache" {
return newMemcacheStorage("localhost:9090")
}
// if opts.name == "memory" {
// return nil
// }
return newDatabaseCache(sqlstore)
}
// DistributedCache allows Grafana to cache data outside its own process
type DistributedCache struct {
log log.Logger
Client cacheStorage
SQLStore *sqlstore.SqlStore `inject:""`
}
type cachedItem struct {
Val interface{}
}
func encodeGob(item *cachedItem) ([]byte, error) {
buf := bytes.NewBuffer(nil)
err := gob.NewEncoder(buf).Encode(item)
return buf.Bytes(), err
}
func decodeGob(data []byte, out *cachedItem) error {
buf := bytes.NewBuffer(data)
return gob.NewDecoder(buf).Decode(&out)
}
type cacheStorage interface {
// Get reads object from Cache
Get(key string) (interface{}, error)
// Puts an object into the cache
Put(key string, value interface{}, expire time.Duration) error
// Delete object from cache
Delete(key string) error
}