1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 03:11:30 +00:00
This commit is contained in:
astaxie 2013-11-05 21:59:35 +08:00
parent 23d79b8b05
commit a443a798e3
4 changed files with 79 additions and 41 deletions

View File

@ -28,7 +28,6 @@ func (fs *FileSessionStore) Set(key, value interface{}) error {
fs.lock.Lock() fs.lock.Lock()
defer fs.lock.Unlock() defer fs.lock.Unlock()
fs.values[key] = value fs.values[key] = value
fs.updatecontent()
return nil return nil
} }
@ -47,7 +46,6 @@ func (fs *FileSessionStore) Delete(key interface{}) error {
fs.lock.Lock() fs.lock.Lock()
defer fs.lock.Unlock() defer fs.lock.Unlock()
delete(fs.values, key) delete(fs.values, key)
fs.updatecontent()
return nil return nil
} }
@ -55,7 +53,6 @@ func (fs *FileSessionStore) Flush() error {
fs.lock.Lock() fs.lock.Lock()
defer fs.lock.Unlock() defer fs.lock.Unlock()
fs.values = make(map[interface{}]interface{}) fs.values = make(map[interface{}]interface{})
fs.updatecontent()
return nil return nil
} }
@ -64,10 +61,7 @@ func (fs *FileSessionStore) SessionID() string {
} }
func (fs *FileSessionStore) SessionRelease() { func (fs *FileSessionStore) SessionRelease() {
fs.f.Close() defer fs.f.Close()
}
func (fs *FileSessionStore) updatecontent() {
b, err := encodeGob(fs.values) b, err := encodeGob(fs.values)
if err != nil { if err != nil {
return return

View File

@ -27,7 +27,6 @@ func (st *MysqlSessionStore) Set(key, value interface{}) error {
st.lock.Lock() st.lock.Lock()
defer st.lock.Unlock() defer st.lock.Unlock()
st.values[key] = value st.values[key] = value
st.updatemysql()
return nil return nil
} }
@ -46,7 +45,6 @@ func (st *MysqlSessionStore) Delete(key interface{}) error {
st.lock.Lock() st.lock.Lock()
defer st.lock.Unlock() defer st.lock.Unlock()
delete(st.values, key) delete(st.values, key)
st.updatemysql()
return nil return nil
} }
@ -54,7 +52,6 @@ func (st *MysqlSessionStore) Flush() error {
st.lock.Lock() st.lock.Lock()
defer st.lock.Unlock() defer st.lock.Unlock()
st.values = make(map[interface{}]interface{}) st.values = make(map[interface{}]interface{})
st.updatemysql()
return nil return nil
} }
@ -62,7 +59,8 @@ func (st *MysqlSessionStore) SessionID() string {
return st.sid return st.sid
} }
func (st *MysqlSessionStore) updatemysql() { func (st *MysqlSessionStore) SessionRelease() {
defer st.c.Close()
if len(st.values) > 0 { if len(st.values) > 0 {
b, err := encodeGob(st.values) b, err := encodeGob(st.values)
if err != nil { if err != nil {
@ -72,10 +70,6 @@ func (st *MysqlSessionStore) updatemysql() {
} }
} }
func (st *MysqlSessionStore) SessionRelease() {
st.c.Close()
}
type MysqlProvider struct { type MysqlProvider struct {
maxlifetime int64 maxlifetime int64
savePath string savePath string

View File

@ -4,6 +4,7 @@ import (
"github.com/garyburd/redigo/redis" "github.com/garyburd/redigo/redis"
"strconv" "strconv"
"strings" "strings"
"sync"
) )
var redispder = &RedisProvider{} var redispder = &RedisProvider{}
@ -15,31 +16,40 @@ var redisPool chan redis.Conn
type RedisSessionStore struct { type RedisSessionStore struct {
c redis.Conn c redis.Conn
sid string sid string
lock sync.RWMutex
values map[interface{}]interface{}
} }
func (rs *RedisSessionStore) Set(key, value interface{}) error { func (rs *RedisSessionStore) Set(key, value interface{}) error {
//_, err := rs.c.Do("HSET", rs.sid, key, value) rs.lock.Lock()
_, err := rs.c.Do("HSET", rs.sid, key, value) defer rs.lock.Unlock()
return err rs.values[key] = value
return nil
} }
func (rs *RedisSessionStore) Get(key interface{}) interface{} { func (rs *RedisSessionStore) Get(key interface{}) interface{} {
reply, err := rs.c.Do("HGET", rs.sid, key) rs.lock.RLock()
if err != nil { defer rs.lock.RUnlock()
if v, ok := rs.values[key]; ok {
return v
} else {
return nil return nil
} }
return reply return nil
} }
func (rs *RedisSessionStore) Delete(key interface{}) error { func (rs *RedisSessionStore) Delete(key interface{}) error {
//_, err := rs.c.Do("HDEL", rs.sid, key) rs.lock.Lock()
_, err := rs.c.Do("HDEL", rs.sid, key) defer rs.lock.Unlock()
return err delete(rs.values, key)
return nil
} }
func (rs *RedisSessionStore) Flush() error { func (rs *RedisSessionStore) Flush() error {
_, err := rs.c.Do("DEL", rs.sid) rs.lock.Lock()
return err defer rs.lock.Unlock()
rs.values = make(map[interface{}]interface{})
return nil
} }
func (rs *RedisSessionStore) SessionID() string { func (rs *RedisSessionStore) SessionID() string {
@ -47,7 +57,23 @@ func (rs *RedisSessionStore) SessionID() string {
} }
func (rs *RedisSessionStore) SessionRelease() { func (rs *RedisSessionStore) SessionRelease() {
rs.c.Close() 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)
}
}
} }
type RedisProvider struct { type RedisProvider struct {
@ -103,7 +129,19 @@ func (rp *RedisProvider) SessionRead(sid string) (SessionStore, error) {
c.Do("HSET", sid, sid, rp.maxlifetime) c.Do("HSET", sid, sid, rp.maxlifetime)
} }
c.Do("EXPIRE", sid, rp.maxlifetime) c.Do("EXPIRE", sid, rp.maxlifetime)
rs := &RedisSessionStore{c: c, sid: sid} 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}
return rs, nil return rs, nil
} }
@ -114,7 +152,19 @@ func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (SessionStore, er
} }
c.Do("RENAME", oldsid, sid) c.Do("RENAME", oldsid, sid)
c.Do("EXPIRE", sid, rp.maxlifetime) c.Do("EXPIRE", sid, rp.maxlifetime)
rs := &RedisSessionStore{c: c, sid: sid} 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}
return rs, nil return rs, nil
} }

View File

@ -18,7 +18,7 @@ type SessionStore interface {
Get(key interface{}) interface{} //get session value Get(key interface{}) interface{} //get session value
Delete(key interface{}) error //delete session value Delete(key interface{}) error //delete session value
SessionID() string //back current sessionID SessionID() string //back current sessionID
SessionRelease() // release the resource SessionRelease() // release the resource & save data to provider
Flush() error //delete all data Flush() error //delete all data
} }
@ -129,17 +129,9 @@ func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (se
if manager.maxage >= 0 { if manager.maxage >= 0 {
cookie.MaxAge = manager.maxage cookie.MaxAge = manager.maxage
} }
//cookie.Expires = time.Now().Add(time.Duration(manager.maxlifetime) * time.Second)
http.SetCookie(w, cookie) http.SetCookie(w, cookie)
r.AddCookie(cookie) r.AddCookie(cookie)
} else { } else {
//cookie.Expires = time.Now().Add(time.Duration(manager.maxlifetime) * time.Second)
//cookie.HttpOnly = true
//cookie.Path = "/"
//if manager.maxage >= 0 {
// cookie.MaxAge = manager.maxage
// http.SetCookie(w, cookie)
//}
sid, _ := url.QueryUnescape(cookie.Value) sid, _ := url.QueryUnescape(cookie.Value)
session, _ = manager.provider.SessionRead(sid) session, _ = manager.provider.SessionRead(sid)
} }
@ -200,8 +192,16 @@ func (manager *Manager) GetActiveSession() int {
return manager.provider.SessionAll() return manager.provider.SessionAll()
} }
//remote_addr cruunixnano randdata func (manager *Manager) SetHashFunc(hasfunc, hashkey string) {
manager.hashfunc = hasfunc
manager.hashkey = hashkey
}
func (manager *Manager) SetSecure(secure bool) {
manager.secure = secure
}
//remote_addr cruunixnano randdata
func (manager *Manager) sessionId(r *http.Request) (sid string) { func (manager *Manager) sessionId(r *http.Request) (sid string) {
bs := make([]byte, 24) bs := make([]byte, 24)
if _, err := io.ReadFull(rand.Reader, bs); err != nil { if _, err := io.ReadFull(rand.Reader, bs); err != nil {