- auth for weixin.
This commit is contained in:
60
business/jxutils/cache/redis/redis.go
vendored
Normal file
60
business/jxutils/cache/redis/redis.go
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"github.com/go-redis/redis"
|
||||
)
|
||||
|
||||
type Cacher struct {
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
func New(host string, port int, password string) *Cacher {
|
||||
if port == 0 {
|
||||
port = 6379
|
||||
}
|
||||
return &Cacher{
|
||||
client: redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%d", host, port),
|
||||
Password: password,
|
||||
},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cacher) FlushAll() error {
|
||||
return c.client.FlushAll().Err()
|
||||
}
|
||||
|
||||
func (c *Cacher) Set(key string, value interface{}, expiration time.Duration) error {
|
||||
strValue := string(utils.MustMarshal(value))
|
||||
return c.client.Set(key, strValue, expiration).Err()
|
||||
}
|
||||
|
||||
func (c *Cacher) Del(key string) error {
|
||||
return c.client.Del(key).Err()
|
||||
}
|
||||
|
||||
func (c *Cacher) Get(key string) interface{} {
|
||||
result, err := c.client.Get(key).Result()
|
||||
if err == nil {
|
||||
var retVal interface{}
|
||||
if err = utils.UnmarshalUseNumber([]byte(result), &retVal); err == nil {
|
||||
return retVal
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cacher) GetAs(key string, ptr interface{}) error {
|
||||
result, err := c.client.Get(key).Result()
|
||||
if err == nil {
|
||||
if err = utils.UnmarshalUseNumber([]byte(result), ptr); err == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
48
business/jxutils/cache/redis/redis_test.go
vendored
Normal file
48
business/jxutils/cache/redis/redis_test.go
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
"git.rosy.net.cn/jx-callback/globals/beegodb"
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
func init() {
|
||||
//E:/goprojects/src/git.rosy.net.cn/jx-callback/conf/app.conf
|
||||
///Users/xujianhua/go/src/git.rosy.net.cn/jx-callback/conf/app.conf
|
||||
beego.InitBeegoBeforeTest("/Users/xujianhua/go/src/git.rosy.net.cn/jx-callback/conf/app.conf")
|
||||
beego.BConfig.RunMode = "dev" // InitBeegoBeforeTest会将runmode设置为test
|
||||
|
||||
globals.Init()
|
||||
beegodb.Init()
|
||||
api.Init()
|
||||
}
|
||||
|
||||
type FuckYou struct {
|
||||
Key string `json:"key"`
|
||||
}
|
||||
|
||||
func TestIt(t *testing.T) {
|
||||
cacher := New("localhost", 6379, "")
|
||||
cacher.FlushAll()
|
||||
if cacher.Get("key") != nil {
|
||||
t.Fatal("should get nothing")
|
||||
}
|
||||
if err := cacher.Set("key", map[string]interface{}{
|
||||
"key": "value",
|
||||
}, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if cacher.Get("key") == nil {
|
||||
t.Fatal("should not get nothing")
|
||||
}
|
||||
|
||||
result := new(FuckYou)
|
||||
cacher.GetAs("key", result)
|
||||
if result.Key != "value" {
|
||||
t.Fatal("should get value")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user