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.

56 lines
1.0 KiB

package cache
import (
"github.com/bradfitz/gomemcache/memcache"
"time"
)
type MemcacheSettings struct {
Servers []string
}
type MemcacheCache struct {
backend *memcache.Client
}
func NewMemcacheCache(s MemcacheSettings) (CacheInterface, error) {
c := memcache.New(s.Servers...)
return &MemcacheCache{
backend: c,
}, nil
}
func (mc *MemcacheCache) Has(key string) bool {
_, err := mc.backend.Get(key)
return err != nil
}
func (mc *MemcacheCache) Get(key string, dst ...interface{}) ([]byte, error) {
item, err := mc.backend.Get(key)
if err != nil {
return nil, err
}
if len(dst) > 0 && dst[0] != nil {
return decodeDst(item.Value, dst[0])
}
return item.Value, nil
}
func (mc *MemcacheCache) Set(key string, val interface{}, ttl time.Duration) error {
v, err := encodeValue(val)
if err != nil {
return err
}
return mc.backend.Set(&memcache.Item{Key: key, Value: v, Expiration: int32(ttl.Seconds())})
}
func (mc *MemcacheCache) Del(key string) error {
return mc.backend.Delete(key)
}