unknown 7 năm trước cách đây
mục cha
commit
2747fca8e2
1 tập tin đã thay đổi với 29 bổ sung3 xóa
  1. 29 3
      src/apiservice/oamanager/content.go

+ 29 - 3
src/apiservice/oamanager/content.go

@@ -34,14 +34,18 @@ var arrapi = make([]map[string]interface{}, 0)
 
 func init() {
 	xweb.AddAction(&Content{})
+	go func() {
+		time.Sleep(1 * time.Minute)
+		SaveLogTask()
+	}()
 }
 func (c *Content) Article(id string) {
 	defer util.Catch()
 	client := c.Header("User-Agent")
 	c.Request.Proto = GetIp(c.Request) //ip
 	var data map[string]interface{}
-	addLog(c.Request)
-	bm := false //访问日志
+	go addLog(c.Request)
+	bm := false //保存用户访问三级页日志
 	bm = regex.MatchString(client)
 	sid_openid := util.DecodeArticleId2ByCheck(id)[0] //解密id
 	res := redis.Get(REDISDB, "jyoadetail_"+sid_openid)
@@ -65,6 +69,28 @@ func (c *Content) Article(id string) {
 	}
 }
 
+//定时保存日志
+func SaveLogTask() {
+	lock.Lock()
+	if len(arr) >= 1 {
+		tmp := arr
+		arr = make([]map[string]interface{}, 0)
+		go func() {
+			log.Println("timer..save..article..log", len(tmp))
+			mongodb.SaveBulk("log", tmp...)
+		}()
+	}
+	if len(arrapi) >= 1 {
+		tmp1 := arrapi
+		arrapi = make([]map[string]interface{}, 0)
+		go func() {
+			log.Println("timer..save..api..log", len(tmp1))
+			mongodb.SaveBulk("log", tmp1...)
+		}()
+	}
+	lock.Unlock()
+	time.AfterFunc(5*time.Minute, SaveLogTask)
+}
 func addLog(req *http.Request) {
 	timeNow := time.Now()
 	agent := req.Header.Get("user-agent")
@@ -121,7 +147,7 @@ func APIlog(username, action, keyword, appid, rMsg, signature string, pagenum, t
 		tmp := arrapi
 		arrapi = make([]map[string]interface{}, 0)
 		go func() {
-			log.Println("save..article..log", len(tmp))
+			log.Println("save..api..log", len(tmp))
 			mongodb.SaveBulk("apilog", tmp...)
 		}()
 	}