|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- package cache
-
- import (
- "errors"
- "fmt"
- "strconv"
- "time"
- )
-
- const (
- redisPassword = "sanhu"
- redisDialTTL = 10 * time.Second
- redisReadTTL = 3 * time.Second
- redisWriteTTL = 3 * time.Second
- redisIdleTTL = 10 * time.Second
- redisPoolTTL = 10 * time.Second
- redisPoolSize int = 512
- redisMaxIdleConn int = 64
- redisMaxActive int = 512
- )
-
- var (
- ErrNil = errors.New("nil return")
- ErrWrongArgsNum = errors.New("args num error")
- ErrNegativeInt = errors.New("redis cluster: unexpected value for Uint64")
- )
-
- // 以下为提供类型转换
-
- func Int(reply interface{}, err error) (int, error) {
- if err != nil {
- return 0, err
- }
- switch reply := reply.(type) {
- case int:
- return reply, nil
- case int8:
- return int(reply), nil
- case int16:
- return int(reply), nil
- case int32:
- return int(reply), nil
- case int64:
- x := int(reply)
- if int64(x) != reply {
- return 0, strconv.ErrRange
- }
- return x, nil
- case uint:
- n := int(reply)
- if n < 0 {
- return 0, strconv.ErrRange
- }
- return n, nil
- case uint8:
- return int(reply), nil
- case uint16:
- return int(reply), nil
- case uint32:
- n := int(reply)
- if n < 0 {
- return 0, strconv.ErrRange
- }
- return n, nil
- case uint64:
- n := int(reply)
- if n < 0 {
- return 0, strconv.ErrRange
- }
- return n, nil
- case []byte:
- data := string(reply)
- if len(data) == 0 {
- return 0, ErrNil
- }
-
- n, err := strconv.ParseInt(data, 10, 0)
- return int(n), err
- case string:
- if len(reply) == 0 {
- return 0, ErrNil
- }
-
- n, err := strconv.ParseInt(reply, 10, 0)
- return int(n), err
- case nil:
- return 0, ErrNil
- case error:
- return 0, reply
- }
- return 0, fmt.Errorf("redis cluster: unexpected type for Int, got type %T", reply)
- }
-
- func Int64(reply interface{}, err error) (int64, error) {
- if err != nil {
- return 0, err
- }
- switch reply := reply.(type) {
- case int:
- return int64(reply), nil
- case int8:
- return int64(reply), nil
- case int16:
- return int64(reply), nil
- case int32:
- return int64(reply), nil
- case int64:
- return reply, nil
- case uint:
- n := int64(reply)
- if n < 0 {
- return 0, strconv.ErrRange
- }
- return n, nil
- case uint8:
- return int64(reply), nil
- case uint16:
- return int64(reply), nil
- case uint32:
- return int64(reply), nil
- case uint64:
- n := int64(reply)
- if n < 0 {
- return 0, strconv.ErrRange
- }
- return n, nil
- case []byte:
- data := string(reply)
- if len(data) == 0 {
- return 0, ErrNil
- }
-
- n, err := strconv.ParseInt(data, 10, 64)
- return n, err
- case string:
- if len(reply) == 0 {
- return 0, ErrNil
- }
-
- n, err := strconv.ParseInt(reply, 10, 64)
- return n, err
- case nil:
- return 0, ErrNil
- case error:
- return 0, reply
- }
- return 0, fmt.Errorf("redis cluster: unexpected type for Int64, got type %T", reply)
- }
-
- func Uint64(reply interface{}, err error) (uint64, error) {
- if err != nil {
- return 0, err
- }
- switch reply := reply.(type) {
- case uint:
- return uint64(reply), nil
- case uint8:
- return uint64(reply), nil
- case uint16:
- return uint64(reply), nil
- case uint32:
- return uint64(reply), nil
- case uint64:
- return reply, nil
- case int:
- if reply < 0 {
- return 0, ErrNegativeInt
- }
- return uint64(reply), nil
- case int8:
- if reply < 0 {
- return 0, ErrNegativeInt
- }
- return uint64(reply), nil
- case int16:
- if reply < 0 {
- return 0, ErrNegativeInt
- }
- return uint64(reply), nil
- case int32:
- if reply < 0 {
- return 0, ErrNegativeInt
- }
- return uint64(reply), nil
- case int64:
- if reply < 0 {
- return 0, ErrNegativeInt
- }
- return uint64(reply), nil
- case []byte:
- data := string(reply)
- if len(data) == 0 {
- return 0, ErrNil
- }
-
- n, err := strconv.ParseUint(data, 10, 64)
- return n, err
- case string:
- if len(reply) == 0 {
- return 0, ErrNil
- }
-
- n, err := strconv.ParseUint(reply, 10, 64)
- return n, err
- case nil:
- return 0, ErrNil
- case error:
- return 0, reply
- }
- return 0, fmt.Errorf("redis cluster: unexpected type for Uint64, got type %T", reply)
- }
-
- func Float64(reply interface{}, err error) (float64, error) {
- if err != nil {
- return 0, err
- }
-
- var value float64
- err = nil
- switch v := reply.(type) {
- case float32:
- value = float64(v)
- case float64:
- value = v
- case int:
- value = float64(v)
- case int8:
- value = float64(v)
- case int16:
- value = float64(v)
- case int32:
- value = float64(v)
- case int64:
- value = float64(v)
- case uint:
- value = float64(v)
- case uint8:
- value = float64(v)
- case uint16:
- value = float64(v)
- case uint32:
- value = float64(v)
- case uint64:
- value = float64(v)
- case []byte:
- data := string(v)
- if len(data) == 0 {
- return 0, ErrNil
- }
- value, err = strconv.ParseFloat(string(v), 64)
- case string:
- if len(v) == 0 {
- return 0, ErrNil
- }
- value, err = strconv.ParseFloat(v, 64)
- case nil:
- err = ErrNil
- case error:
- err = v
- default:
- err = fmt.Errorf("redis cluster: unexpected type for Float64, got type %T", v)
- }
-
- return value, err
- }
-
- func Bool(reply interface{}, err error) (bool, error) {
- if err != nil {
- return false, err
- }
- switch reply := reply.(type) {
- case bool:
- return reply, nil
- case int64:
- return reply != 0, nil
- case []byte:
- data := string(reply)
- if len(data) == 0 {
- return false, ErrNil
- }
-
- return strconv.ParseBool(data)
- case string:
- if len(reply) == 0 {
- return false, ErrNil
- }
-
- return strconv.ParseBool(reply)
- case nil:
- return false, ErrNil
- case error:
- return false, reply
- }
- return false, fmt.Errorf("redis cluster: unexpected type for Bool, got type %T", reply)
- }
-
- func Bytes(reply interface{}, err error) ([]byte, error) {
- if err != nil {
- return nil, err
- }
- switch reply := reply.(type) {
- case []byte:
- if len(reply) == 0 {
- return nil, ErrNil
- }
- return reply, nil
- case string:
- data := []byte(reply)
- if len(data) == 0 {
- return nil, ErrNil
- }
- return data, nil
- case nil:
- return nil, ErrNil
- case error:
- return nil, reply
- }
- return nil, fmt.Errorf("redis cluster: unexpected type for Bytes, got type %T", reply)
- }
-
- func String(reply interface{}, err error) (string, error) {
- if err != nil {
- return "", err
- }
-
- value := ""
- err = nil
- switch v := reply.(type) {
- case string:
- if len(v) == 0 {
- return "", ErrNil
- }
-
- value = v
- case []byte:
- if len(v) == 0 {
- return "", ErrNil
- }
-
- value = string(v)
- case int:
- value = strconv.FormatInt(int64(v), 10)
- case int8:
- value = strconv.FormatInt(int64(v), 10)
- case int16:
- value = strconv.FormatInt(int64(v), 10)
- case int32:
- value = strconv.FormatInt(int64(v), 10)
- case int64:
- value = strconv.FormatInt(v, 10)
- case uint:
- value = strconv.FormatUint(uint64(v), 10)
- case uint8:
- value = strconv.FormatUint(uint64(v), 10)
- case uint16:
- value = strconv.FormatUint(uint64(v), 10)
- case uint32:
- value = strconv.FormatUint(uint64(v), 10)
- case uint64:
- value = strconv.FormatUint(v, 10)
- case float32:
- value = strconv.FormatFloat(float64(v), 'f', -1, 32)
- case float64:
- value = strconv.FormatFloat(v, 'f', -1, 64)
- case bool:
- value = strconv.FormatBool(v)
- case nil:
- err = ErrNil
- case error:
- err = v
- default:
- err = fmt.Errorf("redis cluster: unexpected type for String, got type %T", v)
- }
-
- return value, err
- }
-
- func Strings(reply interface{}, err error) ([]string, error) {
- if err != nil {
- return nil, err
- }
- switch reply := reply.(type) {
- case []interface{}:
- result := make([]string, len(reply))
- for i := range reply {
- if reply[i] == nil {
- continue
- }
- switch subReply := reply[i].(type) {
- case string:
- result[i] = subReply
- case []byte:
- result[i] = string(subReply)
- default:
- return nil, fmt.Errorf("redis cluster: unexpected element type for String, got type %T", reply[i])
- }
- }
- return result, nil
- case []string:
- return reply, nil
- case nil:
- return nil, ErrNil
- case error:
- return nil, reply
- }
- return nil, fmt.Errorf("redis cluster: unexpected type for Strings, got type %T", reply)
- }
-
- func Values(reply interface{}, err error) ([]interface{}, error) {
- if err != nil {
- return nil, err
- }
- switch reply := reply.(type) {
- case []interface{}:
- return reply, nil
- case nil:
- return nil, ErrNil
- case error:
- return nil, reply
- }
- return nil, fmt.Errorf("redis cluster: unexpected type for Values, got type %T", reply)
- }
|