Skip to content

Commit 45393ef

Browse files
authored
Generalize cache + add refresh interval to template cache (#1538)
1 parent a3c2803 commit 45393ef

File tree

7 files changed

+816
-234
lines changed

7 files changed

+816
-234
lines changed

packages/api/internal/cache/auth/cache.go

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,46 @@ package autchcache
22

33
import (
44
"context"
5-
"fmt"
65
"time"
76

8-
"github.com/jellydator/ttlcache/v3"
9-
"golang.org/x/sync/singleflight"
10-
117
"github.com/e2b-dev/infra/packages/api/internal/db/types"
8+
"github.com/e2b-dev/infra/packages/shared/pkg/cache"
129
)
1310

1411
const (
1512
authInfoExpiration = 5 * time.Minute
1613
refreshInterval = 1 * time.Minute
14+
refreshTimeout = 30 * time.Second
1715
)
1816

19-
type TeamInfo struct {
20-
team *types.Team
21-
22-
lastRefresh time.Time
23-
once singleflight.Group
24-
}
25-
2617
type DataCallback = func(ctx context.Context, key string) (*types.Team, error)
2718

2819
type TeamAuthCache struct {
29-
cache *ttlcache.Cache[string, *TeamInfo]
20+
cache *cache.Cache[string, *types.Team]
3021
}
3122

3223
func NewTeamAuthCache() *TeamAuthCache {
33-
cache := ttlcache.New(ttlcache.WithTTL[string, *TeamInfo](authInfoExpiration))
34-
go cache.Start()
24+
config := cache.Config[string, *types.Team]{
25+
TTL: authInfoExpiration,
26+
RefreshInterval: refreshInterval,
27+
RefreshTimeout: refreshTimeout,
28+
}
3529

3630
return &TeamAuthCache{
37-
cache: cache,
31+
cache: cache.NewCache[string, *types.Team](config),
3832
}
3933
}
4034

4135
// TODO: save blocked teams to cache as well, handle the condition in the GetOrSet method
4236
func (c *TeamAuthCache) GetOrSet(ctx context.Context, key string, dataCallback DataCallback) (team *types.Team, err error) {
43-
var item *ttlcache.Item[string, *TeamInfo]
44-
var templateInfo *TeamInfo
45-
46-
item = c.cache.Get(key)
47-
if item == nil {
48-
team, err = dataCallback(ctx, key)
49-
if err != nil {
50-
return nil, fmt.Errorf("error while getting the team: %w", err)
51-
}
52-
53-
templateInfo = &TeamInfo{team: team, lastRefresh: time.Now()}
54-
c.cache.Set(key, templateInfo, authInfoExpiration)
55-
56-
return team, nil
57-
}
58-
59-
templateInfo = item.Value()
60-
if time.Since(templateInfo.lastRefresh) > refreshInterval {
61-
go templateInfo.once.Do(key, func() (any, error) { //nolint:contextcheck // TODO: fix this later
62-
c.Refresh(key, dataCallback)
63-
64-
return nil, err
65-
})
37+
team, err = c.cache.GetOrSet(ctx, key, dataCallback)
38+
if err != nil {
39+
return nil, err
6640
}
6741

68-
return templateInfo.team, nil
42+
return team, nil
6943
}
7044

71-
// Refresh refreshes the cache for the given team ID.
72-
func (c *TeamAuthCache) Refresh(key string, dataCallback DataCallback) {
73-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
74-
defer cancel()
75-
76-
team, err := dataCallback(ctx, key)
77-
if err != nil {
78-
c.cache.Delete(key)
79-
80-
return
81-
}
82-
83-
c.cache.Set(key, &TeamInfo{team: team, lastRefresh: time.Now()}, authInfoExpiration)
45+
func (c *TeamAuthCache) Close(ctx context.Context) error {
46+
return c.cache.Close(ctx)
8447
}

0 commit comments

Comments
 (0)