redis

package module
v0.9.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2022 License: Apache-2.0 Imports: 17 Imported by: 4

README

REDIS

基于 fns.Service 实现的内部 Redis 服务。

安装

go get github.com/aacfactory/fns-contrib/databases/redis

使用

配置文件

  • 单机
    • masterSlaverMode = false,addr 列表为一个元素。
  • 主从
    • masterSlaverMode = true,addr 列表第一个元素为主服务地址,后续为从服务地址。
  • 集群
    • masterSlaverMode = false,addr 列表多元素。
{
  "redis": {
    "masterSlaverMode": false,
    "network": "tcp",
    "addr": [
      "ip:port"
    ],
    "username": "",
    "password": "",
    "db": 0,
    "poolSize": 0,
    "ssl": false,
    "caFilePath": "",
    "certFilePath": "",
    "keyFilePath": "",
    "insecureSkipVerify": false
  }
}

服务部署

  • fns为单机模式
    • 直接部署
  • fns为分布式模式
    • 可以单独起一个(一组)只有 redis 服务的应用(推荐)。
    • 也可以与fns单机模式一样使用。
app.Deply(redis.Service())

服务使用,具体参见 github.com/aacfactory/fns-contrib/databases/redis/proxy.go

// get
result, err := redis.Get(ctx, key)
// set
err := redis.Set(ctx, &redis.SetParam{})


注意事项

  • 它是一个内部服务,即只能被fn访问。
  • 该服务主要服务于缓存,故只实现 key、strings、sorted set 的部分功能,如需要其它功能,请使用 Do 函数。
  • 它是由 go-redis 实现,其 Do 函数是代理的 go-redis 中的 Do 函数,参数与返回值请参见 go-redis。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CacheGetWithSingleFight added in v0.6.0

func CacheGetWithSingleFight(ctx fns.Context, key string, timeout time.Duration, fn func() (result json.RawMessage, err errors.CodeError)) (result json.RawMessage, err errors.CodeError)

func CacheRem added in v0.6.0

func CacheRem(ctx fns.Context, key string) (err errors.CodeError)

func CacheSet added in v0.6.0

func CacheSet(ctx fns.Context, key string, timeout time.Duration, value json.RawMessage) (err errors.CodeError)

func Contains

func Contains(ctx fns.Context, key string) (ok bool, err errors.CodeError)

func Decr

func Decr(ctx fns.Context, key string) (v int64, err errors.CodeError)

func Expire

func Expire(ctx fns.Context, param ExpireParam) (ok bool, err errors.CodeError)

func Incr

func Incr(ctx fns.Context, key string) (v int64, err errors.CodeError)

func Lock

func Lock(ctx fns.Context, param LockParam) (err errors.CodeError)

func Persist

func Persist(ctx fns.Context, key string) (ok bool, err errors.CodeError)

func Remove

func Remove(ctx fns.Context, key string) (err errors.CodeError)

func Service

func Service() fns.Service

func Set

func Set(ctx fns.Context, param SetParam) (err errors.CodeError)

func TTL

func TTL(ctx fns.Context, key string) (ttl time.Duration, err errors.CodeError)

func Unlock

func Unlock(ctx fns.Context, key string) (err errors.CodeError)

func ZAdd

func ZAdd(ctx fns.Context, param ZAddParam) (err errors.CodeError)

func ZCard

func ZCard(ctx fns.Context, key string) (num int64, err errors.CodeError)

func ZRange

func ZRange(ctx fns.Context, param ZRangeParam) (result *json.Array, err errors.CodeError)

func ZRem

func ZRem(ctx fns.Context, param ZRemParam) (ok bool, err errors.CodeError)

func ZRemByRank

func ZRemByRank(ctx fns.Context, param ZRemByRankParam) (ok bool, err errors.CodeError)

func ZRemByScore

func ZRemByScore(ctx fns.Context, param ZRemByScoreParam) (ok bool, err errors.CodeError)

func ZRevRange

func ZRevRange(ctx fns.Context, param ZRevRangeParam) (result *json.Array, err errors.CodeError)

func ZRevRangeByScore

func ZRevRangeByScore(ctx fns.Context, param ZRevRangeByScoreParam) (result *json.Array, err errors.CodeError)

Types

type Client

type Client interface {
	Do(ctx context.Context, args ...interface{}) *rds.Cmd
	Writer() (cmd rds.Cmdable)
	Reader() (cmd rds.Cmdable)
	Close() (err error)
}

type Cluster

type Cluster struct {
	// contains filtered or unexported fields
}

func (*Cluster) Close

func (client *Cluster) Close() (err error)

func (*Cluster) Do

func (client *Cluster) Do(ctx context.Context, args ...interface{}) *rds.Cmd

func (*Cluster) Reader

func (client *Cluster) Reader() (cmd rds.Cmdable)

func (*Cluster) Writer

func (client *Cluster) Writer() (cmd rds.Cmdable)

type Config

type Config struct {
	MasterSlaverMode   bool     `json:"masterSlaverMode,omitempty"`
	Network            string   `json:"network,omitempty"`
	Addr               []string `json:"addr,omitempty"`
	Username           string   `json:"username,omitempty"`
	Password           string   `json:"password,omitempty"`
	DB                 int      `json:"db,omitempty"`
	PoolSize           int      `json:"poolSize,omitempty"`
	SSL                bool     `json:"ssl,omitempty"`
	CaFilePath         string   `json:"caFilePath,omitempty"`
	CertFilePath       string   `json:"certFilePath,omitempty"`
	KeyFilePath        string   `json:"keyFilePath,omitempty"`
	InsecureSkipVerify bool     `json:"insecureSkipVerify,omitempty"`
}

func (*Config) CreateClient

func (config *Config) CreateClient() (client Client, err error)

func (*Config) LoadSSL

func (config *Config) LoadSSL() (ssl *tls.Config, err error)

type ExpireParam

type ExpireParam struct {
	Key string        `json:"key,omitempty"`
	TTL time.Duration `json:"ttl,omitempty"`
}

type GetResult added in v0.6.0

type GetResult struct {
	Value json.RawMessage `json:"value,omitempty"`
	Has   bool            `json:"has,omitempty"`
}

func CacheGet added in v0.6.0

func CacheGet(ctx fns.Context, key string) (result *GetResult, err errors.CodeError)

func Get

func Get(ctx fns.Context, key string) (result *GetResult, err errors.CodeError)

func GetSet added in v0.6.0

func GetSet(ctx fns.Context, param SetParam) (result *GetResult, err errors.CodeError)

type LockParam

type LockParam struct {
	Key string        `json:"key,omitempty"`
	TTL time.Duration `json:"ttl,omitempty"`
}

type MasterSlaver

type MasterSlaver struct {
	// contains filtered or unexported fields
}

func (*MasterSlaver) Close

func (client *MasterSlaver) Close() (err error)

func (*MasterSlaver) Do

func (client *MasterSlaver) Do(ctx context.Context, args ...interface{}) *rds.Cmd

func (*MasterSlaver) Reader

func (client *MasterSlaver) Reader() (cmd rds.Cmdable)

func (*MasterSlaver) Writer

func (client *MasterSlaver) Writer() (cmd rds.Cmdable)

type OriginArgs

type OriginArgs struct {
	// contains filtered or unexported fields
}

func (*OriginArgs) Append

func (args *OriginArgs) Append(v interface{}) (err error)

func (*OriginArgs) MarshalJSON

func (args *OriginArgs) MarshalJSON() (p []byte, err error)

func (*OriginArgs) UnmarshalJSON

func (args *OriginArgs) UnmarshalJSON(p []byte) (err error)

type OriginCommandArg

type OriginCommandArg struct {
	Name string     `json:"name,omitempty"`
	Args OriginArgs `json:"args,omitempty"`
}

func NewOriginCommandArg

func NewOriginCommandArg(name string) *OriginCommandArg

func (*OriginCommandArg) AppendParams

func (arg *OriginCommandArg) AppendParams(params ...interface{}) (err error)

func (*OriginCommandArg) MapToRedisDoArgs

func (arg *OriginCommandArg) MapToRedisDoArgs() (v []interface{}, err error)

type OriginCommandResult

type OriginCommandResult json.RawMessage

func Do

func Do(ctx fns.Context, param OriginCommandArg) (result OriginCommandResult, err errors.CodeError)

func (OriginCommandResult) As

func (result OriginCommandResult) As(v interface{}) (err error)

As map to github.com/go-redis/redis/v8 result value

type SetParam

type SetParam struct {
	Key        string          `json:"key,omitempty"`
	Value      json.RawMessage `json:"value,omitempty"`
	Expiration time.Duration   `json:"expiration,omitempty"`
}

type Standalone

type Standalone struct {
	// contains filtered or unexported fields
}

func (*Standalone) Close

func (client *Standalone) Close() (err error)

func (*Standalone) Do

func (client *Standalone) Do(ctx context.Context, args ...interface{}) *rds.Cmd

func (*Standalone) Reader

func (client *Standalone) Reader() (cmd rds.Cmdable)

func (*Standalone) Writer

func (client *Standalone) Writer() (cmd rds.Cmdable)

type ZAddParam

type ZAddParam struct {
	Key   string          `json:"key,omitempty"`
	Score float64         `json:"score,omitempty"`
	Value json.RawMessage `json:"value,omitempty"`
}

type ZRangeByScoreParam

type ZRangeByScoreParam struct {
	Key    string `json:"key,omitempty"`
	Min    string `json:"min,omitempty"`
	Max    string `json:"max,omitempty"`
	Offset int64  `json:"offset,omitempty"`
	Count  int64  `json:"count,omitempty"`
}

type ZRangeParam

type ZRangeParam struct {
	Key   string `json:"key,omitempty"`
	Start int64  `json:"start,omitempty"`
	Stop  int64  `json:"stop,omitempty"`
}

type ZRemByRankParam

type ZRemByRankParam struct {
	Key   string `json:"key,omitempty"`
	Start int64  `json:"start,omitempty"`
	Stop  int64  `json:"stop,omitempty"`
}

type ZRemByScoreParam

type ZRemByScoreParam struct {
	Key string `json:"key,omitempty"`
	Min string `json:"min,omitempty"`
	Max string `json:"max,omitempty"`
}

type ZRemParam

type ZRemParam struct {
	Key    string          `json:"key,omitempty"`
	Member json.RawMessage `json:"min,omitempty"`
}

type ZRevRangeByScoreParam

type ZRevRangeByScoreParam struct {
	Key    string `json:"key,omitempty"`
	Min    string `json:"min,omitempty"`
	Max    string `json:"max,omitempty"`
	Offset int64  `json:"offset,omitempty"`
	Count  int64  `json:"count,omitempty"`
}

type ZRevRangeParam

type ZRevRangeParam struct {
	Key   string `json:"key,omitempty"`
	Start int64  `json:"start,omitempty"`
	Stop  int64  `json:"stop,omitempty"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL