Преглед изворни кода

全量redis改为href的hash值

maxiaoshan пре 3 година
родитељ
комит
d510b0a8f0
3 измењених фајлова са 28 додато и 14 уклоњено
  1. 3 2
      src/spider/msgservice.go
  2. 22 10
      src/spider/spider.go
  3. 3 2
      src/spider/store.go

+ 3 - 2
src/spider/msgservice.go

@@ -280,6 +280,7 @@ func SaveObj(event int, checkAtrr string, data map[string]interface{}, saveredis
 		href := fmt.Sprint(data["href"])
 		if len(href) > 5 && saveredis { //有效数据
 			db := HexToBigIntMod(href) //根据href的哈希值选择Redis的db
+			hashHref := HexText(href)
 			//增量
 			isExist, _ := util.ExistRedis("title_repeat_judgement", 0, "url_repeat_"+href)
 			id := ""
@@ -294,9 +295,9 @@ func SaveObj(event int, checkAtrr string, data map[string]interface{}, saveredis
 				util.PutRedis("title_repeat_judgement", 0, "url_repeat_"+href, href, 3600*24*30)
 				if !flag { //保存服务发送成功
 					//全量(判断是否已存在防止覆盖id)
-					isExist, _ := util.ExistRedis("title_repeat_fulljudgement", db, "url_repeat_"+href)
+					isExist, _ := util.ExistRedis("title_repeat_fulljudgement", db, hashHref)
 					if !isExist {
-						util.PutRedis("title_repeat_fulljudgement", db, "url_repeat_"+href, "lua_"+id, -1)
+						util.PutRedis("title_repeat_fulljudgement", db, hashHref, "lua_"+id, -1)
 					}
 				}
 			}

+ 22 - 10
src/spider/spider.go

@@ -288,12 +288,13 @@ func (s *Spider) DownListPageItem() (errs interface{}) {
 							href := fmt.Sprint(tmp["href"])
 							if len(href) > 5 { //有效数据
 								db := HexToBigIntMod(href) //根据href的哈希值选择Redis的db
+								hashHref := HexText(href)
 								//增量(redis默认db0)
 								util.PutRedis("title_repeat_judgement", 0, "url_repeat_"+href, href, 3600*24*30)
 								//全量(判断是否已存在防止覆盖id)
-								isExist, _ := util.ExistRedis("title_repeat_fulljudgement", db, "url_repeat_"+href)
+								isExist, _ := util.ExistRedis("title_repeat_fulljudgement", db, hashHref)
 								if !isExist {
-									util.PutRedis("title_repeat_fulljudgement", db, "url_repeat_"+href, "", -1)
+									util.PutRedis("title_repeat_fulljudgement", db, hashHref, "", -1)
 								}
 								list = append(list, tmp)
 							}
@@ -408,8 +409,9 @@ func (s *Spider) HistoricalMendDownloadDetailItem(p interface{}) {
 		return
 	}
 	db := HexToBigIntMod(href)
-	SaveListPageData(paramdata)                                                         //存储采集记录
-	isExist, _ := util.ExistRedis("title_repeat_fulljudgement", db, "url_repeat_"+href) //取全量redis
+	hashHref := HexText(href)
+	SaveListPageData(paramdata)                                               //存储采集记录
+	isExist, _ := util.ExistRedis("title_repeat_fulljudgement", db, hashHref) //取全量redis
 	//log.Println("full href:", href, " isExist:", isExist)
 	logger.Debug("full href:", href, " isExist:", isExist)
 	if !s.IsMustDownload && isExist { //非强制下载redis中存在,结束
@@ -510,9 +512,10 @@ func (s *Spider) DownloadDetailItem(p interface{}, num *int) {
 		util.PutRedis("title_repeat_judgement", 0, "url_repeat_"+href, href, 3600*24*30)
 		//全量
 		db := HexToBigIntMod(href)
-		isExist, _ := util.ExistRedis("title_repeat_fulljudgement", db, "url_repeat_"+href)
+		hashHref := HexText(href)
+		isExist, _ := util.ExistRedis("title_repeat_fulljudgement", db, hashHref)
 		if !isExist {
-			util.PutRedis("title_repeat_fulljudgement", db, "url_repeat_"+href, "", -1)
+			util.PutRedis("title_repeat_fulljudgement", db, hashHref, "", -1)
 		}
 	}
 	t1 := util.ParseDate2Int64(qu.ObjToString(data["publishtime"]))
@@ -720,9 +723,10 @@ func (s *Spider) DownloadHighDetail() {
 						util.PutRedis("title_repeat_judgement", 0, "url_repeat_"+href, href, 3600*24*30)
 						//全量
 						db := HexToBigIntMod(href)
-						isExist, _ := util.ExistRedis("title_repeat_fulljudgement", db, "url_repeat_"+href)
+						hashHref := HexText(href)
+						isExist, _ := util.ExistRedis("title_repeat_fulljudgement", db, hashHref)
 						if !isExist {
-							util.PutRedis("title_repeat_fulljudgement", db, "url_repeat_"+href, "", -1)
+							util.PutRedis("title_repeat_fulljudgement", db, hashHref, "", -1)
 						}
 					}
 					if !success { //下载失败更新次数和状态
@@ -841,9 +845,10 @@ func (s *Spider) DownloadListDetail() {
 				util.PutRedis("title_repeat_judgement", 0, "url_repeat_"+href, href, 3600*24*30)
 				//全量
 				db := HexToBigIntMod(href)
-				isExist, _ := util.ExistRedis("title_repeat_fulljudgement", db, "url_repeat_"+href)
+				hashHref := HexText(href)
+				isExist, _ := util.ExistRedis("title_repeat_fulljudgement", db, hashHref)
 				if !isExist {
-					util.PutRedis("title_repeat_fulljudgement", db, "url_repeat_"+href, "", -1)
+					util.PutRedis("title_repeat_fulljudgement", db, hashHref, "", -1)
 				}
 			}
 			if !success { //下载失败更新次数和状态
@@ -911,6 +916,13 @@ func HexToBigIntMod(href string) int {
 	return int(n.Mod(n, big.NewInt(16)).Int64())
 }
 
+//求hash
+func HexText(href string) string {
+	h := sha256.New()
+	h.Write([]byte(href))
+	return fmt.Sprintf("%x", h.Sum(nil))
+}
+
 //func RedisIsExist(href string) bool {
 //	isExist := false
 //	if len(href) > 75 { //取href的哈希判断是否存在

+ 3 - 2
src/spider/store.go

@@ -82,12 +82,13 @@ func Store(mode, event int, c, coverAttr string, data map[string]interface{}, fl
 		href := fmt.Sprint(data["href"])
 		if len(href) > 5 && flag { //有效数据
 			db := HexToBigIntMod(href) //根据href的哈希值选择Redis的db
+			hashHref := HexText(href)
 			//增量
 			lu.PutRedis("title_repeat_judgement", 0, "url_repeat_"+href, href, 3600*24*30)
 			//全量
-			isExist, _ := lu.ExistRedis("title_repeat_fulljudgement", db, "url_repeat_"+href)
+			isExist, _ := lu.ExistRedis("title_repeat_fulljudgement", db, hashHref)
 			if !isExist {
-				lu.PutRedis("title_repeat_fulljudgement", db, "url_repeat_"+href, "", -1)
+				lu.PutRedis("title_repeat_fulljudgement", db, hashHref, "", -1)
 			}
 		}
 	} else if mode == 2 {