安装redis
go get - u github. com/ gin- gonic/ gin
go get - u github. com/ go- redis/ redis/ v8
创建相关目录
gotest-> conifg -> database . go-> redis . go-> controller -> index . go-> model -> user . go-> router -> router . gomain. go
封装Redis
package config
import ( "github.com/go-redis/redis/v8" "context" "time"
)
var rdb * redis. Client
var Ctx = context. Background ( ) func InitRedis ( ) { rdb = redis. NewClient ( & redis. Options{ Addr : "localhost:6379" , Password : "" , DB : 0 , } ) _ , err : = rdb. Ping ( Ctx) . Result ( ) if err != nil { panic ( err) }
}
func Set ( key, value string , expiration time. Duration) error {
return rdb. Set ( Ctx, key, value, expiration) . Err ( )
}
func Get ( key string ) ( string , error) {
return rdb. Get ( Ctx, key) . Result ( )
}
func Del ( key string ) error {
return rdb. Del ( Ctx, key) . Err ( )
}
func IncrBy ( key string , value int64) ( int64, error) { return rdb. IncrBy ( Ctx, key, value) . Result ( )
}
func DecrBy ( key string , value int64) ( int64, error) { return rdb. DecrBy ( Ctx, key, value) . Result ( )
}
func Expire ( key string , expiration time. Duration) error {
return rdb. Expire ( Ctx, key, expiration) . Err ( )
}
func StringExists ( key string ) ( bool , error) {
count, err : = rdb. Exists ( Ctx, key) . Result ( )
return count >
0 , err
}
func HashSet ( key, field, value string ) error {
return rdb. HSet ( Ctx, key, field, value) . Err ( )
}
func HashMSet ( key string , fields map[ string ] interface {
} ) error {
return rdb. HMSet ( Ctx, key, fields) . Err ( )
}
func HashGet ( key, field string ) ( string , error) {
return rdb. HGet ( Ctx, key, field) . Result ( )
}
func HashGetAll ( key string ) ( map[ string ] string , error) {
return rdb. HGetAll ( Ctx, key) . Result ( )
}
func HashDel ( key string , fields ... string ) error {
return rdb. HDel ( Ctx, key, fields... ) . Err ( )
}
func HashExists ( key, field string ) ( bool , error) {
exists, err : = rdb. HExists ( Ctx, key, field) . Result ( )
return exists, err
}
func HashIncrBy ( key, field string , value int64) ( int64, error) {
return rdb. HIncrBy ( Ctx, key, field, value) . Result ( )
}
func ListLPush ( key string , values ... interface {
} ) error {
return rdb. LPush ( Ctx, key, values... ) . Err ( )
}
func ListRPush ( key string , values ... interface {
} ) error {
return rdb. RPush ( Ctx, key, values... ) . Err ( )
}
func ListLPop ( key string ) ( string , error) {
return rdb. LPop ( Ctx, key) . Result ( )
}
func ListRPop ( key string ) ( string , error) {
return rdb. RPop ( Ctx, key) . Result ( )
}
func ListRange ( key string , start, stop int64) ( [ ] string , error) {
return rdb. LRange ( Ctx, key, start, stop) . Result ( )
}
func ListLen ( key string ) ( int64, error) {
return rdb. LLen ( Ctx, key) . Result ( )
}
func ListDel ( key string ) error {
return rdb. Del ( Ctx, key) . Err ( )
}
func SetAdd ( key string , members ... interface {
} ) error {
return rdb. SAdd ( Ctx, key, members... ) . Err ( )
}
func SetMembers ( key string ) ( [ ] string , error) {
return rdb. SMembers ( Ctx, key) . Result ( )
}
func SetIsMember ( key string , member interface {
} ) ( bool , error) {
exists, err : = rdb. SIsMember ( Ctx, key, member) . Result ( )
return exists, err
}
func SetRem ( key string , members ... interface {
} ) error {
return rdb. SRem ( Ctx, key, members... ) . Err ( )
}
func SetCard ( key string ) ( int64, error) {
return rdb. SCard ( Ctx, key) . Result ( )
}
func SetPop ( key string ) ( string , error) {
return rdb. SPop ( Ctx, key) . Result ( )
}
func ZAdd ( key string , members ... * redis. Z ) error {
return rdb. ZAdd ( Ctx, key, members... ) . Err ( )
}
func ZRange ( key string , start, stop int64) ( [ ] string , error) {
return rdb. ZRange ( Ctx, key, start, stop) . Result ( )
}
func ZRevRange ( key string , start, stop int64) ( [ ] string , error) {
return rdb. ZRevRange ( Ctx, key, start, stop) . Result ( )
}
func ZRangeWithScores ( key string , start, stop int64) ( [ ] redis. Z , error) {
return rdb. ZRangeWithScores ( Ctx, key, start, stop) . Result ( )
}
func ZScore ( key, member string ) ( float64, error) {
return rdb. ZScore ( Ctx, key, member) . Result ( )
}
func ZRem ( key string , members ... string ) error {
var interfaceMembers [ ] interface {
}
for _ , member : = range members {
interfaceMembers = append ( interfaceMembers, member)
}
return rdb. ZRem ( Ctx, key, interfaceMembers... ) . Err ( )
}
func ZCard ( key string ) ( int64, error) {
return rdb. ZCard ( Ctx, key) . Result ( )
}
func ZRank ( key, member string ) ( int64, error) {
return rdb. ZRank ( Ctx, key, member) . Result ( )
}
func ZRevRank ( key, member string ) ( int64, error) {
return rdb. ZRevRank ( Ctx, key, member) . Result ( )
}
全局引用 main.go
package main
import ( "gotest/router" "gotest/config"
) func main ( ) { config. InitRedis ( ) r : = router. SetupRouter ( ) r. Run ( ":8080" )
}
测试调用
package controller
import ( "fmt" "github.com/gin-gonic/gin" "gotest/config" "gotest/model"
)
func RedisHandler ( c * gin. Context) { key : = "name" value : = "我成功設置了redis" err : = config. Set ( key, value, 0 ) if err != nil { c. JSON ( 500 , gin. H { "error" : err. Error ( ) } ) return } name, err : = config. Get ( "name" ) ; if err != nil { c. JSON ( 500 , gin. H { "error" : err. Error ( ) } ) return } c. JSON ( 200 , gin. H { "message" : "Set success" , "data" : name, } )
}
}