| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package distcache
- import (
- "time"
- "github.com/bradfitz/gomemcache/memcache"
- "github.com/grafana/grafana/pkg/setting"
- )
- type memcacheStorage struct {
- c *memcache.Client
- }
- func newMemcacheStorage(opts *setting.CacheOpts) *memcacheStorage {
- return &memcacheStorage{
- c: memcache.New(opts.ConnStr),
- }
- }
- func newItem(sid string, data []byte, expire int32) *memcache.Item {
- return &memcache.Item{
- Key: sid,
- Value: data,
- Expiration: expire,
- }
- }
- // Put sets value to given key in the cache.
- func (s *memcacheStorage) Put(key string, val interface{}, expires time.Duration) error {
- item := &cachedItem{Val: val}
- bytes, err := encodeGob(item)
- if err != nil {
- return err
- }
- memcacheItem := newItem(key, bytes, int32(expires))
- return s.c.Set(memcacheItem)
- }
- // 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 && err.Error() == "memcache: cache miss" {
- return nil, ErrCacheItemNotFound
- }
- if err != nil {
- return nil, err
- }
- item := &cachedItem{}
- 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)
- }
|