2013-04-05 15:50:53 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/garyburd/redigo/redis"
|
|
|
|
)
|
|
|
|
|
|
|
|
var redispder = &RedisProvider{}
|
|
|
|
|
2013-05-14 15:55:50 +00:00
|
|
|
var MAX_POOL_SIZE = 20
|
|
|
|
|
|
|
|
var redisPool chan redis.Conn
|
|
|
|
|
2013-04-05 15:50:53 +00:00
|
|
|
type RedisSessionStore struct {
|
|
|
|
c redis.Conn
|
|
|
|
sid string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RedisSessionStore) Set(key, value interface{}) error {
|
2013-05-14 15:55:50 +00:00
|
|
|
//_, err := rs.c.Do("HSET", rs.sid, key, value)
|
2013-04-05 15:50:53 +00:00
|
|
|
_, err := rs.c.Do("HSET", rs.sid, key, value)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RedisSessionStore) Get(key interface{}) interface{} {
|
2013-08-09 15:41:03 +00:00
|
|
|
reply, err := rs.c.Do("HGET", rs.sid, key)
|
2013-04-05 15:50:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2013-08-09 15:41:03 +00:00
|
|
|
return reply
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RedisSessionStore) Delete(key interface{}) error {
|
2013-05-14 15:55:50 +00:00
|
|
|
//_, err := rs.c.Do("HDEL", rs.sid, key)
|
2013-04-05 15:50:53 +00:00
|
|
|
_, err := rs.c.Do("HDEL", rs.sid, key)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-09-26 10:07:00 +00:00
|
|
|
func (rs *RedisSessionStore) Flush() error {
|
|
|
|
_, err := rs.c.Do("DEL", rs.sid)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-04-05 15:50:53 +00:00
|
|
|
func (rs *RedisSessionStore) SessionID() string {
|
|
|
|
return rs.sid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RedisSessionStore) SessionRelease() {
|
|
|
|
rs.c.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
type RedisProvider struct {
|
|
|
|
maxlifetime int64
|
|
|
|
savePath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *RedisProvider) connectInit() redis.Conn {
|
2013-05-14 15:55:50 +00:00
|
|
|
/*c, err := redis.Dial("tcp", rp.savePath)
|
2013-04-05 15:50:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2013-05-14 15:55:50 +00:00
|
|
|
return c*/
|
2013-05-22 14:26:26 +00:00
|
|
|
//if redisPool == nil {
|
2013-08-09 15:41:03 +00:00
|
|
|
redisPool = make(chan redis.Conn, MAX_POOL_SIZE)
|
2013-05-22 14:26:26 +00:00
|
|
|
//}
|
2013-05-14 15:55:50 +00:00
|
|
|
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 {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
putRedis(c)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
return <-redisPool
|
|
|
|
}
|
|
|
|
|
|
|
|
func putRedis(conn redis.Conn) {
|
|
|
|
if redisPool == nil {
|
|
|
|
redisPool = make(chan redis.Conn, MAX_POOL_SIZE)
|
|
|
|
}
|
2013-05-22 14:26:26 +00:00
|
|
|
if len(redisPool) >= MAX_POOL_SIZE {
|
2013-05-14 15:55:50 +00:00
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
redisPool <- conn
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error {
|
|
|
|
rp.maxlifetime = maxlifetime
|
|
|
|
rp.savePath = savePath
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *RedisProvider) SessionRead(sid string) (SessionStore, error) {
|
|
|
|
c := rp.connectInit()
|
2013-05-14 15:55:50 +00:00
|
|
|
//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 == "" {
|
|
|
|
//c.Do("SET", sid, sid, rp.maxlifetime)
|
|
|
|
c.Do("HSET", sid, sid, rp.maxlifetime)
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
|
|
|
rs := &RedisSessionStore{c: c, sid: sid}
|
|
|
|
return rs, nil
|
|
|
|
}
|
|
|
|
|
2013-09-26 10:07:00 +00:00
|
|
|
func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) {
|
|
|
|
c := rp.connectInit()
|
|
|
|
if str, err := redis.String(c.Do("HGET", oldsid, oldsid)); err != nil || str == "" {
|
|
|
|
c.Do("HSET", oldsid, oldsid, rp.maxlifetime)
|
|
|
|
}
|
|
|
|
c.Do("RENAME", oldsid, sid)
|
|
|
|
rs := &RedisSessionStore{c: c, sid: sid}
|
|
|
|
return rs, nil
|
|
|
|
}
|
|
|
|
|
2013-04-05 15:50:53 +00:00
|
|
|
func (rp *RedisProvider) SessionDestroy(sid string) error {
|
|
|
|
c := rp.connectInit()
|
|
|
|
c.Do("DEL", sid)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *RedisProvider) SessionGC() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Register("redis", redispder)
|
|
|
|
}
|