mirror of
https://github.com/go-micro/go-micro.git
synced 2025-11-23 21:44:41 +02:00
Add simple in-memory cache (#2231)
* Add simple in-memory cache * Support configuring cache expiration duration * Support preinitializing cache with items * Register cache
This commit is contained in:
40
cache/options.go
vendored
Normal file
40
cache/options.go
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
package cache
|
||||
|
||||
import "time"
|
||||
|
||||
// Options represents the options for the cache.
|
||||
type Options struct {
|
||||
Expiration time.Duration
|
||||
Items map[string]Item
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
Reference in New Issue
Block a user