From 07eea46970759aba106f3db3f4bc24c518ab41de Mon Sep 17 00:00:00 2001
From: liujiandao <274878379@qq.com>
Date: 星期二, 20 二月 2024 14:39:04 +0800
Subject: [PATCH] 库中新增更新单条缓存

---
 cache/db.go      |   18 +++++++++
 cache/compare.go |   62 +++++++------------------------
 main.go          |    2 
 3 files changed, 33 insertions(+), 49 deletions(-)

diff --git a/cache/compare.go b/cache/compare.go
index ffaa428..d2e5b23 100644
--- a/cache/compare.go
+++ b/cache/compare.go
@@ -266,56 +266,22 @@
 	}
 }
 
-// UpdateDbPersonsCache 鏇存柊缂撳瓨涓殑鍏ㄩ儴浜哄憳淇℃伅
-func UpdateDbPersonsCache() {
+// UpdateDbPersonsCacheById 鏇存柊缂撳瓨涓殑鍏ㄩ儴浜哄憳淇℃伅
+func UpdateDbPersonsCacheById(id string) {
 	var dbpApi DbPersons
-	total, e := dbpApi.GetPersonTotal("")
-	logger.Debugf("鎵�鏈夊簳搴撳叡鏈�%d鏉¤褰�", total)
-	if e == nil && total > 0 {
-		queryEachNum := *querynum
-		qn := int(total) / *threadnum
-		if *querynum < qn {
-			queryEachNum = qn
+	info, err := dbpApi.GetPersonsCompareCacheById(id)
+	if err != nil {
+		logger.Debug(err)
+		return
+	}
+	if info.Tableid != "" {
+		Cmap.Lock()
+		defer Cmap.Unlock()
+		if _, ok := Cmap.Cam[info.Tableid]; !ok {
+			Cmap.Cam[info.Tableid] = shardmap.New(uint8(*threadnum))
 		}
-		queryT := int(total) / queryEachNum
-		if int(total)%queryEachNum > 0 {
-			queryT++
-		}
-		temptime := time.Now()
-		var wg sync.WaitGroup
-
-		for i := 0; i < queryT; i++ {
-			j := i * queryEachNum
-			wg.Add(1)
-			go func(qs int) {
-				defer wg.Done()
-				dbpersons, err := dbpApi.GetPersonsCompareCacheBase(j, queryEachNum)
-				if err != nil {
-					logger.Debug(err)
-					return
-				}
-				logger.Debugf("鑾峰彇%d鏉′汉鍛樹俊鎭�", len(dbpersons))
-				Cmap.Lock()
-				tableId := ""
-				for _, value := range dbpersons {
-					tableId = value.Tableid
-					if _, ok := Cmap.Cam[value.Tableid]; !ok {
-						Cmap.Cam[value.Tableid] = shardmap.New(uint8(*threadnum))
-					}
-
-					Cmap.Cam[value.Tableid].Set(value.Id, value)
-				}
-
-				if len(dbpersons) != 0 {
-					Cmap.Cam[tableId].Settime()
-				}
-
-				Cmap.Unlock()
-
-			}(j)
-		}
-		wg.Wait()
-		logger.Debug("搴曞簱浜哄憳缂撳瓨瀹屾垚鐢ㄦ椂:", time.Since(temptime))
+		Cmap.Cam[info.Tableid].Set(info.Id, info)
+		Cmap.Cam[info.Tableid].Settime()
 	}
 }
 
diff --git a/cache/db.go b/cache/db.go
index f283c4b..c07cd80 100644
--- a/cache/db.go
+++ b/cache/db.go
@@ -126,3 +126,21 @@
 	}
 	return
 }
+
+func (dbp *DbPersons) GetPersonsCompareCacheById(id string) (info *protomsg.Esinfo, err error) {
+	sql := "select id,faceFeature,tableId,enable from dbtablepersons where id = \"" + id + "\""
+	var p DbPersons
+	err = db.Raw(sql).First(&p).Error
+	if err != nil {
+		return nil, err
+	}
+	if p.FaceFeature != "" {
+		info = &protomsg.Esinfo{
+			Id:          p.Id,
+			Tableid:     p.TableId,
+			FaceFeature: p.FaceFeature,
+			Enable:      int32(p.Enable),
+		}
+	}
+	return
+}
diff --git a/main.go b/main.go
index 0e2691d..f8800b5 100644
--- a/main.go
+++ b/main.go
@@ -131,7 +131,7 @@
 					if compareEvent.EventType == protomsg.CompareEventType_ReInitCache { //鍔犲叆闆嗙兢鍚庨噸鏂板垵濮嬪寲缂撳瓨
 						cache.ReInitDbTablePersonsCache()
 					} else if compareEvent.EventType == protomsg.CompareEventType_UpdateCache { //搴撲腑鏂板鏇存柊缂撳瓨
-						cache.UpdateDbPersonsCache()
+						cache.UpdateDbPersonsCacheById(string(compareEvent.Payload))
 					}
 				} else {
 					logger.Debug("json unmarshal error")

--
Gitblit v1.8.0