2021-08-31 16:31:16 +02:00
|
|
|
package cache
|
|
|
|
|
2022-06-21 21:05:00 +02:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
2022-09-29 16:44:53 +02:00
|
|
|
|
2024-06-04 22:40:43 +02:00
|
|
|
"go-micro.dev/v5/logger"
|
2022-06-21 21:05:00 +02:00
|
|
|
)
|
2021-08-31 16:31:16 +02:00
|
|
|
|
|
|
|
// Options represents the options for the cache.
|
|
|
|
type Options struct {
|
2022-06-21 21:05:00 +02:00
|
|
|
// Context should contain all implementation specific options, using context.WithValue.
|
|
|
|
Context context.Context
|
2022-09-29 16:44:53 +02:00
|
|
|
// Logger is the be used logger
|
|
|
|
Logger logger.Logger
|
2023-04-26 02:16:34 +02:00
|
|
|
Items map[string]Item
|
|
|
|
// Address represents the address or other connection information of the cache service.
|
|
|
|
Address string
|
|
|
|
Expiration time.Duration
|
2021-08-31 16:31:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Option manipulates the Options passed.
|
|
|
|
type Option func(o *Options)
|
|
|
|
|
|
|
|
// Expiration sets the duration for items stored in the cache to expire.
|
|
|
|
func Expiration(d time.Duration) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Expiration = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Items initializes the cache with preconfigured items.
|
|
|
|
func Items(i map[string]Item) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Items = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// WithAddress sets the cache service address or connection information.
|
2022-06-21 21:05:00 +02:00
|
|
|
func WithAddress(addr string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Address = addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// WithContext sets the cache context, for any extra configuration.
|
2022-06-21 21:05:00 +02:00
|
|
|
func WithContext(c context.Context) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Context = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// WithLogger sets underline logger.
|
2022-09-29 16:44:53 +02:00
|
|
|
func WithLogger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 16:31:16 +02:00
|
|
|
// NewOptions returns a new options struct.
|
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
|
|
|
Expiration: DefaultExpiration,
|
|
|
|
Items: make(map[string]Item),
|
2022-09-29 16:44:53 +02:00
|
|
|
Logger: logger.DefaultLogger,
|
2021-08-31 16:31:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|