diff options
author | Hank Shen <swhbox@foxmail.com> | 2021-05-18 17:43:52 +0300 |
---|---|---|
committer | Hank Shen <swhbox@foxmail.com> | 2021-05-18 17:43:52 +0300 |
commit | 86db493d73044ebad01ac419dd380f45298bdd35 (patch) | |
tree | bdfe083202323f13bb987adcfcaadb45c09b14ab /store.go | |
parent | 1f34ca1aac1e31d7e577d3b4aa2c0d96474f6ca0 (diff) |
updatev0.0.2
Diffstat (limited to 'store.go')
-rw-r--r-- | store.go | 25 |
1 files changed, 24 insertions, 1 deletions
@@ -11,6 +11,7 @@ import ( "path/filepath" "strings" "sync" + "time" "github.com/admpub/securecookie" "github.com/webx-top/echo" @@ -154,7 +155,7 @@ var fileMutex sync.RWMutex // // See NewCookieStore() for a description of the other parameters. func NewFilesystemStore(path string, keyPairs ...[]byte) *FilesystemStore { - if path == "" { + if len(path) == 0 { path = os.TempDir() } fs := &FilesystemStore{ @@ -307,3 +308,25 @@ func (s *FilesystemStore) load(ctx echo.Context, session *Session) error { } return nil } + +func (s *FilesystemStore) DeleteExpired(maxAge float64) error { + if maxAge <= 0 { + return nil + } + err := filepath.Walk(s.path, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if info.IsDir() { + return err + } + if !strings.HasPrefix(info.Name(), `session_`) { + return err + } + if time.Since(info.ModTime()).Seconds() > maxAge { + err = os.Remove(path) + } + return err + }) + return err +} |