forked from jellydator/ttlcache
-
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
Showing
4 changed files
with
193 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package ttlcache | ||
|
||
import "time" | ||
|
||
// Option sets a specific cache option. | ||
type Option[K comparable, V any] interface { | ||
apply(opts *options[K, V]) | ||
} | ||
|
||
// optionFunc wraps a function and implements the Option interface. | ||
type optionFunc[K comparable, V any] func(*options[K, V]) | ||
|
||
// apply calls the wrapped function. | ||
func (fn optionFunc[K, V]) apply(opts *options[K, V]) { | ||
fn(opts) | ||
} | ||
|
||
// options holds all available cache configuration options. | ||
type options[K comparable, V any] struct { | ||
capacity uint64 | ||
ttl time.Duration | ||
loader Loader[K, V] | ||
} | ||
|
||
// applyOptions applies the provided option values to the option struct. | ||
func applyOptions[K comparable, V any](v *options[K, V], opts ...Option[K, V]) { | ||
for i := range opts { | ||
opts[i].apply(v) | ||
} | ||
} | ||
|
||
// WithCapacity sets the maximum capacity of the cache. | ||
// It has no effect when passing into Get(). | ||
func WithCapacity[K comparable, V any](c uint64) Option[K, V] { | ||
return optionFunc[K, V](func(opts *options[K, V]) { | ||
opts.capacity = c | ||
}) | ||
} | ||
|
||
// WithTTL sets the TTL of the cache. | ||
// It has no effect when passing into Get(). | ||
func WithTTL[K comparable, V any](ttl time.Duration) Option[K, V] { | ||
return optionFunc[K, V](func(opts *options[K, V]) { | ||
opts.ttl = ttl | ||
}) | ||
} | ||
|
||
// WithLoader sets the loader of the cache. | ||
// When passing into Get(), it sets an epheral loader that | ||
// is used instead of the cache's default one. | ||
func WithLoader[K comparable, V any](l Loader[K, V]) Option[K, V] { | ||
return optionFunc[K, V](func(opts *options[K, V]) { | ||
opts.loader = l | ||
}) | ||
} |
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,53 @@ | ||
package ttlcache | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_optionFunc_apply(t *testing.T) { | ||
var called bool | ||
|
||
optionFunc[string, string](func(_ *options[string, string]) { | ||
called = true | ||
}).apply(nil) | ||
assert.True(t, called) | ||
} | ||
|
||
func Test_applyOptions(t *testing.T) { | ||
var opts options[string, string] | ||
|
||
applyOptions(&opts, | ||
WithCapacity[string, string](12), | ||
WithTTL[string, string](time.Hour), | ||
) | ||
|
||
assert.Equal(t, uint64(12), opts.capacity) | ||
assert.Equal(t, time.Hour, opts.ttl) | ||
} | ||
|
||
func Test_WithCapacity(t *testing.T) { | ||
var opts options[string, string] | ||
|
||
WithCapacity[string, string](12).apply(&opts) | ||
assert.Equal(t, uint64(12), opts.capacity) | ||
} | ||
|
||
func Test_WithTTL(t *testing.T) { | ||
var opts options[string, string] | ||
|
||
WithTTL[string, string](time.Hour).apply(&opts) | ||
assert.Equal(t, time.Hour, opts.ttl) | ||
} | ||
|
||
func Test_WithLoader(t *testing.T) { | ||
var opts options[string, string] | ||
|
||
l := LoaderFunc[string, string](func(_ *Cache[string, string], _ string) *Item[string, string] { | ||
return nil | ||
}) | ||
WithLoader[string, string](l).apply(&opts) | ||
assert.NotNil(t, opts.loader) | ||
} |