Skip to content

Commit

Permalink
Merge pull request #30 from origadmin/dev
Browse files Browse the repository at this point in the history
refactor(cache): implement a new memory cache for security module
  • Loading branch information
godcong authored Nov 25, 2024
2 parents f2fcb2a + e3d4aed commit 993045e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import (
)

const (
ErrNotFound = errors.String("not found")
defaultSize = 64 * 1024 * 1024
)

const (
ErrNotFound = errors.String("not found")
)

type Cache struct {
Expiration time.Duration
CleanupInterval time.Duration
Expand Down
80 changes: 80 additions & 0 deletions security/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Package security implements the functions, types, and interfaces for the module.
package security

import (
"sync"
"time"

"github.com/origadmin/toolkits/errors"

"github.com/origadmin/toolkits/context"
"github.com/origadmin/toolkits/storage/cache"
)

type element struct {
value string
expireAt time.Time
}

type securityCache struct {
maps sync.Map
}

func (s *securityCache) Get(ctx context.Context, key string) (string, error) {
value, ok := s.maps.Load(key)
if !ok {
return "", cache.ErrNotFound
}
ele, ok := value.(*element)
if !ok {
return "", errors.New("invalid cache value")
}
if ele.expireAt.Before(time.Now()) {
_ = s.Delete(ctx, key)
return "", cache.ErrNotFound
}
return ele.value, nil
}

func (s *securityCache) GetAndDelete(ctx context.Context, key string) (string, error) {
value, ok := s.maps.LoadAndDelete(key)
if !ok {
return "", cache.ErrNotFound
}
ele, ok := value.(*element)
if !ok {
return "", errors.New("invalid cache value")
}
return ele.value, nil
}

func (s *securityCache) Exists(ctx context.Context, key string) error {
_, ok := s.maps.Load(key)
if !ok {
return cache.ErrNotFound
}
return nil
}

func (s *securityCache) Set(ctx context.Context, key string, value string, expiration ...time.Duration) error {
var expireAt time.Time
if len(expiration) > 0 {
expireAt = time.Now().Add(expiration[0])
} else {
expireAt = time.Now().Add(time.Hour)
}
ele := &element{value: value, expireAt: expireAt}
s.maps.Store(key, ele)
return nil
}

func (s *securityCache) Delete(ctx context.Context, key string) error {
s.maps.Delete(key)
return nil
}

func NewSecurityCache() cache.Cache {
return &securityCache{}
}

var _ cache.Cache = (*securityCache)(nil)
7 changes: 1 addition & 6 deletions security/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/goexts/generic/settings"

"github.com/origadmin/toolkits/storage/cache"
"github.com/origadmin/toolkits/storage/cache/memory"
)

const (
Expand Down Expand Up @@ -62,11 +61,7 @@ func NewTokenStorage(ss ...StorageSetting) TokenStorage {
}, ss)

if opt.Cache == nil {
c := memory.NewCache()
c.DefaultExpiration = 24 * time.Hour
c.CleanupInterval = 30 * time.Minute
c.Delimiter = ":"
opt.Cache = c
opt.Cache = NewSecurityCache()
}

s := &tokenStorage{
Expand Down
4 changes: 2 additions & 2 deletions storage/cache/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func (c *cacheError) Is(err error) bool {
}

var (
ErrCacheClosed error = &cacheError{msg: "cache closed"}
ErrCacheNotFound error = &cacheError{msg: "cache not found"}
ErrClosed error = &cacheError{msg: "cache closed"}
ErrNotFound error = &cacheError{msg: "cache not found"}
)

func NewError(msg string) error {
Expand Down

0 comments on commit 993045e

Please sign in to comment.