mirror of
https://github.com/astaxie/beego.git
synced 2025-06-13 10:20:40 +00:00
remove pkg directory;
remove build directory; remove githook directory;
This commit is contained in:
230
client/cache/ssdb/ssdb.go
vendored
Normal file
230
client/cache/ssdb/ssdb.go
vendored
Normal file
@ -0,0 +1,230 @@
|
||||
package ssdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ssdb/gossdb/ssdb"
|
||||
|
||||
"github.com/astaxie/beego/client/cache"
|
||||
)
|
||||
|
||||
// Cache SSDB adapter
|
||||
type Cache struct {
|
||||
conn *ssdb.Client
|
||||
conninfo []string
|
||||
}
|
||||
|
||||
//NewSsdbCache creates new ssdb adapter.
|
||||
func NewSsdbCache() cache.Cache {
|
||||
return &Cache{}
|
||||
}
|
||||
|
||||
// Get gets a key's value from memcache.
|
||||
func (rc *Cache) Get(ctx context.Context, key string) (interface{}, error) {
|
||||
if rc.conn == nil {
|
||||
if err := rc.connectInit(); err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
value, err := rc.conn.Get(key)
|
||||
if err == nil {
|
||||
return value, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// GetMulti gets one or keys values from ssdb.
|
||||
func (rc *Cache) GetMulti(ctx context.Context, keys []string) ([]interface{}, error) {
|
||||
size := len(keys)
|
||||
var values []interface{}
|
||||
if rc.conn == nil {
|
||||
if err := rc.connectInit(); err != nil {
|
||||
return values, err
|
||||
}
|
||||
}
|
||||
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, nil
|
||||
}
|
||||
for i := 0; i < size; i++ {
|
||||
values = append(values, err)
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
// DelMulti deletes one or more keys 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)
|
||||
return err
|
||||
}
|
||||
|
||||
// Put puts value into memcache.
|
||||
// value: must be of type string
|
||||
func (rc *Cache) Put(ctx context.Context, key string, val interface{}, timeout time.Duration) error {
|
||||
if rc.conn == nil {
|
||||
if err := rc.connectInit(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
v, ok := val.(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 deletes a value in memcache.
|
||||
func (rc *Cache) Delete(ctx context.Context, key string) error {
|
||||
if rc.conn == nil {
|
||||
if err := rc.connectInit(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
_, err := rc.conn.Del(key)
|
||||
return err
|
||||
}
|
||||
|
||||
// Incr increases a key's counter.
|
||||
func (rc *Cache) Incr(ctx context.Context, 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 decrements a key's counter.
|
||||
func (rc *Cache) Decr(ctx context.Context, 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 checks if a key exists in memcache.
|
||||
func (rc *Cache) IsExist(ctx context.Context, key string) (bool, error) {
|
||||
if rc.conn == nil {
|
||||
if err := rc.connectInit(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
resp, err := rc.conn.Do("exists", key)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if len(resp) == 2 && resp[1] == "1" {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
|
||||
}
|
||||
|
||||
// ClearAll clears all cached items in memcache.
|
||||
func (rc *Cache) ClearAll(context.Context) 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
|
||||
}
|
||||
|
||||
// StartAndGC starts the memcache adapter.
|
||||
// config: must be in the format {"conn":"connection info"}.
|
||||
// If an error occurs during connection, an error is returned
|
||||
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)
|
||||
}
|
118
client/cache/ssdb/ssdb_test.go
vendored
Normal file
118
client/cache/ssdb/ssdb_test.go
vendored
Normal file
@ -0,0 +1,118 @@
|
||||
package ssdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/client/cache"
|
||||
)
|
||||
|
||||
func TestSsdbcacheCache(t *testing.T) {
|
||||
|
||||
ssdbAddr := os.Getenv("SSDB_ADDR")
|
||||
if ssdbAddr == "" {
|
||||
ssdbAddr = "127.0.0.1:8888"
|
||||
}
|
||||
|
||||
ssdb, err := cache.NewCache("ssdb", fmt.Sprintf(`{"conn": "%s"}`, ssdbAddr))
|
||||
if err != nil {
|
||||
t.Error("init err")
|
||||
}
|
||||
|
||||
// test put and exist
|
||||
if res, _ := ssdb.IsExist(context.Background(), "ssdb"); res {
|
||||
t.Error("check err")
|
||||
}
|
||||
timeoutDuration := 10 * time.Second
|
||||
// timeoutDuration := -10*time.Second if timeoutDuration is negtive,it means permanent
|
||||
if err = ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := ssdb.IsExist(context.Background(), "ssdb"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
|
||||
// Get test done
|
||||
if err = ssdb.Put(context.Background(), "ssdb", "ssdb", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
|
||||
if v, _ := ssdb.Get(context.Background(), "ssdb"); v != "ssdb" {
|
||||
t.Error("get Error")
|
||||
}
|
||||
|
||||
// inc/dec test done
|
||||
if err = ssdb.Put(context.Background(), "ssdb", "2", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if err = ssdb.Incr(context.Background(), "ssdb"); err != nil {
|
||||
t.Error("incr Error", err)
|
||||
}
|
||||
|
||||
val, _ := ssdb.Get(context.Background(), "ssdb")
|
||||
if v, err := strconv.Atoi(val.(string)); err != nil || v != 3 {
|
||||
t.Error("get err")
|
||||
}
|
||||
|
||||
if err = ssdb.Decr(context.Background(), "ssdb"); err != nil {
|
||||
t.Error("decr error")
|
||||
}
|
||||
|
||||
// test del
|
||||
if err = ssdb.Put(context.Background(), "ssdb", "3", timeoutDuration); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
|
||||
val, _ = ssdb.Get(context.Background(), "ssdb")
|
||||
if v, err := strconv.Atoi(val.(string)); err != nil || v != 3 {
|
||||
t.Error("get err")
|
||||
}
|
||||
if err := ssdb.Delete(context.Background(), "ssdb"); err == nil {
|
||||
if e, _ := ssdb.IsExist(context.Background(), "ssdb"); e {
|
||||
t.Error("delete err")
|
||||
}
|
||||
}
|
||||
|
||||
// test string
|
||||
if err = ssdb.Put(context.Background(), "ssdb", "ssdb", -10*time.Second); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := ssdb.IsExist(context.Background(), "ssdb"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
if v, _ := ssdb.Get(context.Background(), "ssdb"); v.(string) != "ssdb" {
|
||||
t.Error("get err")
|
||||
}
|
||||
|
||||
// test GetMulti done
|
||||
if err = ssdb.Put(context.Background(), "ssdb1", "ssdb1", -10*time.Second); err != nil {
|
||||
t.Error("set Error", err)
|
||||
}
|
||||
if res, _ := ssdb.IsExist(context.Background(), "ssdb1"); !res {
|
||||
t.Error("check err")
|
||||
}
|
||||
vv, _ := ssdb.GetMulti(context.Background(), []string{"ssdb", "ssdb1"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
if vv[0].(string) != "ssdb" {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
if vv[1].(string) != "ssdb1" {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
|
||||
// test clear all done
|
||||
if err = ssdb.ClearAll(context.Background()); err != nil {
|
||||
t.Error("clear all err")
|
||||
}
|
||||
e1, _ := ssdb.IsExist(context.Background(), "ssdb")
|
||||
e2, _ := ssdb.IsExist(context.Background(), "ssdb1")
|
||||
if e1 || e2 {
|
||||
t.Error("check err")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user