mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
add support for memcached
This commit is contained in:
parent
5ced863f75
commit
11d671c637
@ -39,12 +39,12 @@ func createClient(opts CacheOpts, sqlstore *sqlstore.SqlStore) cacheStorage {
|
||||
}
|
||||
|
||||
if opts.name == "memcache" {
|
||||
return nil
|
||||
return newMemcacheStorage("localhost:9090")
|
||||
}
|
||||
|
||||
if opts.name == "memory" {
|
||||
return nil
|
||||
}
|
||||
// if opts.name == "memory" {
|
||||
// return nil
|
||||
// }
|
||||
|
||||
return newDatabaseCache(sqlstore) //&databaseCache{SQLStore: sqlstore}
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ func createTestClient(t *testing.T, name string) cacheStorage {
|
||||
}
|
||||
|
||||
func TestAllCacheClients(t *testing.T) {
|
||||
clients := []string{"database", "redis"} // add redis, memcache, memory
|
||||
clients := []string{"database", "redis", "memcached"} // add redis, memcache, memory
|
||||
|
||||
for _, v := range clients {
|
||||
client := createTestClient(t, v)
|
||||
|
62
pkg/infra/distcache/memcached_storage.go
Normal file
62
pkg/infra/distcache/memcached_storage.go
Normal file
@ -0,0 +1,62 @@
|
||||
package distcache
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/bradfitz/gomemcache/memcache"
|
||||
)
|
||||
|
||||
type memcacheStorage struct {
|
||||
c *memcache.Client
|
||||
}
|
||||
|
||||
func newMemcacheStorage(connStr string) *memcacheStorage {
|
||||
return &memcacheStorage{
|
||||
c: memcache.New(connStr),
|
||||
}
|
||||
}
|
||||
|
||||
func NewItem(sid string, data []byte, expire int32) *memcache.Item {
|
||||
return &memcache.Item{
|
||||
Key: sid,
|
||||
Value: data,
|
||||
Expiration: expire,
|
||||
}
|
||||
}
|
||||
|
||||
// Set sets value to given key in the cache.
|
||||
func (s *memcacheStorage) Put(key string, val interface{}, expires time.Duration) error {
|
||||
item := &Item{Val: val}
|
||||
|
||||
bytes, err := EncodeGob(item)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
memcacheItem := NewItem(key, bytes, int32(expires))
|
||||
|
||||
s.c.Add(memcacheItem)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get gets value by given key in the cache.
|
||||
func (s *memcacheStorage) Get(key string) (interface{}, error) {
|
||||
i, err := s.c.Get(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
item := &Item{}
|
||||
|
||||
err = DecodeGob(i.Value, item)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return item.Val, nil
|
||||
}
|
||||
|
||||
// Delete delete a key from the cache
|
||||
func (s *memcacheStorage) Delete(key string) error {
|
||||
return s.c.Delete(key)
|
||||
}
|
Loading…
Reference in New Issue
Block a user