mirror of
https://github.com/astaxie/beego.git
synced 2025-07-10 07:40:19 +00:00
add comments for session packages, part 1
This commit is contained in:
@ -18,6 +18,7 @@ var (
|
||||
gcmaxlifetime int64
|
||||
)
|
||||
|
||||
// File session store
|
||||
type FileSessionStore struct {
|
||||
f *os.File
|
||||
sid string
|
||||
@ -25,6 +26,7 @@ type FileSessionStore struct {
|
||||
values map[interface{}]interface{}
|
||||
}
|
||||
|
||||
// Set value to file session
|
||||
func (fs *FileSessionStore) Set(key, value interface{}) error {
|
||||
fs.lock.Lock()
|
||||
defer fs.lock.Unlock()
|
||||
@ -32,6 +34,7 @@ func (fs *FileSessionStore) Set(key, value interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get value from file session
|
||||
func (fs *FileSessionStore) Get(key interface{}) interface{} {
|
||||
fs.lock.RLock()
|
||||
defer fs.lock.RUnlock()
|
||||
@ -43,6 +46,7 @@ func (fs *FileSessionStore) Get(key interface{}) interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete value in file session by given key
|
||||
func (fs *FileSessionStore) Delete(key interface{}) error {
|
||||
fs.lock.Lock()
|
||||
defer fs.lock.Unlock()
|
||||
@ -50,6 +54,7 @@ func (fs *FileSessionStore) Delete(key interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Clean all values in file session
|
||||
func (fs *FileSessionStore) Flush() error {
|
||||
fs.lock.Lock()
|
||||
defer fs.lock.Unlock()
|
||||
@ -57,10 +62,12 @@ func (fs *FileSessionStore) Flush() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get file session store id
|
||||
func (fs *FileSessionStore) SessionID() string {
|
||||
return fs.sid
|
||||
}
|
||||
|
||||
// Write file session to local file with Gob string
|
||||
func (fs *FileSessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
defer fs.f.Close()
|
||||
b, err := encodeGob(fs.values)
|
||||
@ -72,17 +79,23 @@ func (fs *FileSessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
fs.f.Write(b)
|
||||
}
|
||||
|
||||
// File session provider
|
||||
type FileProvider struct {
|
||||
maxlifetime int64
|
||||
savePath string
|
||||
}
|
||||
|
||||
// Init file session provider.
|
||||
// savePath sets the session files path.
|
||||
func (fp *FileProvider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
fp.maxlifetime = maxlifetime
|
||||
fp.savePath = savePath
|
||||
return nil
|
||||
}
|
||||
|
||||
// Read file session by sid.
|
||||
// if file is not exist, create it.
|
||||
// the file path is generated from sid string.
|
||||
func (fp *FileProvider) SessionRead(sid string) (SessionStore, error) {
|
||||
err := os.MkdirAll(path.Join(fp.savePath, string(sid[0]), string(sid[1])), 0777)
|
||||
if err != nil {
|
||||
@ -117,6 +130,8 @@ func (fp *FileProvider) SessionRead(sid string) (SessionStore, error) {
|
||||
return ss, nil
|
||||
}
|
||||
|
||||
// Check file session exist.
|
||||
// it checkes the file named from sid exist or not.
|
||||
func (fp *FileProvider) SessionExist(sid string) bool {
|
||||
_, err := os.Stat(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
|
||||
if err == nil {
|
||||
@ -126,16 +141,20 @@ func (fp *FileProvider) SessionExist(sid string) bool {
|
||||
}
|
||||
}
|
||||
|
||||
// Remove all files in this save path
|
||||
func (fp *FileProvider) SessionDestroy(sid string) error {
|
||||
os.Remove(path.Join(fp.savePath))
|
||||
return nil
|
||||
}
|
||||
|
||||
// Recycle files in save path
|
||||
func (fp *FileProvider) SessionGC() {
|
||||
gcmaxlifetime = fp.maxlifetime
|
||||
filepath.Walk(fp.savePath, gcpath)
|
||||
}
|
||||
|
||||
// Get active file session number.
|
||||
// it walks save path to count files.
|
||||
func (fp *FileProvider) SessionAll() int {
|
||||
a := &activeSession{}
|
||||
err := filepath.Walk(fp.savePath, func(path string, f os.FileInfo, err error) error {
|
||||
@ -148,6 +167,8 @@ func (fp *FileProvider) SessionAll() int {
|
||||
return a.total
|
||||
}
|
||||
|
||||
// Generate new sid for file session.
|
||||
// it delete old file and create new file named from new sid.
|
||||
func (fp *FileProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) {
|
||||
err := os.MkdirAll(path.Join(fp.savePath, string(oldsid[0]), string(oldsid[1])), 0777)
|
||||
if err != nil {
|
||||
@ -197,6 +218,7 @@ func (fp *FileProvider) SessionRegenerate(oldsid, sid string) (SessionStore, err
|
||||
return ss, nil
|
||||
}
|
||||
|
||||
// remove file in save path if expired
|
||||
func gcpath(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
|
Reference in New Issue
Block a user