// Package asArtist ----------------------------- // @file : resumeCache.go // @author : JJXu // @contact : wavingbear@163.com // @time : 2024/9/2 下午4:13 // ------------------------------------------- package asArtist import ( "fmt" "github.com/dorlolo/simpleRequest" "github.com/fonchain_enterprise/fonchain-main/pkg/cache" "math/rand" "strings" "sync" "time" ) var resumeCacheLocker = sync.RWMutex{} var resumeCacheKey = "artistinfo:resume:" type ResumeCache struct { } func (r ResumeCache) QueryOrDownloadLongTextFromBucket(data *UserView) error { if strings.HasPrefix(data.ArtistProfile, "http") { //增加缓存 resumeCacheLocker.RLock() var artistProfile, err = cache.RedisClient.Get(resumeCacheKey + data.ArtistProfile).Bytes() resumeCacheLocker.RUnlock() if artistProfile != nil && err == nil { data.ArtistProfile = string(artistProfile) fmt.Println("查到了resume的缓存") return nil } fmt.Println("下载resume数据") //如果为空则下载data.ArtistProfile req := simpleRequest.NewRequest() res, err := req.GET(data.ArtistProfile) if err != nil { return err } r.setCache(data.ArtistProfile, string(res)) data.ArtistProfile = string(res) } return nil } func (ResumeCache) setCache(key string, value string) { resumeCacheLocker.Lock() cache.RedisClient.Set(resumeCacheKey+key, value, time.Second*time.Duration(rand.Intn(300)+60)) resumeCacheLocker.Unlock() } func downloadLongTextFromBucket(data *UserView) error { if strings.HasPrefix(data.ArtistProfile, "http") { //增加缓存 //下载data.ArtistProfile req := simpleRequest.NewRequest() res, err := req.GET(data.ArtistProfile) if err != nil { return err } data.ArtistProfile = string(res) } return nil }