2013-04-05 15:50:53 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/garyburd/redigo/redis"
|
2013-10-28 14:19:37 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2013-11-05 13:59:35 +00:00
|
|
|
"sync"
|
2013-04-05 15:50:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var redispder = &RedisProvider{}
|
|
|
|
|
2013-10-28 14:19:37 +00:00
|
|
|
var MAX_POOL_SIZE = 100
|
2013-05-14 15:55:50 +00:00
|
|
|
|
|
|
|
var redisPool chan redis.Conn
|
|
|
|
|
2013-04-05 15:50:53 +00:00
|
|
|
type RedisSessionStore struct {
|
2013-11-05 13:59:35 +00:00
|
|
|
c redis.Conn
|
|
|
|
sid string
|
|
|
|
lock sync.RWMutex
|
|
|
|
values map[interface{}]interface{}
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RedisSessionStore) Set(key, value interface{}) error {
|
2013-11-05 13:59:35 +00:00
|
|
|
rs.lock.Lock()
|
|
|
|
defer rs.lock.Unlock()
|
|
|
|
rs.values[key] = value
|
|
|
|
return nil
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RedisSessionStore) Get(key interface{}) interface{} {
|
2013-11-05 13:59:35 +00:00
|
|
|
rs.lock.RLock()
|
|
|
|
defer rs.lock.RUnlock()
|
|
|
|
if v, ok := rs.values[key]; ok {
|
|
|
|
return v
|
|
|
|
} else {
|
2013-04-05 15:50:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
2013-11-05 13:59:35 +00:00
|
|
|
return nil
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RedisSessionStore) Delete(key interface{}) error {
|
2013-11-05 13:59:35 +00:00
|
|
|
rs.lock.Lock()
|
|
|
|
defer rs.lock.Unlock()
|
|
|
|
delete(rs.values, key)
|
|
|
|
return nil
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 10:07:00 +00:00
|
|
|
func (rs *RedisSessionStore) Flush() error {
|
2013-11-05 13:59:35 +00:00
|
|
|
rs.lock.Lock()
|
|
|
|
defer rs.lock.Unlock()
|
|
|
|
rs.values = make(map[interface{}]interface{})
|
|
|
|
return nil
|
2013-09-26 10:07:00 +00:00
|
|
|
}
|
|
|
|
|
2013-04-05 15:50:53 +00:00
|
|
|
func (rs *RedisSessionStore) SessionID() string {
|
|
|
|
return rs.sid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RedisSessionStore) SessionRelease() {
|
2013-11-05 13:59:35 +00:00
|
|
|
defer rs.c.Close()
|
|
|
|
keys, err := redis.Values(rs.c.Do("HKEYS", rs.sid))
|
|
|
|
if err == nil {
|
|
|
|
for _, key := range keys {
|
|
|
|
if val, ok := rs.values[key]; ok {
|
|
|
|
rs.c.Do("HSET", rs.sid, key, val)
|
|
|
|
rs.Delete(key)
|
|
|
|
} else {
|
|
|
|
rs.c.Do("HDEL", rs.sid, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(rs.values) > 0 {
|
|
|
|
for k, v := range rs.values {
|
|
|
|
rs.c.Do("HSET", rs.sid, k, v)
|
|
|
|
}
|
|
|
|
}
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RedisProvider struct {
|
|
|
|
maxlifetime int64
|
|
|
|
savePath string
|
2013-10-28 14:19:37 +00:00
|
|
|
poolsize int
|
|
|
|
password string
|
|
|
|
poollist *redis.Pool
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 14:19:37 +00:00
|
|
|
//savepath like redisserveraddr,poolsize,password
|
|
|
|
//127.0.0.1:6379,100,astaxie
|
|
|
|
func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error {
|
|
|
|
rp.maxlifetime = maxlifetime
|
|
|
|
configs := strings.Split(savePath, ",")
|
|
|
|
if len(configs) > 0 {
|
|
|
|
rp.savePath = configs[0]
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
2013-10-28 14:19:37 +00:00
|
|
|
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
|
2013-05-14 15:55:50 +00:00
|
|
|
}
|
2013-10-28 14:19:37 +00:00
|
|
|
if len(configs) > 2 {
|
|
|
|
rp.password = configs[2]
|
2013-05-14 15:55:50 +00:00
|
|
|
}
|
2013-10-28 14:19:37 +00:00
|
|
|
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)
|
2013-04-05 15:50:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *RedisProvider) SessionRead(sid string) (SessionStore, error) {
|
2013-10-28 14:19:37 +00:00
|
|
|
c := rp.poollist.Get()
|
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
|
|
|
}
|
2013-10-28 14:19:37 +00:00
|
|
|
c.Do("EXPIRE", sid, rp.maxlifetime)
|
2013-11-05 13:59:35 +00:00
|
|
|
kvs, err := redis.Values(c.Do("HGETALL", sid))
|
|
|
|
vals := make(map[interface{}]interface{})
|
|
|
|
var key interface{}
|
|
|
|
if err == nil {
|
|
|
|
for k, v := range kvs {
|
|
|
|
if k%2 == 0 {
|
|
|
|
key = v
|
|
|
|
} else {
|
|
|
|
vals[key] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rs := &RedisSessionStore{c: c, sid: sid, values: vals}
|
2013-04-05 15:50:53 +00:00
|
|
|
return rs, nil
|
|
|
|
}
|
|
|
|
|
2013-09-26 10:07:00 +00:00
|
|
|
func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) {
|
2013-10-28 14:19:37 +00:00
|
|
|
c := rp.poollist.Get()
|
2013-09-26 10:07:00 +00:00
|
|
|
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)
|
2013-10-28 14:19:37 +00:00
|
|
|
c.Do("EXPIRE", sid, rp.maxlifetime)
|
2013-11-05 13:59:35 +00:00
|
|
|
kvs, err := redis.Values(c.Do("HGETALL", sid))
|
|
|
|
vals := make(map[interface{}]interface{})
|
|
|
|
var key interface{}
|
|
|
|
if err == nil {
|
|
|
|
for k, v := range kvs {
|
|
|
|
if k%2 == 0 {
|
|
|
|
key = v
|
|
|
|
} else {
|
|
|
|
vals[key] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rs := &RedisSessionStore{c: c, sid: sid, values: vals}
|
2013-09-26 10:07:00 +00:00
|
|
|
return rs, nil
|
|
|
|
}
|
|
|
|
|
2013-04-05 15:50:53 +00:00
|
|
|
func (rp *RedisProvider) SessionDestroy(sid string) error {
|
2013-10-28 14:19:37 +00:00
|
|
|
c := rp.poollist.Get()
|
2013-04-05 15:50:53 +00:00
|
|
|
c.Do("DEL", sid)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *RedisProvider) SessionGC() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-11-01 16:16:10 +00:00
|
|
|
//@todo
|
|
|
|
func (rp *RedisProvider) SessionAll() int {
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2013-04-05 15:50:53 +00:00
|
|
|
func init() {
|
|
|
|
Register("redis", redispder)
|
|
|
|
}
|