Browse Source

Basket memory cleanup function based on session file

Vova Tkach 5 years ago
parent
commit
4adb423564

+ 1 - 1
assets/template/header_html_file.go

@@ -30,7 +30,7 @@ var VarHeaderHtmlFile = []byte(`<!doctype html>
 			var ShopBasketTotal = 'Total sum:';
 			var ShopBasketBtnContinue = 'Continue Shopping';
 			var ShopBasketBtnOrder = 'Make order';
-			var ShopBasketEmpty = 'You basket currently empty...';
+			var ShopBasketEmpty = 'Your basket currently empty...';
 		</script>
 	</head>
 	<body id="body" class="fixed-top-bar">

+ 21 - 0
engine/basket/basket.go

@@ -2,10 +2,13 @@ package basket
 
 import (
 	"net/http"
+	"os"
 	"sync"
 
+	"golang-fave/consts"
 	"golang-fave/engine/sqlw"
 	"golang-fave/engine/wrapper/config"
+	"golang-fave/utils"
 )
 
 type SBParam struct {
@@ -132,3 +135,21 @@ func (this *Basket) ProductsCount(p *SBParam) int {
 
 	return 0
 }
+
+func (this *Basket) Cleanup(p *SBParam) {
+	this.Lock()
+	defer this.Unlock()
+
+	for host_name, host_data := range this.hosts {
+		var remove []string
+		for session_id, _ := range host_data.sessions {
+			session_file := consts.ParamWwwDir + string(os.PathSeparator) + host_name + string(os.PathSeparator) + "tmp" + string(os.PathSeparator) + session_id
+			if !utils.IsFileExists(session_file) {
+				remove = append(remove, session_id)
+			}
+		}
+		for _, session_id := range remove {
+			delete(host_data.sessions, session_id)
+		}
+	}
+}

+ 1 - 1
hosts/localhost/template/header.html

@@ -28,7 +28,7 @@
 			var ShopBasketTotal = 'Total sum:';
 			var ShopBasketBtnContinue = 'Continue Shopping';
 			var ShopBasketBtnOrder = 'Make order';
-			var ShopBasketEmpty = 'You basket currently empty...';
+			var ShopBasketEmpty = 'Your basket currently empty...';
 		</script>
 	</head>
 	<body id="body" class="fixed-top-bar">