lru
This commit is contained in:
14
bin/main.go
14
bin/main.go
@@ -21,6 +21,8 @@ import (
|
||||
"github.com/graphql-go/handler"
|
||||
"github.com/milvus-io/milvus-sdk-go/v2/entity"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
lru "github.com/hashicorp/golang-lru/v2"
|
||||
)
|
||||
|
||||
// string 转换为 int, 如果转换失败则返回默认值
|
||||
@@ -130,9 +132,16 @@ func GetNetWorkEmbedding(id int) (embedding []float32) {
|
||||
return result.Feature
|
||||
}
|
||||
|
||||
var lruCache, _ = lru.New[int, []int64](1000)
|
||||
|
||||
func (image *Image) GetSimilarImagesIdList(collection_name string) (ids []int64) {
|
||||
ctx := context.Background()
|
||||
|
||||
// 先从 LRU 中查询缓存的结果, 如果缓存中有, 直接返回
|
||||
if value, ok := lruCache.Get(image.Id); ok {
|
||||
return value
|
||||
}
|
||||
|
||||
// 先从milvus中查询图片的向量
|
||||
var embedding []float32
|
||||
result, err := milvusConnection.Client.Query(ctx, collection_name, nil, fmt.Sprintf("id in [%d]", image.Id), []string{"embedding"})
|
||||
@@ -169,6 +178,9 @@ func (image *Image) GetSimilarImagesIdList(collection_name string) (ids []int64)
|
||||
ids = item.IDs.FieldData().GetScalars().GetLongData().GetData()
|
||||
}
|
||||
|
||||
// 将结果缓存到 LRU 中
|
||||
lruCache.Add(image.Id, ids)
|
||||
|
||||
return ids
|
||||
}
|
||||
|
||||
@@ -658,7 +670,9 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("safeParam", safeParam)
|
||||
urls, err := models.GetVideoM3U8(safeParam)
|
||||
fmt.Println("urls", urls, err)
|
||||
if err != nil {
|
||||
log.Println("获取视频链接失败", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
|
Reference in New Issue
Block a user