1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 18:10:54 +00:00

fix #241#192

This commit is contained in:
astaxie 2013-10-28 22:19:37 +08:00
parent 57165f2fb5
commit 060631e952
2 changed files with 45 additions and 43 deletions

View File

@ -39,10 +39,10 @@ Then in you web app init the global session manager
go globalSessions.GC() go globalSessions.GC()
} }
* Use **Redis** as provider, the last param is the Redis conn address: * Use **Redis** as provider, the last param is the Redis conn address,poolsize,password:
func init() { func init() {
globalSessions, _ = session.NewManager("redis", "gosessionid", 3600, "127.0.0.1:6379") globalSessions, _ = session.NewManager("redis", "gosessionid", 3600, "127.0.0.1:6379,100,astaxie")
go globalSessions.GC() go globalSessions.GC()
} }

View File

@ -1,13 +1,14 @@
package session package session
import ( import (
"fmt"
"github.com/garyburd/redigo/redis" "github.com/garyburd/redigo/redis"
"strconv"
"strings"
) )
var redispder = &RedisProvider{} var redispder = &RedisProvider{}
var MAX_POOL_SIZE = 20 var MAX_POOL_SIZE = 100
var redisPool chan redis.Conn var redisPool chan redis.Conn
@ -52,72 +53,73 @@ func (rs *RedisSessionStore) SessionRelease() {
type RedisProvider struct { type RedisProvider struct {
maxlifetime int64 maxlifetime int64
savePath string savePath string
poolsize int
password string
poollist *redis.Pool
} }
func (rp *RedisProvider) connectInit() redis.Conn { //savepath like redisserveraddr,poolsize,password
/*c, err := redis.Dial("tcp", rp.savePath) //127.0.0.1:6379,100,astaxie
if err != nil {
return nil
}
return c*/
//if redisPool == nil {
redisPool = make(chan redis.Conn, MAX_POOL_SIZE)
//}
if len(redisPool) == 0 {
go func() {
for i := 0; i < MAX_POOL_SIZE/2; i++ {
c, err := redis.Dial("tcp", rp.savePath)
if err != nil {
fmt.Println(err)
return
}
putRedis(c)
}
}()
}
return <-redisPool
}
func putRedis(conn redis.Conn) {
if redisPool == nil {
redisPool = make(chan redis.Conn, MAX_POOL_SIZE)
}
if len(redisPool) >= MAX_POOL_SIZE {
conn.Close()
return
}
redisPool <- conn
}
func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error { func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error {
rp.maxlifetime = maxlifetime rp.maxlifetime = maxlifetime
rp.savePath = savePath configs := strings.Split(savePath, ",")
if len(configs) > 0 {
rp.savePath = configs[0]
}
if len(configs) > 1 {
poolsize, err := strconv.Atoi(configs[1])
if err != nil || poolsize <= 0 {
rp.poolsize = MAX_POOL_SIZE
} else {
rp.poolsize = poolsize
}
} else {
rp.poolsize = MAX_POOL_SIZE
}
if len(configs) > 2 {
rp.password = configs[2]
}
rp.poollist = redis.NewPool(func() (redis.Conn, error) {
c, err := redis.Dial("tcp", rp.savePath)
if err != nil {
return nil, err
}
if rp.password != "" {
if _, err := c.Do("AUTH", rp.password); err != nil {
c.Close()
return nil, err
}
}
return c, err
}, rp.poolsize)
return nil return nil
} }
func (rp *RedisProvider) SessionRead(sid string) (SessionStore, error) { func (rp *RedisProvider) SessionRead(sid string) (SessionStore, error) {
c := rp.connectInit() c := rp.poollist.Get()
//if str, err := redis.String(c.Do("GET", sid)); err != nil || str == "" { //if str, err := redis.String(c.Do("GET", sid)); err != nil || str == "" {
if str, err := redis.String(c.Do("HGET", sid, sid)); err != nil || str == "" { if str, err := redis.String(c.Do("HGET", sid, sid)); err != nil || str == "" {
//c.Do("SET", sid, sid, rp.maxlifetime) //c.Do("SET", sid, sid, rp.maxlifetime)
c.Do("HSET", sid, sid, rp.maxlifetime) c.Do("HSET", sid, sid, rp.maxlifetime)
} }
c.Do("EXPIRE", sid, rp.maxlifetime)
rs := &RedisSessionStore{c: c, sid: sid} rs := &RedisSessionStore{c: c, sid: sid}
return rs, nil return rs, nil
} }
func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) { func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) {
c := rp.connectInit() c := rp.poollist.Get()
if str, err := redis.String(c.Do("HGET", oldsid, oldsid)); err != nil || str == "" { if str, err := redis.String(c.Do("HGET", oldsid, oldsid)); err != nil || str == "" {
c.Do("HSET", oldsid, oldsid, rp.maxlifetime) c.Do("HSET", oldsid, oldsid, rp.maxlifetime)
} }
c.Do("RENAME", oldsid, sid) c.Do("RENAME", oldsid, sid)
c.Do("EXPIRE", sid, rp.maxlifetime)
rs := &RedisSessionStore{c: c, sid: sid} rs := &RedisSessionStore{c: c, sid: sid}
return rs, nil return rs, nil
} }
func (rp *RedisProvider) SessionDestroy(sid string) error { func (rp *RedisProvider) SessionDestroy(sid string) error {
c := rp.connectInit() c := rp.poollist.Get()
c.Do("DEL", sid) c.Do("DEL", sid)
return nil return nil
} }