forked from raystack/frontier
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd54d61
commit bf8c863
Showing
13 changed files
with
357 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package cache | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/dgraph-io/ristretto" | ||
) | ||
|
||
var ErrParsing = errors.New("parsing error") | ||
|
||
type Config struct { | ||
NumCounters int64 `yaml:"num_counters" mapstructure:"num_counters" default:"10000000"` | ||
MaxCost int64 `yaml:"max_cost" mapstructure:"max_cost" default:"1073741824"` | ||
BufferItems int64 `yaml:"buffer_items" mapstructure:"buffer_items" default:"64"` | ||
Metrics bool `yaml:"metrics" mapstructure:"metrics" default:"true"` | ||
TTLInSeconds int `yaml:"ttl_in_seconds" mapstructure:"ttl_in_seconds" default:"3600"` | ||
} | ||
|
||
type Cache struct { | ||
*ristretto.Cache | ||
config Config | ||
} | ||
|
||
func NewCache(config Config) (Cache, error) { | ||
cache, err := ristretto.NewCache(&ristretto.Config{ | ||
NumCounters: config.NumCounters, | ||
MaxCost: config.MaxCost, | ||
BufferItems: config.MaxCost, | ||
Metrics: config.Metrics, | ||
}) | ||
if err != nil { | ||
return Cache{}, err | ||
} | ||
|
||
return Cache{ | ||
Cache: cache, | ||
config: config, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/goto/shield/core/group" | ||
) | ||
|
||
var keyPrefix = "group" | ||
|
||
type GroupRepository interface { | ||
GetBySlug(ctx context.Context, slug string) (group.Group, error) | ||
} | ||
|
||
type CachedGroupRepository struct { | ||
cache Cache | ||
repository GroupRepository | ||
} | ||
|
||
func NewCachedGroupRepository(cache Cache, repository GroupRepository) *CachedGroupRepository { | ||
return &CachedGroupRepository{ | ||
cache: cache, | ||
repository: repository, | ||
} | ||
} | ||
|
||
func getKey(identifier string) string { | ||
return fmt.Sprintf("%s:%s", keyPrefix, identifier) | ||
} | ||
|
||
func (r CachedGroupRepository) GetBySlug(ctx context.Context, slug string) (group.Group, error) { | ||
key := getKey(slug) | ||
grp, found := r.cache.Get(key) | ||
if !found { | ||
grp, err := r.repository.GetBySlug(ctx, slug) | ||
if err != nil { | ||
return group.Group{}, err | ||
} | ||
|
||
r.cache.SetWithTTL(key, grp, 0, time.Duration(r.cache.config.TTLInSeconds)*time.Second) | ||
return grp, nil | ||
} | ||
|
||
grpParsed, ok := grp.(group.Group) | ||
if !ok { | ||
return group.Group{}, ErrParsing | ||
} | ||
|
||
return grpParsed, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/goto/shield/core/group" | ||
"github.com/goto/shield/internal/store/cache/mocks" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
var ( | ||
testCacheConfig = Config{ | ||
NumCounters: 10000000, | ||
MaxCost: 1073741824, | ||
BufferItems: 64, | ||
Metrics: true, | ||
TTLInSeconds: 3600, | ||
} | ||
testGroupSlug = "test-group-slug" | ||
testGroup = group.Group{ | ||
ID: "test-group-id", | ||
Slug: testGroupSlug, | ||
Name: "test group", | ||
} | ||
) | ||
|
||
func TestGetBySlug(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
description string | ||
slug string | ||
setup func(t *testing.T) *CachedGroupRepository | ||
want group.Group | ||
wantErr error | ||
}{ | ||
{ | ||
description: "should retrieve group from cache", | ||
slug: testGroupSlug, | ||
setup: func(t *testing.T) *CachedGroupRepository { | ||
t.Helper() | ||
groupRepository := &mocks.GroupRepository{} | ||
c, err := NewCache(testCacheConfig) | ||
if err != nil { | ||
return nil | ||
} | ||
c.Set(getKey(testGroupSlug), testGroup, 0) | ||
return NewCachedGroupRepository(c, groupRepository) | ||
}, | ||
want: testGroup, | ||
}, | ||
{ | ||
description: "should retrieve group from repository", | ||
slug: testGroupSlug, | ||
setup: func(t *testing.T) *CachedGroupRepository { | ||
t.Helper() | ||
groupRepository := &mocks.GroupRepository{} | ||
c, err := NewCache(testCacheConfig) | ||
if err != nil { | ||
return nil | ||
} | ||
groupRepository.EXPECT().GetBySlug(mock.Anything, testGroupSlug). | ||
Return(testGroup, nil) | ||
return NewCachedGroupRepository(c, groupRepository) | ||
}, | ||
want: testGroup, | ||
}, | ||
{ | ||
description: "should return parse error if cache data invalid", | ||
slug: testGroupSlug, | ||
setup: func(t *testing.T) *CachedGroupRepository { | ||
t.Helper() | ||
groupRepository := &mocks.GroupRepository{} | ||
c, err := NewCache(testCacheConfig) | ||
if err != nil { | ||
return nil | ||
} | ||
c.Set(getKey(testGroupSlug), "invalid-group-data", 0) | ||
return NewCachedGroupRepository(c, groupRepository) | ||
}, | ||
wantErr: ErrParsing, | ||
}, | ||
{ | ||
description: "should return error from repository", | ||
slug: testGroupSlug, | ||
setup: func(t *testing.T) *CachedGroupRepository { | ||
t.Helper() | ||
groupRepository := &mocks.GroupRepository{} | ||
c, err := NewCache(testCacheConfig) | ||
if err != nil { | ||
return nil | ||
} | ||
groupRepository.EXPECT().GetBySlug(mock.Anything, testGroupSlug). | ||
Return(group.Group{}, group.ErrInvalidDetail) | ||
return NewCachedGroupRepository(c, groupRepository) | ||
}, | ||
wantErr: group.ErrInvalidDetail, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
t.Run(tc.description, func(t *testing.T) { | ||
t.Parallel() | ||
cacheRepo := tc.setup(t) | ||
assert.NotNil(t, cacheRepo) | ||
|
||
ctx := context.TODO() | ||
got, err := cacheRepo.GetBySlug(ctx, tc.slug) | ||
if tc.wantErr != nil { | ||
assert.Error(t, err) | ||
assert.True(t, errors.Is(err, tc.wantErr)) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
assert.Equal(t, tc.want, got) | ||
}) | ||
} | ||
} |
Oops, something went wrong.