Skip to content

Commit

Permalink
Merge pull request #181 from sgtsquiggs/expose-invalidation-options
Browse files Browse the repository at this point in the history
Expose option details
  • Loading branch information
eko committed Nov 19, 2022
2 parents cff4461 + 36390c6 commit 0b81e39
Show file tree
Hide file tree
Showing 19 changed files with 92 additions and 92 deletions.
10 changes: 5 additions & 5 deletions store/bigcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type BigcacheStore struct {
func NewBigcache(client BigcacheClientInterface, options ...Option) *BigcacheStore {
return &BigcacheStore{
client: client,
options: applyOptions(options...),
options: ApplyOptions(options...),
}
}

Expand All @@ -58,7 +58,7 @@ func (s *BigcacheStore) GetWithTTL(ctx context.Context, key any) (any, time.Dura

// Set defines data in Bigcache for given key identifier
func (s *BigcacheStore) Set(ctx context.Context, key any, value any, options ...Option) error {
opts := applyOptionsWithDefault(s.options, options...)
opts := ApplyOptionsWithDefault(s.options, options...)

var val []byte
switch v := value.(type) {
Expand All @@ -75,7 +75,7 @@ func (s *BigcacheStore) Set(ctx context.Context, key any, value any, options ...
return err
}

if tags := opts.tags; len(tags) > 0 {
if tags := opts.Tags; len(tags) > 0 {
s.setTags(ctx, key, tags)
}

Expand Down Expand Up @@ -116,9 +116,9 @@ func (s *BigcacheStore) Delete(_ context.Context, key any) error {

// Invalidate invalidates some cache data in Bigcache for given options
func (s *BigcacheStore) Invalidate(ctx context.Context, options ...InvalidateOption) error {
opts := applyInvalidateOptions(options...)
opts := ApplyInvalidateOptions(options...)

if tags := opts.tags; len(tags) > 0 {
if tags := opts.Tags; len(tags) > 0 {
for _, tag := range tags {
tagKey := fmt.Sprintf(BigcacheTagPattern, tag)
result, err := s.Get(ctx, tagKey)
Expand Down
12 changes: 6 additions & 6 deletions store/freecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type FreecacheStore struct {
func NewFreecache(client FreecacheClientInterface, options ...Option) *FreecacheStore {
return &FreecacheStore{
client: client,
options: applyOptions(options...),
options: ApplyOptions(options...),
}
}

Expand Down Expand Up @@ -84,7 +84,7 @@ func (f *FreecacheStore) Set(ctx context.Context, key any, value any, options ..
var val []byte

// Using default options set during cache initialization
opts := applyOptionsWithDefault(f.options, options...)
opts := ApplyOptionsWithDefault(f.options, options...)

// type check for value, as freecache only supports value of type []byte
switch v := value.(type) {
Expand All @@ -95,11 +95,11 @@ func (f *FreecacheStore) Set(ctx context.Context, key any, value any, options ..
}

if k, ok := key.(string); ok {
err = f.client.Set([]byte(k), val, int(opts.expiration.Seconds()))
err = f.client.Set([]byte(k), val, int(opts.Expiration.Seconds()))
if err != nil {
return fmt.Errorf("size of key: %v, value: %v, err: %v", k, len(val), err)
}
if tags := opts.tags; len(tags) > 0 {
if tags := opts.Tags; len(tags) > 0 {
f.setTags(ctx, key, tags)
}
return nil
Expand Down Expand Up @@ -151,9 +151,9 @@ func (f *FreecacheStore) Delete(_ context.Context, key any) error {

// Invalidate invalidates some cache data in freecache for given options
func (f *FreecacheStore) Invalidate(ctx context.Context, options ...InvalidateOption) error {
opts := applyInvalidateOptions(options...)
opts := ApplyInvalidateOptions(options...)

if tags := opts.tags; len(tags) > 0 {
if tags := opts.Tags; len(tags) > 0 {
for _, tag := range tags {
tagKey := fmt.Sprintf(FreecacheTagPattern, tag)
cacheKeys := f.getCacheKeysForTag(ctx, tagKey)
Expand Down
2 changes: 1 addition & 1 deletion store/freecache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestNewFreecache(t *testing.T) {
assert.IsType(t, new(FreecacheStore), store)
assert.Equal(t, client, store.client)
assert.Equal(t, &Options{
expiration: 6 * time.Second,
Expiration: 6 * time.Second,
}, store.options)
}

Expand Down
12 changes: 6 additions & 6 deletions store/go_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type GoCacheStore struct {
func NewGoCache(client GoCacheClientInterface, options ...Option) *GoCacheStore {
return &GoCacheStore{
client: client,
options: applyOptions(options...),
options: ApplyOptions(options...),
}
}

Expand Down Expand Up @@ -63,14 +63,14 @@ func (s *GoCacheStore) GetWithTTL(_ context.Context, key any) (any, time.Duratio

// Set defines data in GoCache memoey cache for given key identifier
func (s *GoCacheStore) Set(ctx context.Context, key any, value any, options ...Option) error {
opts := applyOptions(options...)
opts := ApplyOptions(options...)
if opts == nil {
opts = s.options
}

s.client.Set(key.(string), value, opts.expiration)
s.client.Set(key.(string), value, opts.Expiration)

if tags := opts.tags; len(tags) > 0 {
if tags := opts.Tags; len(tags) > 0 {
s.setTags(ctx, key, tags)
}

Expand Down Expand Up @@ -115,9 +115,9 @@ func (s *GoCacheStore) Delete(_ context.Context, key any) error {

// Invalidate invalidates some cache data in GoCache memoey cache for given options
func (s *GoCacheStore) Invalidate(ctx context.Context, options ...InvalidateOption) error {
opts := applyInvalidateOptions(options...)
opts := ApplyInvalidateOptions(options...)

if tags := opts.tags; len(tags) > 0 {
if tags := opts.Tags; len(tags) > 0 {
for _, tag := range tags {
tagKey := fmt.Sprintf(GoCacheTagPattern, tag)
result, err := s.Get(ctx, tagKey)
Expand Down
2 changes: 1 addition & 1 deletion store/go_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestNewGoCache(t *testing.T) {
// Then
assert.IsType(t, new(GoCacheStore), store)
assert.Equal(t, client, store.client)
assert.Equal(t, &Options{cost: 8}, store.options)
assert.Equal(t, &Options{Cost: 8}, store.options)
}

func TestGoCacheGet(t *testing.T) {
Expand Down
24 changes: 12 additions & 12 deletions store/invalidate_options.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package store

// InvalidateOption represents a cache invalidation function.
type InvalidateOption func(o *invalidateOptions)
type InvalidateOption func(o *InvalidateOptions)

type invalidateOptions struct {
tags []string
type InvalidateOptions struct {
Tags []string
}

func (o *invalidateOptions) isEmpty() bool {
return len(o.tags) == 0
func (o *InvalidateOptions) isEmpty() bool {
return len(o.Tags) == 0
}

func applyInvalidateOptionsWithDefault(defaultOptions *invalidateOptions, opts ...InvalidateOption) *invalidateOptions {
returnedOptions := applyInvalidateOptions(opts...)
func ApplyInvalidateOptionsWithDefault(defaultOptions *InvalidateOptions, opts ...InvalidateOption) *InvalidateOptions {
returnedOptions := ApplyInvalidateOptions(opts...)

if returnedOptions == new(invalidateOptions) {
if returnedOptions == new(InvalidateOptions) {
returnedOptions = defaultOptions
}

return returnedOptions
}

func applyInvalidateOptions(opts ...InvalidateOption) *invalidateOptions {
o := &invalidateOptions{}
func ApplyInvalidateOptions(opts ...InvalidateOption) *InvalidateOptions {
o := &InvalidateOptions{}

for _, opt := range opts {
opt(o)
Expand All @@ -33,7 +33,7 @@ func applyInvalidateOptions(opts ...InvalidateOption) *invalidateOptions {

// WithInvalidateTags allows setting the invalidate tags.
func WithInvalidateTags(tags []string) InvalidateOption {
return func(o *invalidateOptions) {
o.tags = tags
return func(o *InvalidateOptions) {
o.Tags = tags
}
}
6 changes: 3 additions & 3 deletions store/invalidate_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

func TestInvalidateOptionsTagsValue(t *testing.T) {
// Given
options := invalidateOptions{
tags: []string{"tag1", "tag2", "tag3"},
options := InvalidateOptions{
Tags: []string{"tag1", "tag2", "tag3"},
}

// When - Then
assert.Equal(t, []string{"tag1", "tag2", "tag3"}, options.tags)
assert.Equal(t, []string{"tag1", "tag2", "tag3"}, options.Tags)
}
12 changes: 6 additions & 6 deletions store/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type MemcacheStore struct {
func NewMemcache(client MemcacheClientInterface, options ...Option) *MemcacheStore {
return &MemcacheStore{
client: client,
options: applyOptions(options...),
options: ApplyOptions(options...),
}
}

Expand Down Expand Up @@ -73,20 +73,20 @@ func (s *MemcacheStore) GetWithTTL(_ context.Context, key any) (any, time.Durati

// Set defines data in Memcache for given key identifier
func (s *MemcacheStore) Set(ctx context.Context, key any, value any, options ...Option) error {
opts := applyOptionsWithDefault(s.options, options...)
opts := ApplyOptionsWithDefault(s.options, options...)

item := &memcache.Item{
Key: key.(string),
Value: value.([]byte),
Expiration: int32(opts.expiration.Seconds()),
Expiration: int32(opts.Expiration.Seconds()),
}

err := s.client.Set(item)
if err != nil {
return err
}

if tags := opts.tags; len(tags) > 0 {
if tags := opts.Tags; len(tags) > 0 {
s.setTags(ctx, key, tags)
}

Expand Down Expand Up @@ -163,9 +163,9 @@ func (s *MemcacheStore) Delete(_ context.Context, key any) error {

// Invalidate invalidates some cache data in Memcache for given options
func (s *MemcacheStore) Invalidate(ctx context.Context, options ...InvalidateOption) error {
opts := applyInvalidateOptions(options...)
opts := ApplyInvalidateOptions(options...)

if tags := opts.tags; len(tags) > 0 {
if tags := opts.Tags; len(tags) > 0 {
for _, tag := range tags {
tagKey := fmt.Sprintf(MemcacheTagPattern, tag)
result, err := s.Get(ctx, tagKey)
Expand Down
2 changes: 1 addition & 1 deletion store/memcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestNewMemcache(t *testing.T) {
// Then
assert.IsType(t, new(MemcacheStore), store)
assert.Equal(t, client, store.client)
assert.Equal(t, &Options{expiration: 3 * time.Second}, store.options)
assert.Equal(t, &Options{Expiration: 3 * time.Second}, store.options)
}

func TestMemcacheGet(t *testing.T) {
Expand Down
20 changes: 10 additions & 10 deletions store/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import (
type Option func(o *Options)

type Options struct {
cost int64
expiration time.Duration
tags []string
Cost int64
Expiration time.Duration
Tags []string
}

func (o *Options) isEmpty() bool {
return o.cost == 0 && o.expiration == 0 && len(o.tags) == 0
func (o *Options) IsEmpty() bool {
return o.Cost == 0 && o.Expiration == 0 && len(o.Tags) == 0
}

func applyOptionsWithDefault(defaultOptions *Options, opts ...Option) *Options {
func ApplyOptionsWithDefault(defaultOptions *Options, opts ...Option) *Options {
returnedOptions := &Options{}
*returnedOptions = *defaultOptions

Expand All @@ -28,7 +28,7 @@ func applyOptionsWithDefault(defaultOptions *Options, opts ...Option) *Options {
return returnedOptions
}

func applyOptions(opts ...Option) *Options {
func ApplyOptions(opts ...Option) *Options {
o := &Options{}

for _, opt := range opts {
Expand All @@ -42,20 +42,20 @@ func applyOptions(opts ...Option) *Options {
// Actually it seems to be used by Ristretto library only.
func WithCost(cost int64) Option {
return func(o *Options) {
o.cost = cost
o.Cost = cost
}
}

// WithExpiration allows to specify an expiration time when setting a value.
func WithExpiration(expiration time.Duration) Option {
return func(o *Options) {
o.expiration = expiration
o.Expiration = expiration
}
}

// WithTags allows to specify associated tags to the current value.
func WithTags(tags []string) Option {
return func(o *Options) {
o.tags = tags
o.Tags = tags
}
}
20 changes: 10 additions & 10 deletions store/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,43 @@ import (
func TestOptionsCostValue(t *testing.T) {
// Given
options := &Options{
cost: 7,
Cost: 7,
}

// When - Then
assert.Equal(t, int64(7), options.cost)
assert.Equal(t, int64(7), options.Cost)
}

func TestOptionsExpirationValue(t *testing.T) {
// Given
options := &Options{
expiration: 25 * time.Second,
Expiration: 25 * time.Second,
}

// When - Then
assert.Equal(t, 25*time.Second, options.expiration)
assert.Equal(t, 25*time.Second, options.Expiration)
}

func TestOptionsTagsValue(t *testing.T) {
// Given
options := &Options{
tags: []string{"tag1", "tag2", "tag3"},
Tags: []string{"tag1", "tag2", "tag3"},
}

// When - Then
assert.Equal(t, []string{"tag1", "tag2", "tag3"}, options.tags)
assert.Equal(t, []string{"tag1", "tag2", "tag3"}, options.Tags)
}

func Test_applyOptionsWithDefault(t *testing.T) {
// Given
defaultOptions := &Options{
expiration: 25 * time.Second,
Expiration: 25 * time.Second,
}

// When
options := applyOptionsWithDefault(defaultOptions, WithCost(7))
options := ApplyOptionsWithDefault(defaultOptions, WithCost(7))

// Then
assert.Equal(t, int64(7), options.cost)
assert.Equal(t, 25*time.Second, options.expiration)
assert.Equal(t, int64(7), options.Cost)
assert.Equal(t, 25*time.Second, options.Expiration)
}
10 changes: 5 additions & 5 deletions store/options_test_matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func (m OptionsMatcher) Matches(x interface{}) bool {
value(opts)
}

return opts.cost == m.Cost &&
opts.expiration == m.Expiration &&
slices.Equal(opts.tags, m.Tags)
return opts.Cost == m.Cost &&
opts.Expiration == m.Expiration &&
slices.Equal(opts.Tags, m.Tags)
}

return false
Expand All @@ -45,12 +45,12 @@ type InvalidateOptionsMatcher struct {
func (m InvalidateOptionsMatcher) Matches(x interface{}) bool {
switch values := x.(type) {
case []InvalidateOption:
opts := &invalidateOptions{}
opts := &InvalidateOptions{}
for _, value := range values {
value(opts)
}

return slices.Equal(opts.tags, m.Tags)
return slices.Equal(opts.Tags, m.Tags)
}

return false
Expand Down
Loading

0 comments on commit 0b81e39

Please sign in to comment.