1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-23 17:20:50 +00:00
Beego/pkg/client/cache/ssdb/ssdb.go

231 lines
4.8 KiB
Go
Raw Normal View History

2020-07-22 14:50:08 +00:00
package ssdb
import (
2020-10-04 14:11:28 +00:00
"context"
2020-07-22 14:50:08 +00:00
"encoding/json"
"errors"
"strconv"
"strings"
"time"
"github.com/ssdb/gossdb/ssdb"
"github.com/astaxie/beego/pkg/client/cache"
2020-07-22 14:50:08 +00:00
)
// Cache SSDB adapter
type Cache struct {
conn *ssdb.Client
conninfo []string
}
2020-08-06 15:07:18 +00:00
//NewSsdbCache creates new ssdb adapter.
2020-07-22 14:50:08 +00:00
func NewSsdbCache() cache.Cache {
return &Cache{}
}
2020-08-06 15:07:18 +00:00
// Get gets a key's value from memcache.
2020-10-04 14:11:28 +00:00
func (rc *Cache) Get(ctx context.Context, key string) (interface{}, error) {
2020-07-22 14:50:08 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
2020-10-04 14:11:28 +00:00
return nil, nil
2020-07-22 14:50:08 +00:00
}
}
value, err := rc.conn.Get(key)
if err == nil {
2020-10-04 14:11:28 +00:00
return value, nil
2020-07-22 14:50:08 +00:00
}
2020-10-04 14:11:28 +00:00
return nil, nil
2020-07-22 14:50:08 +00:00
}
2020-10-04 14:11:28 +00:00
// GetMulti gets one or keys values from ssdb.
func (rc *Cache) GetMulti(ctx context.Context, keys []string) ([]interface{}, error) {
2020-07-22 14:50:08 +00:00
size := len(keys)
var values []interface{}
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
2020-10-04 14:11:28 +00:00
return values, err
2020-07-22 14:50:08 +00:00
}
}
res, err := rc.conn.Do("multi_get", keys)
resSize := len(res)
if err == nil {
for i := 1; i < resSize; i += 2 {
values = append(values, res[i+1])
}
2020-10-04 14:11:28 +00:00
return values, nil
2020-07-22 14:50:08 +00:00
}
for i := 0; i < size; i++ {
values = append(values, err)
}
2020-10-04 14:11:28 +00:00
return values, nil
2020-07-22 14:50:08 +00:00
}
2020-08-06 15:07:18 +00:00
// DelMulti deletes one or more keys from memcache
2020-07-22 14:50:08 +00:00
func (rc *Cache) DelMulti(keys []string) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
_, err := rc.conn.Do("multi_del", keys)
return err
}
2020-08-06 15:07:18 +00:00
// Put puts value into memcache.
// value: must be of type string
2020-10-04 14:11:28 +00:00
func (rc *Cache) Put(ctx context.Context, key string, val interface{}, timeout time.Duration) error {
2020-07-22 14:50:08 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
2020-10-04 14:11:28 +00:00
v, ok := val.(string)
2020-07-22 14:50:08 +00:00
if !ok {
return errors.New("value must string")
}
var resp []string
var err error
ttl := int(timeout / time.Second)
if ttl < 0 {
resp, err = rc.conn.Do("set", key, v)
} else {
resp, err = rc.conn.Do("setx", key, v, ttl)
}
if err != nil {
return err
}
if len(resp) == 2 && resp[0] == "ok" {
return nil
}
return errors.New("bad response")
}
2020-08-06 15:07:18 +00:00
// Delete deletes a value in memcache.
2020-10-04 14:11:28 +00:00
func (rc *Cache) Delete(ctx context.Context, key string) error {
2020-07-22 14:50:08 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
_, err := rc.conn.Del(key)
return err
}
2020-08-06 15:07:18 +00:00
// Incr increases a key's counter.
2020-10-04 14:11:28 +00:00
func (rc *Cache) Incr(ctx context.Context, key string) error {
2020-07-22 14:50:08 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
_, err := rc.conn.Do("incr", key, 1)
return err
}
2020-08-06 15:07:18 +00:00
// Decr decrements a key's counter.
2020-10-04 14:11:28 +00:00
func (rc *Cache) Decr(ctx context.Context, key string) error {
2020-07-22 14:50:08 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
_, err := rc.conn.Do("incr", key, -1)
return err
}
2020-08-06 15:07:18 +00:00
// IsExist checks if a key exists in memcache.
2020-10-04 14:11:28 +00:00
func (rc *Cache) IsExist(ctx context.Context, key string) (bool, error) {
2020-07-22 14:50:08 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
2020-10-04 14:11:28 +00:00
return false, err
2020-07-22 14:50:08 +00:00
}
}
resp, err := rc.conn.Do("exists", key)
if err != nil {
2020-10-04 14:11:28 +00:00
return false, err
2020-07-22 14:50:08 +00:00
}
if len(resp) == 2 && resp[1] == "1" {
2020-10-04 14:11:28 +00:00
return true, nil
2020-07-22 14:50:08 +00:00
}
2020-10-04 14:11:28 +00:00
return false, nil
2020-07-22 14:50:08 +00:00
}
2020-08-06 15:07:18 +00:00
// ClearAll clears all cached items in memcache.
2020-10-04 14:11:28 +00:00
func (rc *Cache) ClearAll(context.Context) error {
2020-07-22 14:50:08 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
keyStart, keyEnd, limit := "", "", 50
resp, err := rc.Scan(keyStart, keyEnd, limit)
for err == nil {
size := len(resp)
if size == 1 {
return nil
}
keys := []string{}
for i := 1; i < size; i += 2 {
keys = append(keys, resp[i])
}
_, e := rc.conn.Do("multi_del", keys)
if e != nil {
return e
}
keyStart = resp[size-2]
resp, err = rc.Scan(keyStart, keyEnd, limit)
}
return err
}
// Scan key all cached in ssdb.
func (rc *Cache) Scan(keyStart string, keyEnd string, limit int) ([]string, error) {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return nil, err
}
}
resp, err := rc.conn.Do("scan", keyStart, keyEnd, limit)
if err != nil {
return nil, err
}
return resp, nil
}
2020-08-06 15:07:18 +00:00
// StartAndGC starts the memcache adapter.
// config: must be in the format {"conn":"connection info"}.
// If an error occurs during connection, an error is returned
2020-07-22 14:50:08 +00:00
func (rc *Cache) StartAndGC(config string) error {
var cf map[string]string
json.Unmarshal([]byte(config), &cf)
if _, ok := cf["conn"]; !ok {
return errors.New("config has no conn key")
}
rc.conninfo = strings.Split(cf["conn"], ";")
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
return nil
}
// connect to memcache and keep the connection.
func (rc *Cache) connectInit() error {
conninfoArray := strings.Split(rc.conninfo[0], ":")
host := conninfoArray[0]
port, e := strconv.Atoi(conninfoArray[1])
if e != nil {
return e
}
var err error
rc.conn, err = ssdb.Connect(host, port)
return err
}
func init() {
cache.Register("ssdb", NewSsdbCache)
}