1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-29 01:35:46 +00:00
Beego/cache/ssdb/ssdb.go

232 lines
4.5 KiB
Go
Raw Normal View History

2016-03-06 05:17:16 +00:00
package ssdb
import (
"encoding/json"
"errors"
"strconv"
"strings"
2016-03-08 06:44:37 +00:00
"time"
2016-03-06 05:17:16 +00:00
"github.com/ssdb/gossdb/ssdb"
"github.com/astaxie/beego/cache"
)
// Cache SSDB adapter
type Cache struct {
conn *ssdb.Client
conninfo []string
}
//NewSsdbCache create new ssdb adapter.
func NewSsdbCache() cache.Cache {
return &Cache{}
}
// Get get value from memcache.
func (rc *Cache) Get(key string) interface{} {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
2016-03-07 07:00:03 +00:00
return nil
2016-03-06 05:17:16 +00:00
}
}
value, err := rc.conn.Get(key)
if err == nil {
return value
}
return nil
}
// GetMulti get value from memcache.
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)
2016-03-07 06:45:45 +00:00
resSize := len(res)
2016-03-06 05:17:16 +00:00
if err == nil {
2016-03-07 06:45:45 +00:00
for i := 1; i < resSize; i += 2 {
2017-03-17 17:45:30 +00:00
values = append(values, res[i+1])
2016-03-06 05:17:16 +00:00
}
return values
}
for i := 0; i < size; i++ {
values = append(values, err)
}
return values
}
// DelMulti get value from memcache.
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)
2017-03-17 17:24:45 +00:00
return err
2016-03-06 05:17:16 +00:00
}
// Put put value to memcache. only support string.
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")
}
// Delete delete value in memcache.
func (rc *Cache) Delete(key string) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
_, err := rc.conn.Del(key)
2017-03-17 17:24:45 +00:00
return err
2016-03-06 05:17:16 +00:00
}
// Incr increase counter.
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
}
// Decr decrease counter.
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
}
// IsExist check value exists in memcache.
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" {
2016-03-06 05:17:16 +00:00
return true
}
2016-03-07 06:45:45 +00:00
return false
2016-03-06 05:17:16 +00:00
}
// ClearAll clear all cached in memcache.
func (rc *Cache) ClearAll() error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
2016-03-07 06:45:45 +00:00
keyStart, keyEnd, limit := "", "", 50
resp, err := rc.Scan(keyStart, keyEnd, limit)
2016-03-06 05:17:16 +00:00
for err == nil {
size := len(resp)
if size == 1 {
return nil
}
keys := []string{}
for i := 1; i < size; i += 2 {
2017-03-17 17:45:30 +00:00
keys = append(keys, resp[i])
2016-03-06 05:17:16 +00:00
}
_, e := rc.conn.Do("multi_del", keys)
if e != nil {
return e
}
2016-03-07 06:45:45 +00:00
keyStart = resp[size-2]
resp, err = rc.Scan(keyStart, keyEnd, limit)
2016-03-06 05:17:16 +00:00
}
return err
}
// Scan key all cached in ssdb.
2016-03-07 06:45:45 +00:00
func (rc *Cache) Scan(keyStart string, keyEnd string, limit int) ([]string, error) {
2016-03-06 05:17:16 +00:00
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return nil, err
}
}
2016-03-07 06:45:45 +00:00
resp, err := rc.conn.Do("scan", keyStart, keyEnd, limit)
2016-03-06 05:17:16 +00:00
if err != nil {
return nil, err
}
return resp, nil
}
// StartAndGC start memcache adapter.
// config string is like {"conn":"connection info"}.
// if connecting error, return.
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 {
2016-03-07 06:45:45 +00:00
conninfoArray := strings.Split(rc.conninfo[0], ":")
host := conninfoArray[0]
port, e := strconv.Atoi(conninfoArray[1])
2016-03-06 05:17:16 +00:00
if e != nil {
return e
}
var err error
rc.conn, err = ssdb.Connect(host, port)
2017-03-17 17:24:45 +00:00
return err
2016-03-06 05:17:16 +00:00
}
func init() {
cache.Register("ssdb", NewSsdbCache)
}