Skip to content
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
6 changes: 3 additions & 3 deletions premium/byguild.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ func (p *PremiumLookupClient) GetTierByGuild(ctx context.Context, guild guild.Gu
_src = ""

defer func() {
// cache result
if _err == nil {
// a query cut short by ctx returns no rows and no error
if _err == nil && ctx.Err() == nil {
go func() {
err := p.SetCachedTier(ctx, guild.Id, CachedTier{
err := p.cacheTier(guild.Id, CachedTier{
Tier: int8(_tier),
Source: _src,
})
Expand Down
6 changes: 3 additions & 3 deletions premium/byuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func (p *PremiumLookupClient) GetTierByUserWithSource(ctx context.Context, userI
}

defer func() {
// cache result
if _err == nil {
go p.SetCachedTier(ctx, userId, CachedTier{
// a query cut short by ctx returns no rows and no error
if _err == nil && ctx.Err() == nil {
go p.cacheTier(userId, CachedTier{
Tier: int8(_tier),
Source: _src,
})
Expand Down
8 changes: 8 additions & 0 deletions premium/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type CachedTier struct {
}

const timeout = time.Minute * 5
const cacheWriteTimeout = 3 * time.Second

// Functions can take a user ID or guild ID

Expand Down Expand Up @@ -50,6 +51,13 @@ func (p *PremiumLookupClient) SetCachedTier(ctx context.Context, id uint64, data
})
}

func (p *PremiumLookupClient) cacheTier(id uint64, data CachedTier) error {
ctx, cancel := context.WithTimeout(context.Background(), cacheWriteTimeout)
defer cancel()

return p.SetCachedTier(ctx, id, data)
}

func (p *PremiumLookupClient) DeleteCachedTier(ctx context.Context, id uint64) error {
return sentry.WithSpan1(ctx, "DeleteCachedTier", func(span *sentry.Span) error {
key := fmt.Sprintf("premium:%d", id)
Expand Down
Loading