1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 02:20:53 +00:00

add comments for session packages, part 2

This commit is contained in:
傅小黑 2014-01-29 18:15:09 +08:00
parent 3f0ec5c0ca
commit 682544165f
3 changed files with 63 additions and 12 deletions

View File

@ -9,6 +9,8 @@ import (
var mempder = &MemProvider{list: list.New(), sessions: make(map[string]*list.Element)} var mempder = &MemProvider{list: list.New(), sessions: make(map[string]*list.Element)}
// memory session store.
// it saved sessions in a map in memory.
type MemSessionStore struct { type MemSessionStore struct {
sid string //session id sid string //session id
timeAccessed time.Time //last access time timeAccessed time.Time //last access time
@ -16,6 +18,7 @@ type MemSessionStore struct {
lock sync.RWMutex lock sync.RWMutex
} }
// set value to memory session
func (st *MemSessionStore) Set(key, value interface{}) error { func (st *MemSessionStore) Set(key, value interface{}) error {
st.lock.Lock() st.lock.Lock()
defer st.lock.Unlock() defer st.lock.Unlock()
@ -23,6 +26,7 @@ func (st *MemSessionStore) Set(key, value interface{}) error {
return nil return nil
} }
// get value from memory session by key
func (st *MemSessionStore) Get(key interface{}) interface{} { func (st *MemSessionStore) Get(key interface{}) interface{} {
st.lock.RLock() st.lock.RLock()
defer st.lock.RUnlock() defer st.lock.RUnlock()
@ -34,6 +38,7 @@ func (st *MemSessionStore) Get(key interface{}) interface{} {
return nil return nil
} }
// delete in memory session by key
func (st *MemSessionStore) Delete(key interface{}) error { func (st *MemSessionStore) Delete(key interface{}) error {
st.lock.Lock() st.lock.Lock()
defer st.lock.Unlock() defer st.lock.Unlock()
@ -41,6 +46,7 @@ func (st *MemSessionStore) Delete(key interface{}) error {
return nil return nil
} }
// clear all values in memory session
func (st *MemSessionStore) Flush() error { func (st *MemSessionStore) Flush() error {
st.lock.Lock() st.lock.Lock()
defer st.lock.Unlock() defer st.lock.Unlock()
@ -48,27 +54,31 @@ func (st *MemSessionStore) Flush() error {
return nil return nil
} }
// get this id of memory session store
func (st *MemSessionStore) SessionID() string { func (st *MemSessionStore) SessionID() string {
return st.sid return st.sid
} }
// Implement method, no used.
func (st *MemSessionStore) SessionRelease(w http.ResponseWriter) { func (st *MemSessionStore) SessionRelease(w http.ResponseWriter) {
} }
type MemProvider struct { type MemProvider struct {
lock sync.RWMutex //用来锁 lock sync.RWMutex // locker
sessions map[string]*list.Element //用来存储在内存 sessions map[string]*list.Element // map in memory
list *list.List //用来做gc list *list.List // for gc
maxlifetime int64 maxlifetime int64
savePath string savePath string
} }
// init memory session
func (pder *MemProvider) SessionInit(maxlifetime int64, savePath string) error { func (pder *MemProvider) SessionInit(maxlifetime int64, savePath string) error {
pder.maxlifetime = maxlifetime pder.maxlifetime = maxlifetime
pder.savePath = savePath pder.savePath = savePath
return nil return nil
} }
// get memory session store by sid
func (pder *MemProvider) SessionRead(sid string) (SessionStore, error) { func (pder *MemProvider) SessionRead(sid string) (SessionStore, error) {
pder.lock.RLock() pder.lock.RLock()
if element, ok := pder.sessions[sid]; ok { if element, ok := pder.sessions[sid]; ok {
@ -87,6 +97,7 @@ func (pder *MemProvider) SessionRead(sid string) (SessionStore, error) {
return nil, nil return nil, nil
} }
// check session store exist in memory session by sid
func (pder *MemProvider) SessionExist(sid string) bool { func (pder *MemProvider) SessionExist(sid string) bool {
pder.lock.RLock() pder.lock.RLock()
defer pder.lock.RUnlock() defer pder.lock.RUnlock()
@ -97,6 +108,7 @@ func (pder *MemProvider) SessionExist(sid string) bool {
} }
} }
// generate new sid for session store in memory session
func (pder *MemProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) { func (pder *MemProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) {
pder.lock.RLock() pder.lock.RLock()
if element, ok := pder.sessions[oldsid]; ok { if element, ok := pder.sessions[oldsid]; ok {
@ -120,6 +132,7 @@ func (pder *MemProvider) SessionRegenerate(oldsid, sid string) (SessionStore, er
return nil, nil return nil, nil
} }
// delete session store in memory session by id
func (pder *MemProvider) SessionDestroy(sid string) error { func (pder *MemProvider) SessionDestroy(sid string) error {
pder.lock.Lock() pder.lock.Lock()
defer pder.lock.Unlock() defer pder.lock.Unlock()
@ -131,6 +144,7 @@ func (pder *MemProvider) SessionDestroy(sid string) error {
return nil return nil
} }
// clean expired session stores in memory session
func (pder *MemProvider) SessionGC() { func (pder *MemProvider) SessionGC() {
pder.lock.RLock() pder.lock.RLock()
for { for {
@ -152,10 +166,12 @@ func (pder *MemProvider) SessionGC() {
pder.lock.RUnlock() pder.lock.RUnlock()
} }
// get count number of memory session
func (pder *MemProvider) SessionAll() int { func (pder *MemProvider) SessionAll() int {
return pder.list.Len() return pder.list.Len()
} }
// expand time of session store by id in memory session
func (pder *MemProvider) SessionUpdate(sid string) error { func (pder *MemProvider) SessionUpdate(sid string) error {
pder.lock.Lock() pder.lock.Lock()
defer pder.lock.Unlock() defer pder.lock.Unlock()

View File

@ -1,8 +1,9 @@
package session package session
// mysql session support need create table as sql:
// CREATE TABLE `session` ( // CREATE TABLE `session` (
// `session_key` char(64) NOT NULL, // `session_key` char(64) NOT NULL,
// `session_data` blob, // session_data` blob,
// `session_expiry` int(11) unsigned NOT NULL, // `session_expiry` int(11) unsigned NOT NULL,
// PRIMARY KEY (`session_key`) // PRIMARY KEY (`session_key`)
// ) ENGINE=MyISAM DEFAULT CHARSET=utf8; // ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
@ -18,6 +19,7 @@ import (
var mysqlpder = &MysqlProvider{} var mysqlpder = &MysqlProvider{}
// mysql session store
type MysqlSessionStore struct { type MysqlSessionStore struct {
c *sql.DB c *sql.DB
sid string sid string
@ -25,6 +27,8 @@ type MysqlSessionStore struct {
values map[interface{}]interface{} values map[interface{}]interface{}
} }
// set value in mysql session.
// it is temp value in map.
func (st *MysqlSessionStore) Set(key, value interface{}) error { func (st *MysqlSessionStore) Set(key, value interface{}) error {
st.lock.Lock() st.lock.Lock()
defer st.lock.Unlock() defer st.lock.Unlock()
@ -32,6 +36,7 @@ func (st *MysqlSessionStore) Set(key, value interface{}) error {
return nil return nil
} }
// get value from mysql session
func (st *MysqlSessionStore) Get(key interface{}) interface{} { func (st *MysqlSessionStore) Get(key interface{}) interface{} {
st.lock.RLock() st.lock.RLock()
defer st.lock.RUnlock() defer st.lock.RUnlock()
@ -43,6 +48,7 @@ func (st *MysqlSessionStore) Get(key interface{}) interface{} {
return nil return nil
} }
// delete value in mysql session
func (st *MysqlSessionStore) Delete(key interface{}) error { func (st *MysqlSessionStore) Delete(key interface{}) error {
st.lock.Lock() st.lock.Lock()
defer st.lock.Unlock() defer st.lock.Unlock()
@ -50,6 +56,7 @@ func (st *MysqlSessionStore) Delete(key interface{}) error {
return nil return nil
} }
// clear all values in mysql session
func (st *MysqlSessionStore) Flush() error { func (st *MysqlSessionStore) Flush() error {
st.lock.Lock() st.lock.Lock()
defer st.lock.Unlock() defer st.lock.Unlock()
@ -57,10 +64,13 @@ func (st *MysqlSessionStore) Flush() error {
return nil return nil
} }
// get session id of this mysql session store
func (st *MysqlSessionStore) SessionID() string { func (st *MysqlSessionStore) SessionID() string {
return st.sid return st.sid
} }
// save mysql session values to database.
// must call this method to save values to database.
func (st *MysqlSessionStore) SessionRelease(w http.ResponseWriter) { func (st *MysqlSessionStore) SessionRelease(w http.ResponseWriter) {
defer st.c.Close() defer st.c.Close()
b, err := encodeGob(st.values) b, err := encodeGob(st.values)
@ -72,11 +82,13 @@ func (st *MysqlSessionStore) SessionRelease(w http.ResponseWriter) {
} }
// mysql session provider
type MysqlProvider struct { type MysqlProvider struct {
maxlifetime int64 maxlifetime int64
savePath string savePath string
} }
// connect to mysql
func (mp *MysqlProvider) connectInit() *sql.DB { func (mp *MysqlProvider) connectInit() *sql.DB {
db, e := sql.Open("mysql", mp.savePath) db, e := sql.Open("mysql", mp.savePath)
if e != nil { if e != nil {
@ -85,12 +97,15 @@ func (mp *MysqlProvider) connectInit() *sql.DB {
return db return db
} }
// init mysql session.
// savepath is the connection string of mysql.
func (mp *MysqlProvider) SessionInit(maxlifetime int64, savePath string) error { func (mp *MysqlProvider) SessionInit(maxlifetime int64, savePath string) error {
mp.maxlifetime = maxlifetime mp.maxlifetime = maxlifetime
mp.savePath = savePath mp.savePath = savePath
return nil return nil
} }
// get mysql session by sid
func (mp *MysqlProvider) SessionRead(sid string) (SessionStore, error) { func (mp *MysqlProvider) SessionRead(sid string) (SessionStore, error) {
c := mp.connectInit() c := mp.connectInit()
row := c.QueryRow("select session_data from session where session_key=?", sid) row := c.QueryRow("select session_data from session where session_key=?", sid)
@ -113,6 +128,7 @@ func (mp *MysqlProvider) SessionRead(sid string) (SessionStore, error) {
return rs, nil return rs, nil
} }
// check mysql session exist
func (mp *MysqlProvider) SessionExist(sid string) bool { func (mp *MysqlProvider) SessionExist(sid string) bool {
c := mp.connectInit() c := mp.connectInit()
defer c.Close() defer c.Close()
@ -126,6 +142,7 @@ func (mp *MysqlProvider) SessionExist(sid string) bool {
} }
} }
// generate new sid for mysql session
func (mp *MysqlProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) { func (mp *MysqlProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) {
c := mp.connectInit() c := mp.connectInit()
row := c.QueryRow("select session_data from session where session_key=?", oldsid) row := c.QueryRow("select session_data from session where session_key=?", oldsid)
@ -148,6 +165,7 @@ func (mp *MysqlProvider) SessionRegenerate(oldsid, sid string) (SessionStore, er
return rs, nil return rs, nil
} }
// delete mysql session by sid
func (mp *MysqlProvider) SessionDestroy(sid string) error { func (mp *MysqlProvider) SessionDestroy(sid string) error {
c := mp.connectInit() c := mp.connectInit()
c.Exec("DELETE FROM session where session_key=?", sid) c.Exec("DELETE FROM session where session_key=?", sid)
@ -155,6 +173,7 @@ func (mp *MysqlProvider) SessionDestroy(sid string) error {
return nil return nil
} }
// delete expired values in mysql session
func (mp *MysqlProvider) SessionGC() { func (mp *MysqlProvider) SessionGC() {
c := mp.connectInit() c := mp.connectInit()
c.Exec("DELETE from session where session_expiry < ?", time.Now().Unix()-mp.maxlifetime) c.Exec("DELETE from session where session_expiry < ?", time.Now().Unix()-mp.maxlifetime)
@ -162,6 +181,7 @@ func (mp *MysqlProvider) SessionGC() {
return return
} }
// count values in mysql session
func (mp *MysqlProvider) SessionAll() int { func (mp *MysqlProvider) SessionAll() int {
c := mp.connectInit() c := mp.connectInit()
defer c.Close() defer c.Close()

View File

@ -11,10 +11,12 @@ import (
var redispder = &RedisProvider{} var redispder = &RedisProvider{}
// redis max pool size
var MAX_POOL_SIZE = 100 var MAX_POOL_SIZE = 100
var redisPool chan redis.Conn var redisPool chan redis.Conn
// redis session store
type RedisSessionStore struct { type RedisSessionStore struct {
p *redis.Pool p *redis.Pool
sid string sid string
@ -23,6 +25,7 @@ type RedisSessionStore struct {
maxlifetime int64 maxlifetime int64
} }
// set value in redis session
func (rs *RedisSessionStore) Set(key, value interface{}) error { func (rs *RedisSessionStore) Set(key, value interface{}) error {
rs.lock.Lock() rs.lock.Lock()
defer rs.lock.Unlock() defer rs.lock.Unlock()
@ -30,6 +33,7 @@ func (rs *RedisSessionStore) Set(key, value interface{}) error {
return nil return nil
} }
// get value in redis session
func (rs *RedisSessionStore) Get(key interface{}) interface{} { func (rs *RedisSessionStore) Get(key interface{}) interface{} {
rs.lock.RLock() rs.lock.RLock()
defer rs.lock.RUnlock() defer rs.lock.RUnlock()
@ -41,6 +45,7 @@ func (rs *RedisSessionStore) Get(key interface{}) interface{} {
return nil return nil
} }
// delete value in redis session
func (rs *RedisSessionStore) Delete(key interface{}) error { func (rs *RedisSessionStore) Delete(key interface{}) error {
rs.lock.Lock() rs.lock.Lock()
defer rs.lock.Unlock() defer rs.lock.Unlock()
@ -48,6 +53,7 @@ func (rs *RedisSessionStore) Delete(key interface{}) error {
return nil return nil
} }
// clear all values in redis session
func (rs *RedisSessionStore) Flush() error { func (rs *RedisSessionStore) Flush() error {
rs.lock.Lock() rs.lock.Lock()
defer rs.lock.Unlock() defer rs.lock.Unlock()
@ -55,10 +61,12 @@ func (rs *RedisSessionStore) Flush() error {
return nil return nil
} }
// get redis session id
func (rs *RedisSessionStore) SessionID() string { func (rs *RedisSessionStore) SessionID() string {
return rs.sid return rs.sid
} }
// save session values to redis
func (rs *RedisSessionStore) SessionRelease(w http.ResponseWriter) { func (rs *RedisSessionStore) SessionRelease(w http.ResponseWriter) {
c := rs.p.Get() c := rs.p.Get()
defer c.Close() defer c.Close()
@ -77,6 +85,7 @@ func (rs *RedisSessionStore) SessionRelease(w http.ResponseWriter) {
c.Do("SET", rs.sid, string(b), "EX", rs.maxlifetime) c.Do("SET", rs.sid, string(b), "EX", rs.maxlifetime)
} }
// redis session provider
type RedisProvider struct { type RedisProvider struct {
maxlifetime int64 maxlifetime int64
savePath string savePath string
@ -85,8 +94,9 @@ type RedisProvider struct {
poollist *redis.Pool poollist *redis.Pool
} }
// init redis session
// savepath like redis server addr,pool size,password // savepath like redis server addr,pool size,password
//127.0.0.1:6379,100,astaxie // e.g. 127.0.0.1:6379,100,astaxie
func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error { func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error {
rp.maxlifetime = maxlifetime rp.maxlifetime = maxlifetime
configs := strings.Split(savePath, ",") configs := strings.Split(savePath, ",")
@ -122,6 +132,7 @@ func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error {
return nil return nil
} }
// read redis session by sid
func (rp *RedisProvider) SessionRead(sid string) (SessionStore, error) { func (rp *RedisProvider) SessionRead(sid string) (SessionStore, error) {
c := rp.poollist.Get() c := rp.poollist.Get()
defer c.Close() defer c.Close()
@ -141,6 +152,7 @@ func (rp *RedisProvider) SessionRead(sid string) (SessionStore, error) {
return rs, nil return rs, nil
} }
// check redis session exist by sid
func (rp *RedisProvider) SessionExist(sid string) bool { func (rp *RedisProvider) SessionExist(sid string) bool {
c := rp.poollist.Get() c := rp.poollist.Get()
defer c.Close() defer c.Close()
@ -152,6 +164,7 @@ func (rp *RedisProvider) SessionExist(sid string) bool {
} }
} }
// generate new sid for redis session
func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) { func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) {
c := rp.poollist.Get() c := rp.poollist.Get()
defer c.Close() defer c.Close()
@ -181,6 +194,7 @@ func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (SessionStore, er
return rs, nil return rs, nil
} }
// delete redis session by id
func (rp *RedisProvider) SessionDestroy(sid string) error { func (rp *RedisProvider) SessionDestroy(sid string) error {
c := rp.poollist.Get() c := rp.poollist.Get()
defer c.Close() defer c.Close()
@ -189,6 +203,7 @@ func (rp *RedisProvider) SessionDestroy(sid string) error {
return nil return nil
} }
// Impelment method, no used.
func (rp *RedisProvider) SessionGC() { func (rp *RedisProvider) SessionGC() {
return return
} }