This commit is contained in:
2024-07-20 10:30:48 +08:00
parent a492f6d6eb
commit 1b73e8d15c
4 changed files with 26 additions and 0 deletions

View File

@@ -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)

1
go.mod
View File

@@ -31,6 +31,7 @@ require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/magiconair/properties v1.8.7 // indirect

2
go.sum
View File

@@ -178,6 +178,8 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaW
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0/go.mod h1:z0ButlSOZa5vEBq9m2m2hlwIgKw+rp3sdCBRoJY+30Y=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=

View File

@@ -7,6 +7,7 @@ import (
"fmt"
"log"
"regexp"
"strings"
"time"
openapi "github.com/alibabacloud-go/darabonba-openapi/client"
@@ -21,6 +22,7 @@ var (
)
func GetVideoM3U8(content string) (playinfo *vod20170321.GetPlayInfoResponseBody, err error) {
// 判断是否为视频地址
if len(imageRegexp.FindStringSubmatch(content)) > 0 {
key := urlRegexp.ReplaceAllString(content, "")
// 连接点播服务的OSS
@@ -32,13 +34,20 @@ func GetVideoM3U8(content string) (playinfo *vod20170321.GetPlayInfoResponseBody
if _err != nil {
return nil, _err
}
// 将 key 中的空格转码
key = strings.Replace(key, " ", "%20", -1)
// 通过标题查询视频ID
log.Println("查询视频", key)
response, _err := client.SearchMedia(&vod20170321.SearchMediaRequest{
Match: tea.String(fmt.Sprintf("Title='%s'", key)),
})
if _err != nil {
return nil, _err
}
log.Println("查询视频结果", response.Body)
if len(response.Body.MediaList) > 0 && *response.Body.MediaList[0].MediaType == "video" {
// 通过ID查询视频播放地址
VideoId := response.Body.MediaList[0].Video.VideoId