浏览代码

feat:内存锁

wangshan 1 年之前
父节点
当前提交
57deb46678
共有 2 个文件被更改,包括 14 次插入2 次删除
  1. 7 1
      src/jfw/front/shorturl.go
  2. 7 1
      src/jfw/modules/app/src/app/front/shorturl.go

+ 7 - 1
src/jfw/front/shorturl.go

@@ -12,6 +12,7 @@ import (
 	"net/http"
 	"net/url"
 	"strconv"
+	"sync"
 
 	"github.com/gogf/gf/v2/util/gconv"
 
@@ -94,9 +95,13 @@ var (
 		"advancedProject": false,
 	}
 	cacheIndex = []byte{}
+	cacheLock  = sync.Mutex{}
 )
 
 func (s *Short) ClearCache() {
+	defer util.Catch()
+	cacheLock.Lock()
+	defer cacheLock.Unlock()
 	log.Println("before cacheIndex:", string(cacheIndex))
 	if len(cacheIndex) > 0 {
 		cacheIndex = []byte{}
@@ -214,10 +219,11 @@ func (s *Short) LoginCommon(sess map[string]interface{}, stype, id string, bm bo
 		}
 		// 将页面内容返回给前端
 		s.ResponseWriter.Header().Set("Content-Type", "text/html; charset=utf-8")
-		//body, err := redis.GetBytes(redisLimitation, mobileHtmlKey)
+		cacheLock.Lock()
 		if len(cacheIndex) == 0 {
 			cacheIndex = loginPageInfo()
 		}
+		cacheLock.Unlock()
 		s.ResponseWriter.Write(cacheIndex)
 		return nil
 	}

+ 7 - 1
src/jfw/modules/app/src/app/front/shorturl.go

@@ -11,6 +11,7 @@ import (
 	"regexp"
 	"strconv"
 	"strings"
+	"sync"
 	"time"
 
 	"github.com/gogf/gf/v2/util/gconv"
@@ -75,9 +76,13 @@ var TypeCodeMap = map[string]string{
 var (
 	redisLimitation = "limitation"
 	cacheIndex      = []byte{}
+	cacheLock       = sync.Mutex{}
 )
 
 func (s *Short) ClearCache() {
+	defer util.Catch()
+	cacheLock.Lock()
+	defer cacheLock.Unlock()
 	log.Println("before cacheIndex:", string(cacheIndex))
 	if len(cacheIndex) > 0 {
 		cacheIndex = []byte{}
@@ -143,10 +148,11 @@ func (s *Short) Article(contentType, stype, id string) error {
 		}
 		// 将页面内容返回给前端
 		s.ResponseWriter.Header().Set("Content-Type", "text/html; charset=utf-8")
-		//body, err := redis.GetBytes(redisLimitation, mobileHtmlKey)
+		cacheLock.Lock()
 		if len(cacheIndex) == 0 {
 			cacheIndex = loginPageInfo()
 		}
+		cacheLock.Unlock()
 		s.ResponseWriter.Write(cacheIndex)
 		return nil
 	}