Browse Source

Optimization

Vova Tkach 6 years ago
parent
commit
2a33d29265
2 changed files with 5 additions and 6 deletions
  1. 1 3
      session/clean.go
  2. 4 3
      session/session.go

+ 1 - 3
session/clean.go

@@ -11,11 +11,9 @@ func Clean(tmpdir string) error {
 	if err != nil {
 		return err
 	}
-	now := time.Now()
-	exp := 24 * time.Hour
 	for _, file := range files {
 		if len(file.Name()) == 40 {
-			if diff := now.Sub(file.ModTime()); diff > exp {
+			if diff := time.Now().Sub(file.ModTime()); diff > 24*time.Hour {
 				err = os.Remove(tmpdir + string(os.PathSeparator) + file.Name())
 				if err != nil {
 					return err

+ 4 - 3
session/session.go

@@ -32,7 +32,8 @@ func New(w http.ResponseWriter, r *http.Request, tmpdir string) *Session {
 	if err == nil && len(cookie.Value) == 40 {
 		// Load from file
 		sess.i = cookie.Value
-		f, err := os.Open(sess.d + string(os.PathSeparator) + sess.i)
+		fname := sess.d + string(os.PathSeparator) + sess.i
+		f, err := os.Open(fname)
 		if err == nil {
 			defer f.Close()
 			dec := json.NewDecoder(f)
@@ -42,9 +43,9 @@ func New(w http.ResponseWriter, r *http.Request, tmpdir string) *Session {
 			}
 
 			// Update file last modify time if needs
-			if info, err := os.Stat(sess.d + string(os.PathSeparator) + sess.i); err == nil {
+			if info, err := os.Stat(fname); err == nil {
 				if time.Now().Sub(info.ModTime()) > 30*time.Minute {
-					_ = os.Chtimes(sess.d+string(os.PathSeparator)+sess.i, time.Now(), time.Now())
+					_ = os.Chtimes(fname, time.Now(), time.Now())
 				}
 			}
 		}