1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-14 17:10:54 +00:00
Beego/pkg/client/cache/ssdb/ssdb.go

233 lines
4.7 KiB
Go
Raw Normal View History

2020-07-22 14:50:08 +00:00
package ssdb
import (
"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-07-22 14:50:08 +00:00
func (rc *Cache) Get(key string) interface{} {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return nil
}
}
value, err := rc.conn.Get(key)
if err == nil {
return value
}
return nil
}
2020-08-06 15:07:18 +00:00
// GetMulti gets one or keys values from memcache.
2020-07-22 14:50:08 +00:00
func (rc *Cache) GetMulti(keys []string) []interface{} {
size := len(keys)
var values []interface{}
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
for i := 0; i < size; i++ {
values = append(values, err)
}
return values
}
}
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])
}
return values
}
for i := 0; i < size; i++ {
values = append(values, err)
}
return values
}
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-07-22 14:50:08 +00:00
func (rc *Cache) Put(key string, value interface{}, timeout time.Duration) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
v, ok := value.(string)
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-07-22 14:50:08 +00:00
func (rc *Cache) Delete(key string) error {
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-07-22 14:50:08 +00:00
func (rc *Cache) Incr(key string) error {
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-07-22 14:50:08 +00:00
func (rc *Cache) Decr(key string) error {
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-07-22 14:50:08 +00:00
func (rc *Cache) IsExist(key string) bool {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return false
}
}
resp, err := rc.conn.Do("exists", key)
if err != nil {
return false
}
if len(resp) == 2 && resp[1] == "1" {
return true
}
return false
}
2020-08-06 15:07:18 +00:00
// ClearAll clears all cached items in memcache.
2020-07-22 14:50:08 +00:00
func (rc *Cache) ClearAll() error {
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)
}