-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Feature/internal/sync map #2109
Changes from all commits
aee8970
70f0040
aed5069
6c72b3a
11eb3ab
60229ed
5366f3c
194fd42
ecea10c
9dee4b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,25 +26,25 @@ import ( | |
"github.com/vdaas/vald/internal/errors" | ||
) | ||
|
||
type cache struct { | ||
type cache[V any] struct { | ||
cacher cacher.Type | ||
expireDur time.Duration | ||
expireCheckDur time.Duration | ||
expiredHook func(context.Context, string) | ||
} | ||
|
||
// New returns the Cache instance or error. | ||
func New(opts ...Option) (cc cacher.Cache, err error) { | ||
c := new(cache) | ||
for _, opt := range append(defaultOptions, opts...) { | ||
func New[V any](opts ...Option[V]) (cc cacher.Cache[V], err error) { | ||
c := new(cache[V]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci] reported by reviewdog 🐶 |
||
for _, opt := range append(defaultOptions[V](), opts...) { | ||
opt(c) | ||
} | ||
switch c.cacher { | ||
case cacher.GACHE: | ||
return gache.New( | ||
gache.WithExpireDuration(c.expireDur), | ||
gache.WithExpireCheckDuration(c.expireCheckDur), | ||
gache.WithExpiredHook(c.expiredHook), | ||
return gache.New[V]( | ||
gache.WithExpireDuration[V](c.expireDur), | ||
gache.WithExpireCheckDuration[V](c.expireCheckDur), | ||
gache.WithExpiredHook[V](c.expiredHook), | ||
), nil | ||
default: | ||
return nil, errors.ErrInvalidCacherType | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,21 +21,21 @@ import ( | |
"context" | ||
"time" | ||
|
||
"github.com/kpango/gache" | ||
gache "github.com/kpango/gache/v2" | ||
"github.com/vdaas/vald/internal/cache/cacher" | ||
) | ||
|
||
type cache struct { | ||
gache gache.Gache | ||
type cache[V any] struct { | ||
gache gache.Gache[V] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci] reported by reviewdog 🐶 |
||
expireDur time.Duration | ||
expireCheckDur time.Duration | ||
expiredHook func(context.Context, string) | ||
} | ||
|
||
// New loads a cache model and returns a new cache struct. | ||
func New(opts ...Option) cacher.Cache { | ||
c := new(cache) | ||
for _, opt := range append(defaultOptions(), opts...) { | ||
func New[V any](opts ...Option[V]) cacher.Cache[V] { | ||
c := new(cache[V]) | ||
for _, opt := range append(defaultOptions[V](), opts...) { | ||
opt(c) | ||
} | ||
c.gache.SetDefaultExpire(c.expireDur) | ||
|
@@ -48,31 +48,31 @@ func New(opts ...Option) cacher.Cache { | |
} | ||
|
||
// Start calls StartExpired func of c.gache. | ||
func (c *cache) Start(ctx context.Context) { | ||
func (c *cache[V]) Start(ctx context.Context) { | ||
c.gache.StartExpired(ctx, c.expireCheckDur) | ||
} | ||
|
||
// Get calls StartExpired func of c.gache and returns (interface{}, bool) according to key. | ||
func (c *cache) Get(key string) (interface{}, bool) { | ||
func (c *cache[V]) Get(key string) (V, bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci] reported by reviewdog 🐶 |
||
return c.gache.Get(key) | ||
} | ||
|
||
// Set calls Set func of c.gache. | ||
func (c *cache) Set(key string, val interface{}) { | ||
func (c *cache[V]) Set(key string, val V) { | ||
c.gache.Set(key, val) | ||
} | ||
|
||
// Delete calls Delete func of c.gache. | ||
func (c *cache) Delete(key string) { | ||
func (c *cache[V]) Delete(key string) { | ||
c.gache.Delete(key) | ||
} | ||
|
||
// GetAndDelete returns (interface{}, bool) and delete value according to key when value of key is set. | ||
// When value of key is not set, returns (nil, false). | ||
func (c *cache) GetAndDelete(key string) (interface{}, bool) { | ||
func (c *cache[V]) GetAndDelete(key string) (V, bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci] reported by reviewdog 🐶 |
||
v, ok := c.gache.Get(key) | ||
if !ok { | ||
return nil, false | ||
return *new(V), false | ||
} | ||
c.gache.Delete(key) | ||
return v, true | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚫 [golangci] reported by reviewdog 🐶
named return "cc" with type "cacher.Cache[V]" found (nonamedreturns)