redisutil

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2022 License: MIT Imports: 7 Imported by: 0

README

redis-util

方便业务使用的redis操作封装,比如常见的缓存set get操作, 一行代码搞定,不像开源库需要写好多行

使用方法

package main

import (
	"fmt"

	redisutil "github.com/cclehui/redis-util"
	"github.com/gomodule/redigo/redis"
)

func main() {
	server := "xxxxx:6379"
	password := "wxxxxxxxxx"

	redisClient := &redis.Pool{
		Dial: func() (redis.Conn, error) {
			c, err := redis.Dial("tcp", server)
			if err != nil {
				return nil, err
			}

			if _, err := c.Do("AUTH", password); err != nil {
				c.Close()
				return nil, err
			}

			return c, nil
		},
	}

	redisUtil := redisutil.NewRedisUtil(redisClient)
	cacheKey := "cclehui_test_set_get_key_211022"

	_ = redisUtil.Set(cacheKey, "axxxaa", 3600) // 设置缓存

	valueStrRes := ""
	_, _ = redisUtil.Get(cacheKey, &valueStrRes) // 获取缓存
	fmt.Println("获取缓存:", valueStrRes)

	_ = redisUtil.Del(cacheKey) // Del

	value, _ := redisUtil.Incr(cacheKey)
	fmt.Println("Incr:", value)

	value, _ = redisUtil.Decr(cacheKey)
	fmt.Println("Decr:", value)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidNum = errors.New("Invalid num")

Functions

func Decode

func Decode(b []byte, ptr interface{}) error

Decode将Encode的结果重新赋值给ptr指向的类型

func Encode

func Encode(val interface{}) ([]byte, error)

Encode将任意类型编码为[]byte类型

Types

type RedisUtil

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

func NewRedisUtil

func NewRedisUtil(pool *redis.Pool) *RedisUtil

func (*RedisUtil) Decr

func (ru *RedisUtil) Decr(ctx context.Context, key string) (res int64, err error)

func (*RedisUtil) DecrBy

func (ru *RedisUtil) DecrBy(ctx context.Context, key string, diff int64) (res int64, err error)

func (*RedisUtil) Del

func (ru *RedisUtil) Del(ctx context.Context, key string) (err error)

func (*RedisUtil) Expire

func (ru *RedisUtil) Expire(ctx context.Context, key string, ttl int) (err error)

func (*RedisUtil) Get

func (ru *RedisUtil) Get(ctx context.Context, key string, value interface{}) (hit bool, err error)

func (*RedisUtil) Incr

func (ru *RedisUtil) Incr(ctx context.Context, key string) (res int64, err error)

func (*RedisUtil) IncrBy

func (ru *RedisUtil) IncrBy(ctx context.Context, key string, diff int64) (res int64, err error)

func (*RedisUtil) Set

func (ru *RedisUtil) Set(ctx context.Context, key string, value interface{}, ttl int) (err error)

func (*RedisUtil) TTL

func (ru *RedisUtil) TTL(ctx context.Context, key string) (ttl int, err error)

func (*RedisUtil) WrapDo

func (ru *RedisUtil) WrapDo(ctx context.Context, doFunction func(con redis.Conn) error) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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