Skip to content

Commit a7eba59

Browse files
authored
refactor: export cache package (#1017)
1 parent ac8703e commit a7eba59

5 files changed

Lines changed: 26 additions & 23 deletions

File tree

‎internal/service/auth_service.go‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/tinyauthapp/tinyauth/internal/repository"
1616
"github.com/tinyauthapp/tinyauth/internal/utils"
1717
"github.com/tinyauthapp/tinyauth/internal/utils/logger"
18+
"github.com/tinyauthapp/tinyauth/pkg/cache"
1819
"go.uber.org/dig"
1920

2021
"github.com/google/uuid"
@@ -71,9 +72,9 @@ type AuthService struct {
7172
dummyHash string
7273

7374
caches struct {
74-
login *CacheStore[LoginAttempt]
75-
oauth *CacheStore[OAuthPendingSession]
76-
ldap *CacheStore[[]string]
75+
login *cache.CacheStore[LoginAttempt]
76+
oauth *cache.CacheStore[OAuthPendingSession]
77+
ldap *cache.CacheStore[[]string]
7778
}
7879
}
7980

@@ -115,9 +116,9 @@ func NewAuthService(i AuthServiceInput) (*AuthService, error) {
115116
service.dummyHash = string(dummyHash)
116117

117118
// caches setup
118-
oauthCache := NewCacheStore[OAuthPendingSession](256)
119-
loginCache := NewCacheStore[LoginAttempt](service.calculateLockdownLimit())
120-
ldapCache := NewCacheStore[[]string](1024)
119+
oauthCache := cache.NewCacheStore[OAuthPendingSession](256)
120+
loginCache := cache.NewCacheStore[LoginAttempt](service.calculateLockdownLimit())
121+
ldapCache := cache.NewCacheStore[[]string](1024)
121122

122123
service.caches.oauth = oauthCache
123124
service.caches.login = loginCache
@@ -279,7 +280,7 @@ func (auth *AuthService) RecordLoginAttempt(identifier string, success bool) {
279280
return
280281
}
281282

282-
auth.caches.login.WithLock(func(actions CacheStoreActions[LoginAttempt]) {
283+
auth.caches.login.WithLock(func(actions cache.CacheStoreActions[LoginAttempt]) {
283284
entry, ok := actions.Get(identifier)
284285

285286
if !ok {

‎internal/service/oidc_service.go‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/tinyauthapp/tinyauth/internal/repository"
2828
"github.com/tinyauthapp/tinyauth/internal/utils"
2929
"github.com/tinyauthapp/tinyauth/internal/utils/logger"
30+
"github.com/tinyauthapp/tinyauth/pkg/cache"
3031
"go.uber.org/dig"
3132
)
3233

@@ -158,9 +159,9 @@ type OIDCService struct {
158159
issuer string
159160

160161
caches struct {
161-
code *CacheStore[AuthorizeCodeEntry]
162-
usedCode *CacheStore[UsedCodeEntry]
163-
authorize *CacheStore[AuthorizeRequest]
162+
code *cache.CacheStore[AuthorizeCodeEntry]
163+
usedCode *cache.CacheStore[UsedCodeEntry]
164+
authorize *cache.CacheStore[AuthorizeRequest]
164165
}
165166
}
166167

@@ -339,11 +340,11 @@ func NewOIDCService(i OIDCServiceInput) (*OIDCService, error) {
339340
i.Ding.Go(service.cleanupRoutine, ding.RingMinor)
340341

341342
// Create caches
342-
codeCash := NewCacheStore[AuthorizeCodeEntry](256)
343-
usedCode := NewCacheStore[UsedCodeEntry](256)
344-
authorize := NewCacheStore[AuthorizeRequest](256)
343+
codeCache := cache.NewCacheStore[AuthorizeCodeEntry](256)
344+
usedCode := cache.NewCacheStore[UsedCodeEntry](256)
345+
authorize := cache.NewCacheStore[AuthorizeRequest](256)
345346

346-
service.caches.code = codeCash
347+
service.caches.code = codeCache
347348
service.caches.usedCode = usedCode
348349
service.caches.authorize = authorize
349350

@@ -503,7 +504,7 @@ func (service *OIDCService) GetCodeEntry(codeHash string, clientId string) (*Aut
503504
var entry AuthorizeCodeEntry
504505
var ok bool
505506

506-
service.caches.code.WithLock(func(actions CacheStoreActions[AuthorizeCodeEntry]) {
507+
service.caches.code.WithLock(func(actions cache.CacheStoreActions[AuthorizeCodeEntry]) {
507508
entry, ok = actions.Get(codeHash)
508509

509510
if !ok {

‎internal/service/tailscale_service.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/tinyauthapp/tinyauth/internal/model"
1111
"github.com/tinyauthapp/tinyauth/internal/utils"
1212
"github.com/tinyauthapp/tinyauth/internal/utils/logger"
13+
"github.com/tinyauthapp/tinyauth/pkg/cache"
1314
"go.uber.org/dig"
1415
)
1516

@@ -59,8 +60,8 @@ type TailscaleService struct {
5960
apiToken string
6061

6162
caches struct {
62-
devices *CacheStore[tailscaleAPIDevices]
63-
users *CacheStore[tailscaleAPIUsers]
63+
devices *cache.CacheStore[tailscaleAPIDevices]
64+
users *cache.CacheStore[tailscaleAPIUsers]
6465
}
6566

6667
urls struct {
@@ -100,8 +101,8 @@ func NewTailscaleService(i TailscaleServiceInput) (*TailscaleService, error) {
100101
apiToken: apiToken,
101102
}
102103

103-
devicesCache := NewCacheStore[tailscaleAPIDevices](0)
104-
usersCache := NewCacheStore[tailscaleAPIUsers](0)
104+
devicesCache := cache.NewCacheStore[tailscaleAPIDevices](0)
105+
usersCache := cache.NewCacheStore[tailscaleAPIUsers](0)
105106

106107
s.caches.devices = devicesCache
107108
s.caches.users = usersCache
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package service
1+
package cache
22

33
import (
44
"slices"
@@ -33,8 +33,8 @@ func NewCacheStore[T any](maxSize int) *CacheStore[T] {
3333
}
3434
}
3535

36-
// With lock allows performing multiple operations on the cache store atomically.
37-
// The provided mutate function receives a set of actions (Set, Get, Delete) that
36+
// WithLock allows performing multiple operations on a single lock.
37+
// The provided mutate function receives a set of actions (Set, Get, Delete, Update) that
3838
// can be used to manipulate the cache store within the locked context.
3939
func (cs *CacheStore[T]) WithLock(mutate func(actions CacheStoreActions[T])) {
4040
cs.mu.Lock()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package service
1+
package cache
22

33
import (
44
"strconv"

0 commit comments

Comments
 (0)