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/graphql-go/handler"
 | 
				
			||||||
	"github.com/milvus-io/milvus-sdk-go/v2/entity"
 | 
						"github.com/milvus-io/milvus-sdk-go/v2/entity"
 | 
				
			||||||
	"github.com/spf13/viper"
 | 
						"github.com/spf13/viper"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						lru "github.com/hashicorp/golang-lru/v2"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// string 转换为 int, 如果转换失败则返回默认值
 | 
					// string 转换为 int, 如果转换失败则返回默认值
 | 
				
			||||||
@@ -130,9 +132,16 @@ func GetNetWorkEmbedding(id int) (embedding []float32) {
 | 
				
			|||||||
	return result.Feature
 | 
						return result.Feature
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var lruCache, _ = lru.New[int, []int64](1000)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (image *Image) GetSimilarImagesIdList(collection_name string) (ids []int64) {
 | 
					func (image *Image) GetSimilarImagesIdList(collection_name string) (ids []int64) {
 | 
				
			||||||
	ctx := context.Background()
 | 
						ctx := context.Background()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 先从 LRU 中查询缓存的结果, 如果缓存中有, 直接返回
 | 
				
			||||||
 | 
						if value, ok := lruCache.Get(image.Id); ok {
 | 
				
			||||||
 | 
							return value
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 先从milvus中查询图片的向量
 | 
						// 先从milvus中查询图片的向量
 | 
				
			||||||
	var embedding []float32
 | 
						var embedding []float32
 | 
				
			||||||
	result, err := milvusConnection.Client.Query(ctx, collection_name, nil, fmt.Sprintf("id in [%d]", image.Id), []string{"embedding"})
 | 
						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()
 | 
							ids = item.IDs.FieldData().GetScalars().GetLongData().GetData()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 将结果缓存到 LRU 中
 | 
				
			||||||
 | 
						lruCache.Add(image.Id, ids)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return ids
 | 
						return ids
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -658,7 +670,9 @@ func main() {
 | 
				
			|||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							fmt.Println("safeParam", safeParam)
 | 
				
			||||||
		urls, err := models.GetVideoM3U8(safeParam)
 | 
							urls, err := models.GetVideoM3U8(safeParam)
 | 
				
			||||||
 | 
							fmt.Println("urls", urls, err)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Println("获取视频链接失败", err)
 | 
								log.Println("获取视频链接失败", err)
 | 
				
			||||||
			w.WriteHeader(http.StatusBadRequest)
 | 
								w.WriteHeader(http.StatusBadRequest)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										1
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.mod
									
									
									
									
									
								
							@@ -31,6 +31,7 @@ require (
 | 
				
			|||||||
	github.com/fsnotify/fsnotify v1.6.0 // indirect
 | 
						github.com/fsnotify/fsnotify v1.6.0 // indirect
 | 
				
			||||||
	github.com/golang/protobuf v1.5.2 // indirect
 | 
						github.com/golang/protobuf v1.5.2 // indirect
 | 
				
			||||||
	github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // 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/hashicorp/hcl v1.0.0 // indirect
 | 
				
			||||||
	github.com/json-iterator/go v1.1.12 // indirect
 | 
						github.com/json-iterator/go v1.1.12 // indirect
 | 
				
			||||||
	github.com/magiconair/properties v1.8.7 // indirect
 | 
						github.com/magiconair/properties v1.8.7 // indirect
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										2
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.sum
									
									
									
									
									
								
							@@ -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/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.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 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 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
 | 
				
			||||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
 | 
					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=
 | 
					github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,6 +7,7 @@ import (
 | 
				
			|||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	openapi "github.com/alibabacloud-go/darabonba-openapi/client"
 | 
						openapi "github.com/alibabacloud-go/darabonba-openapi/client"
 | 
				
			||||||
@@ -21,6 +22,7 @@ var (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func GetVideoM3U8(content string) (playinfo *vod20170321.GetPlayInfoResponseBody, err error) {
 | 
					func GetVideoM3U8(content string) (playinfo *vod20170321.GetPlayInfoResponseBody, err error) {
 | 
				
			||||||
 | 
						// 判断是否为视频地址
 | 
				
			||||||
	if len(imageRegexp.FindStringSubmatch(content)) > 0 {
 | 
						if len(imageRegexp.FindStringSubmatch(content)) > 0 {
 | 
				
			||||||
		key := urlRegexp.ReplaceAllString(content, "")
 | 
							key := urlRegexp.ReplaceAllString(content, "")
 | 
				
			||||||
		// 连接点播服务的OSS
 | 
							// 连接点播服务的OSS
 | 
				
			||||||
@@ -32,13 +34,20 @@ func GetVideoM3U8(content string) (playinfo *vod20170321.GetPlayInfoResponseBody
 | 
				
			|||||||
		if _err != nil {
 | 
							if _err != nil {
 | 
				
			||||||
			return nil, _err
 | 
								return nil, _err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// 将 key 中的空格转码
 | 
				
			||||||
 | 
							key = strings.Replace(key, " ", "%20", -1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 通过标题查询视频ID
 | 
							// 通过标题查询视频ID
 | 
				
			||||||
 | 
							log.Println("查询视频", key)
 | 
				
			||||||
		response, _err := client.SearchMedia(&vod20170321.SearchMediaRequest{
 | 
							response, _err := client.SearchMedia(&vod20170321.SearchMediaRequest{
 | 
				
			||||||
			Match: tea.String(fmt.Sprintf("Title='%s'", key)),
 | 
								Match: tea.String(fmt.Sprintf("Title='%s'", key)),
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
		if _err != nil {
 | 
							if _err != nil {
 | 
				
			||||||
			return nil, _err
 | 
								return nil, _err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							log.Println("查询视频结果", response.Body)
 | 
				
			||||||
		if len(response.Body.MediaList) > 0 && *response.Body.MediaList[0].MediaType == "video" {
 | 
							if len(response.Body.MediaList) > 0 && *response.Body.MediaList[0].MediaType == "video" {
 | 
				
			||||||
			// 通过ID查询视频播放地址
 | 
								// 通过ID查询视频播放地址
 | 
				
			||||||
			VideoId := response.Body.MediaList[0].Video.VideoId
 | 
								VideoId := response.Body.MediaList[0].Video.VideoId
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user