-
Notifications
You must be signed in to change notification settings - Fork 14
/
workspace_redis.go
53 lines (41 loc) · 1.25 KB
/
workspace_redis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package repository
import (
"context"
"time"
"github.com/beam-cloud/beta9/pkg/common"
"github.com/beam-cloud/beta9/pkg/types"
)
type WorkspaceRedisRepository struct {
rdb *common.RedisClient
}
func NewWorkspaceRedisRepository(r *common.RedisClient) WorkspaceRepository {
return &WorkspaceRedisRepository{rdb: r}
}
func (wr *WorkspaceRedisRepository) GetConcurrencyLimitByWorkspaceId(workspaceId string) (*types.ConcurrencyLimit, error) {
key := common.RedisKeys.WorkspaceConcurrencyLimit(workspaceId)
res, err := wr.rdb.HGetAll(context.Background(), key).Result()
if err != nil {
return nil, err
}
if len(res) == 0 {
return nil, nil
}
limit := &types.ConcurrencyLimit{}
if err = common.ToStruct(res, limit); err != nil {
return nil, err
}
return limit, nil
}
var cachedConcurrencyLimitTtl = 600
func (wr *WorkspaceRedisRepository) SetConcurrencyLimitByWorkspaceId(workspaceId string, limit *types.ConcurrencyLimit) error {
key := common.RedisKeys.WorkspaceConcurrencyLimit(workspaceId)
err := wr.rdb.HSet(context.Background(), key, common.ToSlice(limit)).Err()
if err != nil {
return err
}
err = wr.rdb.Expire(context.Background(), key, time.Duration(cachedConcurrencyLimitTtl)*time.Second).Err()
if err != nil {
return err
}
return nil
}