redis

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2021 License: GPL-3.0 Imports: 3 Imported by: 1

README

go-models-redis

go-models-redis its lite and easy model.

Requirements

* Go 1.12 or higher.
* [go-redis/redis](https://github.com/go-redis/redis) package

Go-Module

create go.mod file in your package folder, and fill below

module github.com/eehsiao/go-models-example

go 1.13

require (
	github.com/eehsiao/go-models-lib latest
	github.com/eehsiao/go-models-redis latest
	github.com/go-redis/redis v6.15.5+incompatible
)

Docker

Easy to start the test evn. That you can run the example code.

$ docker-compose up -d

Usage

import (
    "database/sql"
	"fmt"

	mysql "github.com/eehsiao/go-models-mysql"
	redis "github.com/eehsiao/go-models-redis"
)

// User : json struct that to store into redis
type User struct {
	Host       string `json:"host"`
	User       string `json:"user"`
	SelectPriv string `json:"select_priv"`
}

//new redis dao
redUserModel := &RedUserModel{
    Dao: redis.NewDao().SetConfig("127.0.0.1:6379", "", 0).OpenDB(),
}

// set a struct for dao as default model (option)
// (*User)(nil) : nil pointer of the User struct
// "user" : is real table name in the db
SetDefaultModel((*User)(nil), "user")

Example

1 build-in

example.go

The example will connect to local mysql and get user data. Then connect to local redis and set user data, and get back.

2 example

https://github.com/eehsiao/go-models-example/

How-to

How to design model data logical

Redis
1.

create a data struct, and add the tag json:"name"

type User struct {
	Host       string `json:"host"`
	User       string `json:"user"`
	SelectPriv string `json:"select_priv"`
	IntVal     int    `json:"user,string"`
}

if you have integer value, you can add a transfer type desc. such as json:"user,string"

2.

create redis dao

m := redis.NewDao().SetConfig("127.0.0.1:6379", "", 0).OpenDB()
3.

directly use the go-redis command function

redBool, err = m.HSet(userTable, redKey, serialStr).Result()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dao

type Dao struct {
	*redis.Client
	DaoStruct     string
	DaoStructType reflect.Type
	DbNnum        int
	DataKey       string
}

func NewDao

func NewDao() *Dao

func (*Dao) GetConfig

func (dao *Dao) GetConfig() *redis.Options

GetConfig : return redis.Options

func (*Dao) OpenDB

func (dao *Dao) OpenDB() *Dao

OpenDB : connect to db

func (*Dao) OpenDBWithPoolConns

func (dao *Dao) OpenDBWithPoolConns(active, idle int) *Dao

OpenDBWithPoolConns : connect to db and set pool conns

func (*Dao) OpenDBWithPoolConnsnRet added in v0.0.5

func (dao *Dao) OpenDBWithPoolConnsnRet(active, idle int) error

OpenDBWithPoolConns : connect to db and set pool conns

func (*Dao) OpenDBnRet added in v0.0.5

func (dao *Dao) OpenDBnRet() error

OpenDBnRet : connect to db

func (*Dao) SetConfig

func (dao *Dao) SetConfig(addr, pw string, db int) *Dao

SetConfig : set config by user, pw, addr, db

func (*Dao) SetDefaultModel

func (dao *Dao) SetDefaultModel(tb interface{}, deftultDataKey string) (err error)

SetDefaultModel : register a table struct for this dao

func (*Dao) SetOriginConfig

func (dao *Dao) SetOriginConfig(c *redis.Options) *Dao

SetOriginConfig : set config by redis.Options

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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