1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-13 04:00:40 +00:00

Add ctx to session API

This commit is contained in:
Ming Deng
2020-08-30 15:39:07 +00:00
parent 0019e0fc1b
commit 670064686e
23 changed files with 302 additions and 288 deletions

View File

@ -41,6 +41,7 @@
package mysql
import (
"context"
"database/sql"
"net/http"
"sync"
@ -67,7 +68,7 @@ type SessionStore struct {
// Set value in mysql session.
// it is temp value in map.
func (st *SessionStore) Set(key, value interface{}) error {
func (st *SessionStore) Set(ctx context.Context, key, value interface{}) error {
st.lock.Lock()
defer st.lock.Unlock()
st.values[key] = value
@ -75,7 +76,7 @@ func (st *SessionStore) Set(key, value interface{}) error {
}
// Get value from mysql session
func (st *SessionStore) Get(key interface{}) interface{} {
func (st *SessionStore) Get(ctx context.Context, key interface{}) interface{} {
st.lock.RLock()
defer st.lock.RUnlock()
if v, ok := st.values[key]; ok {
@ -85,7 +86,7 @@ func (st *SessionStore) Get(key interface{}) interface{} {
}
// Delete value in mysql session
func (st *SessionStore) Delete(key interface{}) error {
func (st *SessionStore) Delete(ctx context.Context, key interface{}) error {
st.lock.Lock()
defer st.lock.Unlock()
delete(st.values, key)
@ -93,7 +94,7 @@ func (st *SessionStore) Delete(key interface{}) error {
}
// Flush clear all values in mysql session
func (st *SessionStore) Flush() error {
func (st *SessionStore) Flush(context.Context) error {
st.lock.Lock()
defer st.lock.Unlock()
st.values = make(map[interface{}]interface{})
@ -101,13 +102,13 @@ func (st *SessionStore) Flush() error {
}
// SessionID get session id of this mysql session store
func (st *SessionStore) SessionID() string {
func (st *SessionStore) SessionID(context.Context) string {
return st.sid
}
// SessionRelease save mysql session values to database.
// must call this method to save values to database.
func (st *SessionStore) SessionRelease(w http.ResponseWriter) {
func (st *SessionStore) SessionRelease(ctx context.Context, w http.ResponseWriter) {
defer st.c.Close()
b, err := session.EncodeGob(st.values)
if err != nil {
@ -134,14 +135,14 @@ func (mp *Provider) connectInit() *sql.DB {
// SessionInit init mysql session.
// savepath is the connection string of mysql.
func (mp *Provider) SessionInit(maxlifetime int64, savePath string) error {
func (mp *Provider) SessionInit(ctx context.Context, maxlifetime int64, savePath string) error {
mp.maxlifetime = maxlifetime
mp.savePath = savePath
return nil
}
// SessionRead get mysql session by sid
func (mp *Provider) SessionRead(sid string) (session.Store, error) {
func (mp *Provider) SessionRead(ctx context.Context, sid string) (session.Store, error) {
c := mp.connectInit()
row := c.QueryRow("select session_data from "+TableName+" where session_key=?", sid)
var sessiondata []byte
@ -164,7 +165,7 @@ func (mp *Provider) SessionRead(sid string) (session.Store, error) {
}
// SessionExist check mysql session exist
func (mp *Provider) SessionExist(sid string) (bool, error) {
func (mp *Provider) SessionExist(ctx context.Context, sid string) (bool, error) {
c := mp.connectInit()
defer c.Close()
row := c.QueryRow("select session_data from "+TableName+" where session_key=?", sid)
@ -180,7 +181,7 @@ func (mp *Provider) SessionExist(sid string) (bool, error) {
}
// SessionRegenerate generate new sid for mysql session
func (mp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
func (mp *Provider) SessionRegenerate(ctx context.Context, oldsid, sid string) (session.Store, error) {
c := mp.connectInit()
row := c.QueryRow("select session_data from "+TableName+" where session_key=?", oldsid)
var sessiondata []byte
@ -203,7 +204,7 @@ func (mp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error)
}
// SessionDestroy delete mysql session by sid
func (mp *Provider) SessionDestroy(sid string) error {
func (mp *Provider) SessionDestroy(ctx context.Context, sid string) error {
c := mp.connectInit()
c.Exec("DELETE FROM "+TableName+" where session_key=?", sid)
c.Close()
@ -211,14 +212,14 @@ func (mp *Provider) SessionDestroy(sid string) error {
}
// SessionGC delete expired values in mysql session
func (mp *Provider) SessionGC() {
func (mp *Provider) SessionGC(context.Context) {
c := mp.connectInit()
c.Exec("DELETE from "+TableName+" where session_expiry < ?", time.Now().Unix()-mp.maxlifetime)
c.Close()
}
// SessionAll count values in mysql session
func (mp *Provider) SessionAll() int {
func (mp *Provider) SessionAll(context.Context) int {
c := mp.connectInit()
defer c.Close()
var total int