cacheinterface/cache.go

49 lines
764 B
Go

package cache
import (
"errors"
"net/url"
"strings"
"time"
)
const (
Memcache = "memcache"
Redis = "redis"
)
var (
ErrInvalidDriver = errors.New("invalid driver")
)
type CacheInterface interface {
Has(key string) bool
Get(key string) ([]byte, error)
Set(key string, val interface{}, ttl time.Duration) (err error)
Del(key string) error
}
func New(uri string) (CacheInterface, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
query := u.Query()
switch u.Scheme {
case Redis:
return NewRedisCache(RedisSettings{
Address: u.Host,
Password: query.Get("password"),
})
case Memcache:
return NewMemcacheCache(MemcacheSettings{
Servers: strings.Split(u.Host, ","),
})
}
return nil, ErrInvalidDriver
}