diff --git a/.example/net/ghttp/server/session/basic/session.go b/.example/net/ghttp/server/session/basic/session.go index d7ae0e1ec..c7a21a684 100644 --- a/.example/net/ghttp/server/session/basic/session.go +++ b/.example/net/ghttp/server/session/basic/session.go @@ -8,7 +8,6 @@ import ( func main() { s := g.Server() - s.SetSessionCookieMaxAge(0) s.Group("/", func(group *ghttp.RouterGroup) { group.GET("/set", func(r *ghttp.Request) { r.Session.Set("time", gtime.Timestamp()) diff --git a/os/gsession/gsession_storage_file.go b/os/gsession/gsession_storage_file.go index f67aee337..51936a499 100644 --- a/os/gsession/gsession_storage_file.go +++ b/os/gsession/gsession_storage_file.go @@ -11,7 +11,6 @@ import ( "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/intlog" "github.com/gogf/gf/internal/json" - "github.com/gogf/gf/util/grand" "os" "time" @@ -36,12 +35,10 @@ type StorageFile struct { } var ( - DefaultStorageFilePath = gfile.TempDir("gsessions") - DefaultStorageFileCryptoKey = []byte("Session storage file crypto key!") - DefaultStorageFileCryptoEnabled = false - DefaultStorageFileLoopInterval = 10 * time.Second - DefaultStorageFileRemoveIntervalMin = time.Hour - DefaultStorageFileRemoveIntervalMax = time.Hour * 24 + DefaultStorageFilePath = gfile.TempDir("gsessions") + DefaultStorageFileCryptoKey = []byte("Session storage file crypto key!") + DefaultStorageFileCryptoEnabled = false + DefaultStorageFileLoopInterval = 10 * time.Second ) // NewStorageFile creates and returns a file storage object for session. @@ -69,10 +66,6 @@ func NewStorageFile(path ...string) *StorageFile { } gtimer.AddSingleton(DefaultStorageFileLoopInterval, s.updateSessionTimely) - gtimer.AddOnce( - grand.D(DefaultStorageFileRemoveIntervalMin, DefaultStorageFileRemoveIntervalMax), - s.checkAndRemoveSessionTimely, - ) return s } @@ -93,37 +86,6 @@ func (s *StorageFile) updateSessionTimely() { } } -// checkAndRemoveSessionTimely checks the session storage directory path and removes the expired session files. -func (s *StorageFile) checkAndRemoveSessionTimely() { - defer gtimer.AddOnce( - grand.D(DefaultStorageFileRemoveIntervalMin, DefaultStorageFileRemoveIntervalMax), - s.checkAndRemoveSessionTimely, - ) - - var ( - timestampMilliFile int64 - timestampMilliNow = gtime.Now().TimestampMilli() - files, _ = gfile.ScanDirFile(s.path, "*") - timestampMilliBytes = make([]byte, 8) - ) - for _, path := range files { - file, err := gfile.OpenWithFlag(path, os.O_RDONLY) - if err != nil { - continue - } - if _, err := file.Read(timestampMilliBytes); err == nil { - timestampMilliFile = gbinary.DecodeToInt64(timestampMilliBytes) - if timestampMilliNow >= timestampMilliFile { - intlog.Printf( - "remove expired session file: %s, result:%v", - path, gfile.Remove(path), - ) - } - } - file.Close() - } -} - // SetCryptoKey sets the crypto key for session storage. // The crypto key is used when crypto feature is enabled. func (s *StorageFile) SetCryptoKey(key []byte) {