Documentation ¶
Overview ¶
Example ¶
go test -run Example
package main import ( "fmt" "time" "github.com/xcltapestry/golibs/driver/redisx" ) func connRedis() (*redisx.Client, error) { rdb := redisx.NewClient(&redisx.Options{ Addr: ":6379", DialTimeout: 10 * time.Second, ReadTimeout: 30 * time.Second, WriteTimeout: 30 * time.Second, PoolSize: 10, PoolTimeout: 30 * time.Second, }) err := rdb.Ping() if err != nil { return nil, err } return rdb, nil } func main() { rdb, err := connRedis() if err != nil { fmt.Println(err.Error()) return } defer rdb.Close() // -- v2, err2 := rdb.Set("key3", "aaaa", 1*time.Second) fmt.Println("[cmd-Set] v:", v2, " err:", err2) v3, err3 := rdb.SetEX("key3-ex", "aaaa", 1*time.Second) fmt.Println("[cmd-SetEX] v:", v3, " err:", err3) v4, err4 := rdb.SetNX("key3-nx", "aaaa", 1*time.Second) fmt.Println("[cmd-SetNX] v:", v4, " err:", err4) // -- ttlv5, ttlerr5 := rdb.TTL("key3-nx") fmt.Println("[cmd-Expire-TTL-1] ttlv5:", ttlv5, " ttlerr5:", ttlerr5) v5, err5 := rdb.Expire("key3-nx", 5*time.Second) fmt.Println("[cmd-Expire] v:", v5, " err:", err5) ttlv5, ttlerr5 = rdb.TTL("key3-nx") fmt.Println("[cmd-Expire-TTL-2] ttlv5:", ttlv5, " ttlerr5:", ttlerr5) // -- v, err := rdb.Exists("key2") fmt.Println("[cmd-Exists3] v:", v, " err:", err) v, err = rdb.Exists("key2", "key3") fmt.Println("[cmd-Exists1] v:", v, " err:", err) v, err = rdb.Exists("key3") fmt.Println("[cmd-Exists2] v:", v, " err:", err) kval, kerr := rdb.Get("key3") fmt.Println("[cmd-Get] kval:", kval, " err:", kerr) ttlv, ttlerr := rdb.TTL("key3") fmt.Println("[cmd-TTL] ttlv:", ttlv, " ttlerr:", ttlerr) // -- crv, crerr := rdb.Incr("keyCR") fmt.Println("[cmd-Incr] crv:", crv, " crerr:", crerr) crv, crerr = rdb.Decr("keyCR") fmt.Println("[cmd-Decr] crv:", crv, " crerr:", crerr) crv, crerr = rdb.Incr("keyCR") fmt.Println("[cmd-Incr] crv:", crv, " crerr:", crerr) }
Output:
Index ¶
- type Client
- func (rc *Client) Append(key, value string) (int64, error)
- func (rc *Client) Close() error
- func (rc *Client) Decr(key string) (int64, error)
- func (rc *Client) Del(keys ...string) (int64, error)
- func (rc *Client) Exists(keys ...string) (int64, error)
- func (rc *Client) Expire(key string, expiration time.Duration) (bool, error)
- func (rc *Client) Get(key string) (string, error)
- func (rc *Client) Incr(key string) (int64, error)
- func (rc *Client) LRange(key string, start, stop int64) ([]string, error)
- func (rc *Client) NewLocker() *Lock
- func (rc *Client) PSubscribe(channel string, message interface{}) (*redis.PubSub, error)
- func (rc *Client) Ping() error
- func (rc *Client) Publish(channel string, message interface{}) (int64, error)
- func (rc *Client) Set(key string, value interface{}, expiration time.Duration) (string, error)
- func (rc *Client) SetEX(key string, value interface{}, expiration time.Duration) (string, error)
- func (rc *Client) SetNX(key string, value interface{}, expiration time.Duration) (bool, error)
- func (rc *Client) TTL(key string) (time.Duration, error)
- type Lock
- func (lk *Lock) ExponentialBackoff(min, max time.Duration)
- func (lk *Lock) Key() string
- func (lk *Lock) LimitRetry(backoff time.Duration, max int)
- func (lk *Lock) LinearBackoff(backoff time.Duration)
- func (lk *Lock) Metadata() string
- func (lk *Lock) Obtain(ctx context.Context, key string, ttl time.Duration) error
- func (lk *Lock) Refresh(ctx context.Context, ttl time.Duration) error
- func (lk *Lock) Release(ctx context.Context) error
- func (lk *Lock) TTL(ctx context.Context) (time.Duration, error)
- func (lk *Lock) Token() string
- type Options
- type Redis
- type RedisLock
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func (*Client) PSubscribe ¶
type Lock ¶
type Lock struct {
// contains filtered or unexported fields
}
Lock 分布式锁
func (*Lock) ExponentialBackoff ¶
func (*Lock) Key ¶
//////////////////////////////////////////
Lock 相关函数
//////////////////////////////////////////
func (*Lock) LinearBackoff ¶
type Redis ¶
type Redis interface { Close() error Ping() (int64, error) Exists(keys ...string) (int64, error) Append(key, value string) (int64, error) Set(key string, value interface{}, expiration time.Duration) (string, error) SetEX(key string, value interface{}, expiration time.Duration) (string, error) SetNX(key string, value interface{}, expiration time.Duration) (bool, error) Get(key string) (string, error) Del(key string) (int64, error) TTL(key string) (time.Duration, error) Expire(key string, expiration time.Duration) (bool, error) Incr(key string) (int64, error) Decr(key string) (int64, error) LRange(key string, start, stop int64) ([]string, error) Publish(channel string, message interface{}) (int64, error) PSubscribe(channel string, message interface{}) (*redis.PubSub, error) NewLocker() *RedisLock //分布式锁 }
type RedisLock ¶
type RedisLock interface { LinearBackoff(backoff time.Duration) LimitRetry(backoff time.Duration, max int) ExponentialBackoff(min, max time.Duration) Key() string Token() string Metadata() string TTL(ctx context.Context) (time.Duration, error) Refresh(ctx context.Context, ttl time.Duration) Release(ctx context.Context) error }
RedisLock 分布式锁
Click to show internal directories.
Click to hide internal directories.