2019-10-02 04:10:27 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2023-02-05 01:22:41 +00:00
|
|
|
"meow.tf/go/cacheinterface/v2/driver/lru"
|
|
|
|
"meow.tf/go/cacheinterface/v2/driver/memcache"
|
|
|
|
"meow.tf/go/cacheinterface/v2/driver/memory"
|
|
|
|
"meow.tf/go/cacheinterface/v2/driver/redis"
|
2019-10-03 00:36:45 +00:00
|
|
|
"net"
|
2019-10-02 04:10:27 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-02-05 01:22:41 +00:00
|
|
|
type Type string
|
|
|
|
|
2019-10-02 04:10:27 +00:00
|
|
|
const (
|
2023-02-05 01:22:41 +00:00
|
|
|
TypeMemcache Type = "memcache"
|
|
|
|
TypeRedis Type = "redis"
|
|
|
|
TypeMemory Type = "memory"
|
|
|
|
TypeLru Type = "lru"
|
2019-10-02 04:10:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrInvalidDriver = errors.New("invalid driver")
|
|
|
|
)
|
|
|
|
|
2023-02-05 01:22:41 +00:00
|
|
|
func init() {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Driver interface {
|
2019-10-02 04:10:27 +00:00
|
|
|
Has(key string) bool
|
2023-02-05 01:22:41 +00:00
|
|
|
Get(key string, dst any) error
|
|
|
|
GetBytes(key string) ([]byte, error)
|
|
|
|
Set(key string, val any, ttl time.Duration) (err error)
|
2019-10-02 04:10:27 +00:00
|
|
|
Del(key string) error
|
|
|
|
}
|
|
|
|
|
2023-02-05 01:22:41 +00:00
|
|
|
type (
|
|
|
|
Marshaller func(val any) ([]byte, error)
|
|
|
|
Unmarshaler func(b []byte, dest any) error
|
|
|
|
)
|
|
|
|
|
|
|
|
func New(uri string) (Driver, error) {
|
2019-10-02 04:10:27 +00:00
|
|
|
u, err := url.Parse(uri)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
query := u.Query()
|
|
|
|
|
2023-02-05 01:22:41 +00:00
|
|
|
switch Type(u.Scheme) {
|
|
|
|
case TypeRedis:
|
2019-10-03 00:36:45 +00:00
|
|
|
port := u.Port()
|
|
|
|
|
|
|
|
if port == "" {
|
|
|
|
port = "6379"
|
|
|
|
}
|
|
|
|
|
2023-02-05 01:22:41 +00:00
|
|
|
var settings redis.Options
|
2019-10-03 00:17:34 +00:00
|
|
|
|
2023-02-05 01:22:41 +00:00
|
|
|
if err = decodeQuery(query, &settings); err != nil {
|
2019-10-03 00:17:34 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-05 01:22:41 +00:00
|
|
|
settings.Address = net.JoinHostPort(u.Hostname(), port)
|
2019-10-05 20:56:24 +00:00
|
|
|
|
2023-02-05 01:22:41 +00:00
|
|
|
return redis.New(settings)
|
|
|
|
case TypeMemcache:
|
|
|
|
var options memcache.Options
|
2019-10-05 20:56:24 +00:00
|
|
|
|
2023-02-05 01:22:41 +00:00
|
|
|
if err = decodeQuery(query, &options); err != nil {
|
2019-10-05 20:56:24 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-05 01:22:41 +00:00
|
|
|
options.Servers = strings.Split(u.Host, ",")
|
2019-10-02 04:10:27 +00:00
|
|
|
|
2023-02-05 01:22:41 +00:00
|
|
|
return memcache.New(options)
|
|
|
|
case TypeMemory:
|
|
|
|
var options memory.Options
|
2019-10-03 00:17:34 +00:00
|
|
|
|
2023-02-05 01:22:41 +00:00
|
|
|
if err = decodeQuery(query, &options); err != nil {
|
2019-10-03 00:17:34 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-05 01:22:41 +00:00
|
|
|
return memory.New(options)
|
|
|
|
case TypeLru:
|
|
|
|
var options lru.Options
|
2019-10-03 00:17:34 +00:00
|
|
|
|
2023-02-05 01:22:41 +00:00
|
|
|
if err = decodeQuery(query, &options); err != nil {
|
|
|
|
return nil, err
|
2019-10-03 00:17:34 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 01:22:41 +00:00
|
|
|
return lru.New(options)
|
2019-10-03 00:17:34 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 01:22:41 +00:00
|
|
|
return nil, ErrInvalidDriver
|
2019-10-03 00:17:34 +00:00
|
|
|
}
|