Skip to content

Commit d420c7d

Browse files
authored
Replace cache to use hashicorp/golang-lru/v2 (#1230)
1 parent 91ed6ac commit d420c7d

File tree

7 files changed

+14
-195
lines changed

7 files changed

+14
-195
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ require (
4848
github.com/google/uuid v1.6.0 // indirect
4949
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
5050
github.com/hashicorp/golang-lru v0.5.4 // indirect
51+
github.com/hashicorp/golang-lru/v2 v2.0.7
5152
github.com/hashicorp/hcl v1.0.0 // indirect
5253
github.com/inconshreveable/mousetrap v1.0.1 // indirect
5354
github.com/jonboulle/clockwork v0.4.0 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
189189
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
190190
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
191191
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
192+
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
193+
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
192194
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
193195
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
194196
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=

pkg/cache/cache.go

-115
This file was deleted.

pkg/cache/cache_test.go

-67
This file was deleted.

server/backend/backend.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ import (
2424
"fmt"
2525
"os"
2626

27+
"github.com/hashicorp/golang-lru/v2/expirable"
28+
2729
"github.com/yorkie-team/yorkie/api/types"
28-
"github.com/yorkie-team/yorkie/pkg/cache"
2930
pkgtypes "github.com/yorkie-team/yorkie/pkg/types"
3031
pkgwebhook "github.com/yorkie-team/yorkie/pkg/webhook"
3132
"github.com/yorkie-team/yorkie/server/backend/background"
@@ -48,10 +49,7 @@ type Backend struct {
4849
Config *Config
4950

5051
// AuthWebhookCache is used to cache the response of the auth webhook.
51-
AuthWebhookCache *cache.LRUExpireCache[string, pkgtypes.Pair[
52-
int,
53-
*types.AuthWebhookResponse,
54-
]]
52+
AuthWebhookCache *expirable.LRU[string, pkgtypes.Pair[int, *types.AuthWebhookResponse]]
5553
// AuthWebhookClient is used to send auth webhook.
5654
AuthWebhookClient *pkgwebhook.Client[types.AuthWebhookRequest, types.AuthWebhookResponse]
5755

@@ -99,8 +97,10 @@ func New(
9997
}
10098

10199
// 02. Create the webhook authWebhookCache and client.
102-
authWebhookCache := cache.NewLRUExpireCache[string, pkgtypes.Pair[int, *types.AuthWebhookResponse]](
100+
authWebhookCache := expirable.NewLRU[string, pkgtypes.Pair[int, *types.AuthWebhookResponse]](
103101
conf.AuthWebhookCacheSize,
102+
nil,
103+
conf.ParseAuthWebhookCacheTTL(),
104104
)
105105
authWebhookClient := pkgwebhook.NewClient[types.AuthWebhookRequest, types.AuthWebhookResponse](
106106
pkgwebhook.Options{

server/rpc/auth/webhook.go

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ func verifyAccess(
7777
be.AuthWebhookCache.Add(
7878
cacheKey,
7979
pkgtypes.Pair[int, *types.AuthWebhookResponse]{First: status, Second: res},
80-
be.Config.ParseAuthWebhookCacheTTL(),
8180
)
8281
}
8382

server/rpc/interceptors/yorkie.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@ import (
2525
"strings"
2626

2727
"connectrpc.com/connect"
28-
29-
"github.com/yorkie-team/yorkie/server/rpc/connecthelper"
28+
"github.com/hashicorp/golang-lru/v2/expirable"
3029

3130
"github.com/yorkie-team/yorkie/api/types"
32-
"github.com/yorkie-team/yorkie/pkg/cache"
3331
"github.com/yorkie-team/yorkie/server/backend"
3432
"github.com/yorkie-team/yorkie/server/logging"
3533
"github.com/yorkie-team/yorkie/server/projects"
34+
"github.com/yorkie-team/yorkie/server/rpc/connecthelper"
3635
"github.com/yorkie-team/yorkie/server/rpc/metadata"
3736
)
3837

@@ -45,12 +44,12 @@ func isYorkieService(method string) bool {
4544
type YorkieServiceInterceptor struct {
4645
backend *backend.Backend
4746
requestID *requestID
48-
projectCache *cache.LRUExpireCache[string, *types.Project]
47+
projectCache *expirable.LRU[string, *types.Project]
4948
}
5049

5150
// NewYorkieServiceInterceptor creates a new instance of YorkieServiceInterceptor.
5251
func NewYorkieServiceInterceptor(be *backend.Backend) *YorkieServiceInterceptor {
53-
cache := cache.NewLRUExpireCache[string, *types.Project](be.Config.ProjectCacheSize)
52+
cache := expirable.NewLRU[string, *types.Project](be.Config.ProjectCacheSize, nil, be.Config.ParseProjectCacheTTL())
5453
return &YorkieServiceInterceptor{
5554
backend: be,
5655
requestID: newRequestID("r"),
@@ -174,7 +173,7 @@ func (i *YorkieServiceInterceptor) buildContext(ctx context.Context, header http
174173
if err != nil {
175174
return nil, connecthelper.ToStatusError(err)
176175
}
177-
i.projectCache.Add(cacheKey, prj, i.backend.Config.ParseProjectCacheTTL())
176+
i.projectCache.Add(cacheKey, prj)
178177
}
179178
project, _ := i.projectCache.Get(cacheKey)
180179
ctx = projects.With(ctx, project)

0 commit comments

Comments
 (0)