Skip to content
This repository has been archived by the owner on Nov 29, 2020. It is now read-only.

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mohuishou committed Mar 5, 2018
1 parent 008fbfe commit 6daf11a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
20 changes: 13 additions & 7 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@ import (
"github.com/mohuishou/scuplus-go/config"
)

var Redis redis.Conn
var RedisPool *redis.Pool

// init 初始化缓存
// 目前使用redis作为底层
func init() {
var err error
Redis, err = redis.Dial("tcp", fmt.Sprintf("%s:%s", config.Get().Redis.IP, config.Get().Redis.Port))
if err != nil {
panic(err)
func Init() {
RedisPool = &redis.Pool{
MaxIdle: 3,
MaxActive: 15, // max number of connections
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", fmt.Sprintf("%s:%s", config.Get().Redis.IP, config.Get().Redis.Port))
if err != nil {
panic(err.Error())
}
return c, err
},
}
}

func Do(cmd string, args ...interface{}) (interface{}, error) {
return Redis.Do(cmd, args...)
return RedisPool.Get().Do(cmd, args...)
}
4 changes: 2 additions & 2 deletions cache/code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// Get get
func Get(key string) string {
v, err := cache.Redis.Do("GET", "verifyCode."+key)
v, err := cache.Do("GET", "verifyCode."+key)
if err != nil {
log.Println("get cache code err:", err)
}
Expand All @@ -17,7 +17,7 @@ func Get(key string) string {

// Set set
func Set(key, v string) error {
_, err := cache.Redis.Do("SET", "verifyCode."+key, v)
_, err := cache.Do("SET", "verifyCode."+key, v)
if err != nil {
log.Println("set cache code err:", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cache/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const expireTime = 3600 * 1.5

// Get get
func Get() string {
v, err := cache.Redis.Do("GET", key)
v, err := cache.Do("GET", key)
if err != nil {
log.Println("get cache token err:", err)
}
Expand All @@ -25,7 +25,7 @@ func Get() string {

// Set set
func Set(v string) error {
_, err := cache.Redis.Do("SET", key, v)
_, err := cache.Do("SET", key, v)
if err != nil {
log.Println("set cache token err:", err)
}
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"os"

"github.com/mohuishou/scuplus-go/cache"

"github.com/betacraft/yaag/irisyaag"
"github.com/betacraft/yaag/yaag"

Expand All @@ -27,6 +29,8 @@ func main() {
app.Use(irisyaag.New()) // <- IMPORTANT, register the middleware.
}

cache.Init()

// 注册中间件
middleware.Register(app)
route.Routes(app)
Expand Down

0 comments on commit 6daf11a

Please sign in to comment.