redis_full

package module
v0.0.0-...-1d34869 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2019 License: MIT Imports: 7 Imported by: 4

README

redis-full wercker status Image of license

all of the redis method

DEV Version

  • Redis version: 3.2.12
  • Go version: 1.11

How to install

Use go get to install or upgrade (-u) the redis-full package.

go get -u github.com/yangfei4913438/redis-full

Usage

Like on the command line using redis to use it!

- use in beego

1)add a init file


package dbs

import (
	"github.com/astaxie/beego"
	redis "github.com/yangfei4913438/redis-full"
	"strings"
	"time"
)

//redis对外接口
var RedisDB redis.RedisCache

func initRedis() {
	hosts := beego.AppConfig.String("redis.host")
	password := beego.AppConfig.DefaultString("redis.password", "")
	database := beego.AppConfig.DefaultInt("redis.db", 0)
	MaxIdle := beego.AppConfig.DefaultInt("redis.maxidle", 100)
	MaxActive := beego.AppConfig.DefaultInt("redis.maxactive", 1000)
	IdleTimeout := time.Duration(beego.AppConfig.DefaultInt("redis.idletimeout", 600)) * time.Second

	//通过赋值对外接口来使用
	RedisDB = redis.NewRedisCache(hosts, password, database, MaxIdle, MaxActive, IdleTimeout, 24*time.Hour)

	if err := RedisDB.CheckRedis(); err != nil {
		panic("Redis Server:" + hosts + " Connect failed: " + err.Error() + "!")
	} else {
		beego.Info("Connect Redis Server(" + hosts + ") to successful!")
	}
}

  1. register to init.go

package dbs

func init() {
	initMysql()
	initRedis()
}

  1. use it! so easy!

For Example, a model file:


package models

import (
	"github.com/astaxie/beego"
	"strconv"
	"strings"
	"testapi/dbs"
	"testapi/tools"
)

// 用户表结构体,用于接收数据库查询出来的对象,数据类型和数据库尽量保持一致
type User struct {
	Id          int64 `json:"id" db:"id"`
	ReceiveUser
}

// 添加用户时,接收用户传值的对象
type ReceiveUser struct {
	Name  string `json:"name" db:"name"`
	Age   int64  `json:"age" db:"age"`
	Email string `json:"email" db:"email"`
}

// 查询用户
func SelectUser(id int64) (resObj *User, resErr error) {
	// 定义redis的key, id转string类型
	redisKey := "test:user_" + strconv.FormatInt(id, 10)

	// 定义接收数据的对象
	var user User

	// 先从缓存查询,没有再从数据库查
	if err := dbs.RedisDB.GetJSON(redisKey, &user); err != nil {
		if strings.Contains(err.Error(), "key not found") {
			// key不存在,就重新查询一次

			// 预处理SQL语句
			selectSql := "select * from users where id=? limit 1"

			// 打印日志
			beego.Debug("[sql]: "+selectSql, id)
			err := dbs.MysqlDB.Get(&user, selectSql, id)
			if err != nil {
				if err.Error() == "sql: no rows in result set" {
					beego.Trace("查询结果为空值!")

					// 将空值添加到缓存, 有效期1小时
					if err1 := dbs.RedisDB.SetJSON(redisKey, nil, tools.OneHour); err1 != nil {
						beego.Error(err1)
						return nil, err1
					}
					// 返回空值
					return nil, nil
				} else {
					// 打印错误日志
					beego.Error(err)
					// 返回错误信息
					return nil, err
				}
			}

			// 将结果添加到缓存
			if err2 := dbs.RedisDB.SetJSON(redisKey, &user, tools.OneDay); err2 != nil {
				// 打印错误日志
				beego.Error(err2)
				// 返回错误信息
				return nil, err2
			}

			// 返回结果给用户
			return &user, nil
		}
	}

	// 空值返回用户信息
	if user.Id == 0 {
		// 因为正常情况下,ID是从1开始的。0就表示读取出来的值是空值
		return nil, nil
	} else {
		return &user, nil
	}
}

Be careful!

- The GETJSON method and SETJSON method is depend on each other!
- Before the Objects are stored to redis, it will first serialized using JSON.
- Objects were taken out from the redis, before using, it will first deserialization using JSON.

More documentation, please be patient!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheMiss = errors.New("redis_full: key not found.")

Functions

func CheckBitOP

func CheckBitOP(t *testing.T, newredis RedisFactory)

func CheckGET

func CheckGET(t *testing.T, newredis RedisFactory)

func CheckGETBIT

func CheckGETBIT(t *testing.T, newredis RedisFactory)

func CheckGetBitSetBit

func CheckGetBitSetBit(t *testing.T, newredis RedisFactory)

func CheckLINDEX

func CheckLINDEX(t *testing.T, newredis RedisFactory)

func CheckLock

func CheckLock(t *testing.T, newRedis RedisFactory)

相同两次设置来判断是否加锁成功

func CheckMGET

func CheckMGET(t *testing.T, newredis RedisFactory)

func CheckSCARD

func CheckSCARD(t *testing.T, newredis RedisFactory)

func CheckSMEMBERS

func CheckSMEMBERS(t *testing.T, newredis RedisFactory)

func IsBody

func IsBody(s []interface{}, y interface{}) bool

some tools

Types

type RedisCache

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

Wraps the Redis client to meet the Cache interface.

func NewRedisCache

func NewRedisCache(host, password string, database, MaxIdle, MaxActive int, IdleTimeout, defaultExpiration time.Duration) RedisCache

until redigo supports sharding/clustering, only one host will be in hostList

func (RedisCache) APPEND

func (c RedisCache) APPEND(key string, value string, expires time.Duration) error

func (RedisCache) BITCOUNT

func (c RedisCache) BITCOUNT(key string, start, end int) (int64, error)

func (RedisCache) BITOP

func (c RedisCache) BITOP(opt string, key1, key2 string) (string, error)

func (RedisCache) CheckRedis

func (c RedisCache) CheckRedis() error

func (RedisCache) DECR

func (c RedisCache) DECR(key string, expires time.Duration) error

func (RedisCache) DECRBY

func (c RedisCache) DECRBY(key string, value int, expires time.Duration) error

func (RedisCache) Del

func (c RedisCache) Del(key string) error

func (RedisCache) Exists

func (c RedisCache) Exists(key string) (bool, error)

func (RedisCache) FlushALL

func (c RedisCache) FlushALL() error

func (RedisCache) FlushDB

func (c RedisCache) FlushDB() error

func (RedisCache) GETBIT

func (c RedisCache) GETBIT(key string, offset int) (int64, error)

func (RedisCache) GetJSON

func (c RedisCache) GetJSON(key string, ptrValue interface{}) error

func (RedisCache) HDEL

func (c RedisCache) HDEL(key, field string) error

func (RedisCache) HGET

func (c RedisCache) HGET(key, field string) (string, error)

func (RedisCache) HGETMAP

func (c RedisCache) HGETMAP(key string, fields ...string) (map[string]string, error)

func (RedisCache) HKEYS

func (c RedisCache) HKEYS(key string) ([]string, error)

func (RedisCache) HLEN

func (c RedisCache) HLEN(key string) (int64, error)

func (RedisCache) HMDEL

func (c RedisCache) HMDEL(key string, fields ...string) error

func (RedisCache) HSET

func (c RedisCache) HSET(key, field, value string, expires time.Duration) error

func (RedisCache) HSETMAP

func (c RedisCache) HSETMAP(key string, args map[string]string, expires time.Duration) error

func (RedisCache) INCR

func (c RedisCache) INCR(key string, expires time.Duration) error

func (RedisCache) INCRBY

func (c RedisCache) INCRBY(key string, value int, expires time.Duration) error

func (RedisCache) INCRBYFLOAT

func (c RedisCache) INCRBYFLOAT(key string, value float64, expires time.Duration) error

func (RedisCache) Keys

func (c RedisCache) Keys() ([]string, error)

func (RedisCache) LINDEX

func (c RedisCache) LINDEX(key string, index int, result interface{}) error

func (RedisCache) LINSERT

func (c RedisCache) LINSERT(key, direction string, pivot, value interface{}) error

func (RedisCache) LLEN

func (c RedisCache) LLEN(key string) (int64, error)

func (RedisCache) LLREM

func (c RedisCache) LLREM(key string, count, value int) error

delete from left

func (RedisCache) LPOP

func (c RedisCache) LPOP(key string, result interface{}) error

func (RedisCache) LPUSH

func (c RedisCache) LPUSH(key string, expires time.Duration, values ...interface{}) error

func (RedisCache) LRANGE

func (c RedisCache) LRANGE(key string, start, stop int) ([]interface{}, error)

func (RedisCache) LRREM

func (c RedisCache) LRREM(key string, count, value int) error

delete from right

func (RedisCache) LSET

func (c RedisCache) LSET(key string, index int, value interface{}) error

func (RedisCache) LTRIM

func (c RedisCache) LTRIM(key string, start, stop int) error

func (RedisCache) Life

func (c RedisCache) Life(key string) (int64, error)

查询剩余生存时间

func (RedisCache) Lock

func (c RedisCache) Lock(key string, expires time.Duration) (ok bool, err error)

func (RedisCache) MDEL

func (c RedisCache) MDEL(keys ...string) error

func (RedisCache) MGetJSON

func (c RedisCache) MGetJSON(keys ...string) (map[string]interface{}, error)

func (RedisCache) MSetJSON

func (c RedisCache) MSetJSON(args map[string]interface{}, expires time.Duration) error

func (RedisCache) RPOP

func (c RedisCache) RPOP(key string, result interface{}) error

func (RedisCache) RPOPLPUSH

func (c RedisCache) RPOPLPUSH(key1, key2 string) error

func (RedisCache) RPUSH

func (c RedisCache) RPUSH(key string, expires time.Duration, values ...interface{}) error

func (RedisCache) SADD

func (c RedisCache) SADD(key string, expires time.Duration, args ...interface{}) error

func (RedisCache) SCARD

func (c RedisCache) SCARD(key string) (int64, error)

func (RedisCache) SETBIT

func (c RedisCache) SETBIT(key string, offset, value int, expires time.Duration) error

func (RedisCache) SISMEMBER

func (c RedisCache) SISMEMBER(key string, value interface{}) (bool, error)

func (RedisCache) SMEMBERS

func (c RedisCache) SMEMBERS(key string) ([]interface{}, error)

func (RedisCache) SREM

func (c RedisCache) SREM(key string, args ...interface{}) error

func (RedisCache) STRLEN

func (c RedisCache) STRLEN(key string) (int64, error)

func (RedisCache) SetJSON

func (c RedisCache) SetJSON(key string, value interface{}, expires time.Duration) error

func (RedisCache) SetLife

func (c RedisCache) SetLife(key string, expires time.Duration) error

设置 key 的剩余生存时间

func (RedisCache) Type

func (c RedisCache) Type(key string) (string, error)

func (RedisCache) Unlock

func (c RedisCache) Unlock(key string) error

type RedisFactory

type RedisFactory func(*testing.T, time.Duration) RedisCache

Jump to

Keyboard shortcuts

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