Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UC 查询缓存键包含 UC 地址 #110

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions storage/region_uc_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package storage

import (
"context"
"crypto/md5"
"encoding/json"
"fmt"
"github.com/qiniu/go-sdk/v7/internal/clientv2"
"golang.org/x/sync/singleflight"
"os"
"path/filepath"
"sync"
"time"

"github.com/qiniu/go-sdk/v7/internal/clientv2"
"golang.org/x/sync/singleflight"
)

// 此处废弃,但为了兼容老版本,单独放置一个文件
Expand Down Expand Up @@ -201,13 +203,13 @@ func getRegionByV2(ak, bucket string, options UCApiOptions) (*Region, error) {
}()
}

regionID := fmt.Sprintf("%s:%s", ak, bucket)
regionCacheKey := makeRegionCacheKey(ak, bucket)
//check from cache
if v, ok := regionV2Cache.Load(regionID); ok && time.Now().Before(v.(regionV2CacheValue).Deadline) {
if v, ok := regionV2Cache.Load(regionCacheKey); ok && time.Now().Before(v.(regionV2CacheValue).Deadline) {
return v.(regionV2CacheValue).Region, nil
}

newRegion, err, _ := ucQueryV2Group.Do(regionID, func() (interface{}, error) {
newRegion, err, _ := ucQueryV2Group.Do(regionCacheKey, func() (interface{}, error) {
reqURL := fmt.Sprintf("%s/v2/query?ak=%s&bucket=%s", getUcHost(options.UseHttps), ak, bucket)

var ret UcQueryRet
Expand Down Expand Up @@ -271,7 +273,7 @@ func getRegionByV2(ak, bucket string, options UCApiOptions) (*Region, error) {
IoSrcHost: ioSrcHost,
}

regionV2Cache.Store(regionID, regionV2CacheValue{
regionV2Cache.Store(regionCacheKey, regionV2CacheValue{
Region: region,
Deadline: time.Now().Add(time.Duration(ret.TTL) * time.Second),
})
Expand All @@ -288,3 +290,7 @@ func getRegionByV2(ak, bucket string, options UCApiOptions) (*Region, error) {

return newRegion.(*Region), err
}

func makeRegionCacheKey(ak, bucket string) string {
return fmt.Sprintf("%s:%s:%x", ak, bucket, md5.Sum([]byte(getUcHost(false))))
}
13 changes: 7 additions & 6 deletions storage/region_uc_v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/qiniu/go-sdk/v7/internal/clientv2"
"golang.org/x/sync/singleflight"
"math"
"os"
"path/filepath"
"sync"
"time"

"github.com/qiniu/go-sdk/v7/internal/clientv2"
"golang.org/x/sync/singleflight"
)

type ucQueryV4Ret struct {
Expand Down Expand Up @@ -137,14 +138,14 @@ func getRegionByV4(ak, bucket string, options UCApiOptions) (*RegionGroup, error
}()
}

regionID := fmt.Sprintf("%s:%s", ak, bucket)
regionCacheKey := makeRegionCacheKey(ak, bucket)
//check from cache
if v, ok := regionV4Cache.Load(regionID); ok && time.Now().Before(v.(regionV4CacheValue).Deadline) {
if v, ok := regionV4Cache.Load(regionCacheKey); ok && time.Now().Before(v.(regionV4CacheValue).Deadline) {
cacheValue, _ := v.(regionV4CacheValue)
return NewRegionGroup(cacheValue.getRegions()...), nil
}

newRegion, err, _ := ucQueryV4Group.Do(regionID, func() (interface{}, error) {
newRegion, err, _ := ucQueryV4Group.Do(regionCacheKey, func() (interface{}, error) {
reqURL := fmt.Sprintf("%s/v4/query?ak=%s&bucket=%s", getUcHost(options.UseHttps), ak, bucket)

var ret ucQueryV4Ret
Expand Down Expand Up @@ -181,7 +182,7 @@ func getRegionByV4(ak, bucket string, options UCApiOptions) (*RegionGroup, error
})
}

regionV4Cache.Store(regionID, regionV4CacheValue{
regionV4Cache.Store(regionCacheKey, regionV4CacheValue{
Regions: regions,
Deadline: time.Now().Add(time.Duration(ttl) * time.Second),
})
Expand Down
Loading