Skip to content

Commit

Permalink
Merge pull request #160 from eko/public-options
Browse files Browse the repository at this point in the history
Make store options publicly available
  • Loading branch information
eko committed Jul 13, 2022
2 parents 315e76a + a06a387 commit f2ed440
Show file tree
Hide file tree
Showing 18 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion store/bigcache.go
Expand Up @@ -26,7 +26,7 @@ const (
// BigcacheStore is a store for Bigcache
type BigcacheStore struct {
client BigcacheClientInterface
options *options
options *Options
}

// NewBigcache creates a new store to Bigcache instance(s)
Expand Down
2 changes: 1 addition & 1 deletion store/bigcache_test.go
Expand Up @@ -23,7 +23,7 @@ func TestNewBigcache(t *testing.T) {
// Then
assert.IsType(t, new(BigcacheStore), store)
assert.Equal(t, client, store.client)
assert.Equal(t, new(options), store.options)
assert.Equal(t, new(Options), store.options)
}

func TestBigcacheGet(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion store/freecache.go
Expand Up @@ -30,7 +30,7 @@ type FreecacheClientInterface interface {
// FreecacheStore is a store for freecache
type FreecacheStore struct {
client FreecacheClientInterface
options *options
options *Options
}

// NewFreecache creates a new store to freecache instance(s)
Expand Down
4 changes: 2 additions & 2 deletions store/freecache_test.go
Expand Up @@ -24,7 +24,7 @@ func TestNewFreecache(t *testing.T) {
// Then
assert.IsType(t, new(FreecacheStore), store)
assert.Equal(t, client, store.client)
assert.Equal(t, &options{
assert.Equal(t, &Options{
expiration: 6 * time.Second,
}, store.options)
}
Expand All @@ -41,7 +41,7 @@ func TestNewFreecacheDefaultOptions(t *testing.T) {
// Then
assert.IsType(t, new(FreecacheStore), store)
assert.Equal(t, client, store.client)
assert.Equal(t, new(options), store.options)
assert.Equal(t, new(Options), store.options)
}

func TestFreecacheGet(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion store/go_cache.go
Expand Up @@ -28,7 +28,7 @@ type GoCacheClientInterface interface {
type GoCacheStore struct {
mu sync.RWMutex
client GoCacheClientInterface
options *options
options *Options
}

// NewGoCache creates a new store to GoCache (memory) library instance
Expand Down
2 changes: 1 addition & 1 deletion store/go_cache_test.go
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
2 changes: 1 addition & 1 deletion store/memcache.go
Expand Up @@ -34,7 +34,7 @@ const (
// MemcacheStore is a store for Memcache
type MemcacheStore struct {
client MemcacheClientInterface
options *options
options *Options
}

// NewMemcache creates a new store to Memcache instance(s)
Expand Down
2 changes: 1 addition & 1 deletion store/memcache_test.go
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
18 changes: 9 additions & 9 deletions store/options.go
Expand Up @@ -5,19 +5,19 @@ import (
)

// Options represents a store option function.
type Option func(o *options)
type Option func(o *Options)

type options struct {
type Options struct {
cost int64
expiration time.Duration
tags []string
}

func (o *options) isEmpty() bool {
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 := applyOptions(opts...)

if returnedOptions.isEmpty() {
Expand All @@ -27,8 +27,8 @@ func applyOptionsWithDefault(defaultOptions *options, opts ...Option) *options {
return returnedOptions
}

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

for _, opt := range opts {
opt(o)
Expand All @@ -40,21 +40,21 @@ func applyOptions(opts ...Option) *options {
// WithCost allows setting the memory capacity used by the item when setting a value.
// Actually it seems to be used by Ristretto library only.
func WithCost(cost int64) Option {
return func(o *options) {
return func(o *Options) {
o.cost = cost
}
}

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

// WithTags allows to specify associated tags to the current value.
func WithTags(tags []string) Option {
return func(o *options) {
return func(o *Options) {
o.tags = tags
}
}
6 changes: 3 additions & 3 deletions store/options_test.go
Expand Up @@ -9,7 +9,7 @@ import (

func TestOptionsCostValue(t *testing.T) {
// Given
options := &options{
options := &Options{
cost: 7,
}

Expand All @@ -19,7 +19,7 @@ func TestOptionsCostValue(t *testing.T) {

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

Expand All @@ -29,7 +29,7 @@ func TestOptionsExpirationValue(t *testing.T) {

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

Expand Down
2 changes: 1 addition & 1 deletion store/options_test_matchers.go
Expand Up @@ -16,7 +16,7 @@ type OptionsMatcher struct {
func (m OptionsMatcher) Matches(x interface{}) bool {
switch values := x.(type) {
case []Option:
opts := &options{}
opts := &Options{}
for _, value := range values {
value(opts)
}
Expand Down
2 changes: 1 addition & 1 deletion store/pegasus.go
Expand Up @@ -31,7 +31,7 @@ var empty = []byte("-")

// OptionsPegasus is options of Pegasus
type OptionsPegasus struct {
*options
*Options
MetaServers []string

TableName string
Expand Down
2 changes: 1 addition & 1 deletion store/redis.go
Expand Up @@ -30,7 +30,7 @@ const (
// RedisStore is a store for Redis
type RedisStore struct {
client RedisClientInterface
options *options
options *Options
}

// NewRedis creates a new store to Redis instance(s)
Expand Down
2 changes: 1 addition & 1 deletion store/redis_test.go
Expand Up @@ -23,7 +23,7 @@ func TestNewRedis(t *testing.T) {
// Then
assert.IsType(t, new(RedisStore), store)
assert.Equal(t, client, store.client)
assert.Equal(t, &options{expiration: 6 * time.Second}, store.options)
assert.Equal(t, &Options{expiration: 6 * time.Second}, store.options)
}

func TestRedisGet(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion store/rediscluster.go
Expand Up @@ -30,7 +30,7 @@ const (
// RedisStore is a store for Redis
type RedisClusterStore struct {
clusclient RedisClusterClientInterface
options *options
options *Options
}

// NewRedis creates a new store to Redis instance(s)
Expand Down
2 changes: 1 addition & 1 deletion store/rediscluster_test.go
Expand Up @@ -23,7 +23,7 @@ func TestNewRedisCluster(t *testing.T) {
// Then
assert.IsType(t, new(RedisClusterStore), store)
assert.Equal(t, client, store.clusclient)
assert.Equal(t, &options{expiration: 6 * time.Second}, store.options)
assert.Equal(t, &Options{expiration: 6 * time.Second}, store.options)
}

func TestRedisClusterGet(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion store/ristretto.go
Expand Up @@ -26,7 +26,7 @@ type RistrettoClientInterface interface {
// RistrettoStore is a store for Ristretto (memory) library
type RistrettoStore struct {
client RistrettoClientInterface
options *options
options *Options
}

// NewRistretto creates a new store to Ristretto (memory) library instance
Expand Down
2 changes: 1 addition & 1 deletion store/ristretto_test.go
Expand Up @@ -23,7 +23,7 @@ func TestNewRistretto(t *testing.T) {
// Then
assert.IsType(t, new(RistrettoStore), store)
assert.Equal(t, client, store.client)
assert.Equal(t, &options{cost: 8}, store.options)
assert.Equal(t, &Options{cost: 8}, store.options)
}

func TestRistrettoGet(t *testing.T) {
Expand Down

0 comments on commit f2ed440

Please sign in to comment.