d46833c6 by astaxie

Merge pull request #997 from dockercn/master

增加session模块中的ledisdb的动态配置
2 parents 5d8187d0 29d98731
...@@ -2,6 +2,8 @@ package session ...@@ -2,6 +2,8 @@ package session
2 2
3 import ( 3 import (
4 "net/http" 4 "net/http"
5 "strconv"
6 "strings"
5 "sync" 7 "sync"
6 8
7 "github.com/astaxie/beego/session" 9 "github.com/astaxie/beego/session"
...@@ -74,19 +76,29 @@ func (ls *LedisSessionStore) SessionRelease(w http.ResponseWriter) { ...@@ -74,19 +76,29 @@ func (ls *LedisSessionStore) SessionRelease(w http.ResponseWriter) {
74 type LedisProvider struct { 76 type LedisProvider struct {
75 maxlifetime int64 77 maxlifetime int64
76 savePath string 78 savePath string
79 db int
77 } 80 }
78 81
79 // init ledis session 82 // init ledis session
80 // savepath like ledis server saveDataPath,pool size 83 // savepath like ledis server saveDataPath,pool size
81 // e.g. 127.0.0.1:6379,100,astaxie 84 // e.g. 127.0.0.1:6379,100,astaxie
82 func (lp *LedisProvider) SessionInit(maxlifetime int64, savePath string) error { 85 func (lp *LedisProvider) SessionInit(maxlifetime int64, savePath string) error {
86 var err error
83 lp.maxlifetime = maxlifetime 87 lp.maxlifetime = maxlifetime
84 lp.savePath = savePath 88 configs := strings.Split(savepath, ",")
89 if len(configs) == 1 {
90 lp.savePath = configs[0]
91 } else if len(configs) == 2 {
92 lp.savePath = configs[0]
93 lp.db, err = strconv.Atoi(configs[1])
94 if err != nil {
95 return err
96 }
97 }
85 cfg := new(config.Config) 98 cfg := new(config.Config)
86 cfg.DataDir = lp.savePath 99 cfg.DataDir = lp.savePath
87 var err error
88 nowLedis, err := ledis.Open(cfg) 100 nowLedis, err := ledis.Open(cfg)
89 c, err = nowLedis.Select(0) 101 c, err = nowLedis.Select(lp.db)
90 if err != nil { 102 if err != nil {
91 println(err) 103 println(err)
92 return nil 104 return nil
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!