redis

package
v1.4.5 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2020 License: MIT Imports: 6 Imported by: 0

README

redis

初始化

只支持单例 redis

import "github.com/go-eyas/toolkit/redis"

func main() {
  // 使用前必须先初始化
  r, err := redis.Init(&redis.Config{
    Cluster:  false, // 是否集群
    Addrs:    []string{"10.0.3.252:6379"}, // redis 地址,如果是集群则在数组上写多个元素
    Password: "",
		DB:       1,
  })
  if err != nil {
    panic(err)
  }

  err = r.Set("tookit:test", `{"hello": "world"}`)

  v, err = r.Get("tookit:test")
  fmt.Printf("v: %s", v) // v: {"hello": "world"}

  err = redis.Del("tookit:test")


  redis.Expire("tookit:test", time.Hour * 24)

  redis.Redis // *redis.RedisClient
  redis.Client // *github.com/go-redis/redis.Client
}

godoc

API 文档

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RedisTTL = time.Hour * 24

RedisTTL 默认有效期 24 小时

Functions

This section is empty.

Types

type Config

type Config struct {
	Cluster  bool
	Addrs    []string
	Password string
	DB       int
	Prefix   string
}

type Message

type Message struct {
	Channel string
	Pattern string
	Payload string
}

func (*Message) JSON

func (msg *Message) JSON(v interface{}) error

JSON 绑定json对象

type RedisClient

type RedisClient struct {
	Namespace string
	Client    redis.UniversalClient
	Prefix    string
	// contains filtered or unexported fields
}

RedisClient redis client wrapper

func New

func New(redisConf *Config) (*RedisClient, error)

Init 初始化redis

func (*RedisClient) Close

func (r *RedisClient) Close()

Close 关闭redis连接

func (*RedisClient) Del

func (r *RedisClient) Del(keys ...string) error

Del 删除键

func (*RedisClient) Expire

func (r *RedisClient) Expire(key string, expiration time.Duration) (bool, error)

func (*RedisClient) Get

func (r *RedisClient) Get(key string) (string, error)

Get 获取字符串值

func (*RedisClient) HDel

func (r *RedisClient) HDel(key string, field ...string) error

HDel 删除hash的键

func (*RedisClient) HGet

func (r *RedisClient) HGet(key string, field string) (string, error)

HGet 获取 Hash 的字段值

func (*RedisClient) HGetAll

func (r *RedisClient) HGetAll(key string) (map[string]string, error)

HGetAll 获取 Hash 的所有字段

func (*RedisClient) HSet

func (r *RedisClient) HSet(key, field string, val interface{}, expiration ...time.Duration) error

HSet 设置hash值

func (*RedisClient) Pub

func (r *RedisClient) Pub(channel string, msg string) error

Pub 发布事件 example: Redis.Pub("chat", "this is a test message")

func (*RedisClient) Set

func (r *RedisClient) Set(key string, value interface{}, expiration ...time.Duration) error

Set 设置字符串值,有效期默认 24 小时

func (*RedisClient) Sub

func (r *RedisClient) Sub(channel string, handler func(*Message))

Sub 监听通道,有数据时触发回调 handler example:

redis.Sub("chat")(func(msg *redis.Message) {
	fmt.Printf("receive message: %#v", msg)
})

Jump to

Keyboard shortcuts

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