1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-18 08:26:38 +02:00
go-micro/cache/options.go
Johnson C 7a6a2b6373
feat: redis implementation of cache (#2513)
* feat: redis implementation of cache

* Revert "feat: redis implementation of cache"

This reverts commit 4b3f25c1fc.

* feat: redis implementation of cache
2022-06-21 20:05:00 +01:00

62 lines
1.3 KiB
Go

package cache
import (
"context"
"time"
)
// Options represents the options for the cache.
type Options struct {
Expiration time.Duration
Items map[string]Item
// Address represents the address or other connection information of the cache service.
Address string
// Context should contain all implementation specific options, using context.WithValue.
Context context.Context
}
// 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
}
}
// WithAddress sets the cache service address or connection information
func WithAddress(addr string) Option {
return func(o *Options) {
o.Address = addr
}
}
// WithContext sets the cache context, for any extra configuration
func WithContext(c context.Context) Option {
return func(o *Options) {
o.Context = c
}
}
// NewOptions returns a new options struct.
func NewOptions(opts ...Option) Options {
options := Options{
Expiration: DefaultExpiration,
Items: make(map[string]Item),
}
for _, o := range opts {
o(&options)
}
return options
}