Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Important: Now need go1.9+ version support.

Document: https://www.kancloud.cn/devfeel/dotweb/346608

CookBook: https://www.kancloud.cn/devfeel/dotweb/439314
Guide: https://github.com/devfeel/dotweb/blob/master/docs/GUIDE.md

[![Gitter](https://badges.gitter.im/devfeel/dotweb.svg)](https://gitter.im/devfeel-dotweb/wechat)
[![GoDoc](https://godoc.org/github.com/devfeel/dotweb?status.svg)](https://godoc.org/github.com/devfeel/dotweb)
Expand Down
4 changes: 2 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"
"github.com/devfeel/dotweb"
"github.com/devfeel/dotweb/framework/exception"
"github.com/devfeel/dotweb/session"
"net/http"
"strconv"
"time"
"github.com/devfeel/dotweb/session"
)

func main() {
Expand Down Expand Up @@ -46,7 +46,7 @@ func main() {
//runtime mode
app.HttpServer.SetSessionConfig(session.NewDefaultRuntimeConfig())
//redis mode
//app.HttpServer.SetSessionConfig(session.NewDefaultRedisConfig("192.168.8.175:6379", ""))
//app.HttpServer.SetSessionConfig(session.NewDefaultRedisConfig("redis://192.168.8.175:6379/0"))

app.HttpServer.SetEnabledDetailRequestData(true)

Expand Down
8 changes: 8 additions & 0 deletions framework/redis/redisutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,14 @@ func (rc *RedisClient) SUnionStore(destination string, key ...interface{})(int,

//****************** 全局操作 ***********************

// Ping 测试一个连接是否可用
func (rc *RedisClient) Ping()(string,error){
conn := rc.pool.Get()
defer conn.Close()
val, err:=redis.String(conn.Do("PING"))
return val, err
}

// DBSize 返回当前数据库的 key 的数量
func (rc *RedisClient) DBSize()(int64, error){
conn := rc.pool.Get()
Expand Down
16 changes: 16 additions & 0 deletions framework/redis/redisutil_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
package redisutil

import (
"testing"
)

const redisServerURL = "redis://192.168.8.175:6379/0"

func TestRedisClient_Ping(t *testing.T) {
redisClient := GetRedisClient(redisServerURL)
val, err := redisClient.Ping()
if err != nil{
t.Error(err)
}else{
t.Log(val)
}
}
9 changes: 7 additions & 2 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"
"strconv"
"time"
"fmt"
)

const (
Expand Down Expand Up @@ -51,12 +52,16 @@ func GetSessionStore(config *StoreConfig) SessionStore {
case SessionMode_Runtime:
return NewRuntimeStore(config)
case SessionMode_Redis:
return NewRedisStore(config)
store, err := NewRedisStore(config)
if err != nil{
panic(fmt.Sprintf("redis session [%v] ping error -> %v", config.StoreName, err.Error()))
}else{
return store
}
default:
panic("not support session store -> " + config.StoreName)
}
return nil

}

//create new store with default config and use runtime store
Expand Down
7 changes: 5 additions & 2 deletions session/store_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ func getRedisKey(key string) string {
}

//create new redis store
func NewRedisStore(config *StoreConfig) *RedisStore {
return &RedisStore{
func NewRedisStore(config *StoreConfig) (*RedisStore, error){
store := &RedisStore{
lock: new(sync.RWMutex),
serverIp: config.ServerIP,
maxlifetime: config.Maxlifetime,
}
redisClient := redisutil.GetRedisClient(store.serverIp)
_, err:=redisClient.Ping()
return store, err
}

// SessionRead get session state by sessionId
Expand Down
4 changes: 4 additions & 0 deletions version.MD
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## dotweb版本记录:

#### Version 1.4.9.5
* framewrok.RedisClient增加Ping接口,用于检查服务是否可用
* GetSessionStore增加Redis ServerUrl的ping检查,若不可用直接panic异常信息
* 2018-05-23 13:30

#### Version 1.4.9.4
* 调整: dotweb.Classic移除自动UseRequestLog逻辑
Expand Down