From 6915863d0afb602f3b223fa3e48322fc1f04bab4 Mon Sep 17 00:00:00 2001 From: hekki Date: Fri, 25 Oct 2024 16:06:01 +0900 Subject: [PATCH] =?UTF-8?q?coupon=20API=E3=82=92=E3=82=AD=E3=83=A3?= =?UTF-8?q?=E3=83=83=E3=82=B7=E3=83=A5=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platform/bill.go | 4 ++-- platform/coupon.go | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/platform/bill.go b/platform/bill.go index ae0d783..1f2fe12 100644 --- a/platform/bill.go +++ b/platform/bill.go @@ -90,7 +90,7 @@ func (c *billClient) Read(ctx context.Context) (*iaas.Bill, error) { } } - n, err := nextCacheExpiresAt() + n, err := c.nextCacheExpiresAt() if err != nil { return nil, err } @@ -107,7 +107,7 @@ func (c *billClient) Read(ctx context.Context) (*iaas.Bill, error) { // Billing APIは1日1回 AM4:30 (JST) にデータが更新される。 // このため、現在時刻がAM4:30 (JST) よりも早ければ当日のAM4:30 (JST)、 // 現在時刻がAM4:30 (JST) よりも遅ければ翌日のAM4:30 (JST) を有効期限として扱う。 -func nextCacheExpiresAt() (time.Time, error) { +func (c *billClient) nextCacheExpiresAt() (time.Time, error) { jst, err := time.LoadLocation("Asia/Tokyo") if err != nil { return time.Time{}, err diff --git a/platform/coupon.go b/platform/coupon.go index 5c6f094..62042b5 100644 --- a/platform/coupon.go +++ b/platform/coupon.go @@ -18,6 +18,7 @@ import ( "context" "errors" "sync" + "time" "github.com/sacloud/iaas-api-go" "github.com/sacloud/iaas-api-go/types" @@ -29,16 +30,25 @@ type CouponClient interface { } func getCouponClient(caller iaas.APICaller) CouponClient { - return &couponClient{caller: caller} + return &couponClient{ + caller: caller, + cache: *newCache(30 * time.Minute), + } } type couponClient struct { caller iaas.APICaller accountID types.ID once sync.Once + cache cache } func (c *couponClient) Find(ctx context.Context) ([]*iaas.Coupon, error) { + ca := c.cache.get() + if ca != nil { + return ca.([]*iaas.Coupon), nil + } + var err error c.once.Do(func() { var auth *iaas.AuthStatus @@ -63,5 +73,35 @@ func (c *couponClient) Find(ctx context.Context) ([]*iaas.Coupon, error) { return nil, err } + n, err := c.nextCacheExpiresAt() + if err != nil { + return nil, err + } + err = c.cache.set(searched.Coupons, n) + if err != nil { + return nil, err + } + return searched.Coupons, nil } + +// キャッシュの有効期限を算出する +// +// Billing APIは1日1回 AM4:30 (JST) にデータが更新される。 +// このため、現在時刻がAM4:30 (JST) よりも早ければ当日のAM4:30 (JST)、 +// 現在時刻がAM4:30 (JST) よりも遅ければ翌日のAM4:30 (JST) を有効期限として扱う。 +func (c *couponClient) nextCacheExpiresAt() (time.Time, error) { + jst, err := time.LoadLocation("Asia/Tokyo") + if err != nil { + return time.Time{}, err + } + + // 実行環境のタイムゾーンは不定のためJSTを基準にする + now := time.Now().In(jst) + expiresAt := time.Date(now.Year(), now.Month(), now.Day(), BillAPIUpdateHourJST, BillAPIUpdateMinuteJST, 0, 0, jst) + if now.Equal(expiresAt) || now.After(expiresAt) { + expiresAt = expiresAt.Add(24 * time.Hour) + } + + return expiresAt, nil +}