Documentation ¶
Overview ¶
Package cached provides a cached provider for xload. This loader can be used to cache the results of any loader.
Useful for loaders that have a high latency, or loaders that are called frequently.
Example ¶
package main import ( "context" "time" "github.com/gojekfarm/xtools/xload" "github.com/gojekfarm/xtools/xload/providers/cached" ) func main() { // This example shows how to use the cached loader // with a remote loader. ctx := context.Background() cfg := struct { Title string `env:"TITLE"` Link string `env:"LINK"` ButtonLabel string `env:"BUTTON_LABEL"` }{} remoteLoader := xload.LoaderFunc(func(ctx context.Context, key string) (string, error) { // Load the value from a remote source. return "", nil }) err := xload.Load( ctx, &cfg, cached.NewLoader( remoteLoader, cached.TTL(5*60*time.Minute), ), ) if err != nil { panic(err) } }
Output:
Example (CustomCache) ¶
package main import ( "context" "time" "github.com/gojekfarm/xtools/xload" "github.com/gojekfarm/xtools/xload/providers/cached" ) type CustomCache struct{} func NewCustomCache() *CustomCache { return &CustomCache{} } func (c *CustomCache) Get(key string) (*string, error) { return nil, nil } func (c *CustomCache) Set(key, value string, ttl time.Duration) error { return nil } func main() { // This example shows how to use a custom cache // with the cached loader. ctx := context.Background() cfg := struct { Title string `env:"TITLE"` Link string `env:"LINK"` ButtonLabel string `env:"BUTTON_LABEL"` }{} remoteLoader := xload.LoaderFunc(func(ctx context.Context, key string) (string, error) { // Load the value from a remote source. return "", nil }) err := xload.Load( ctx, &cfg, cached.NewLoader( remoteLoader, cached.TTL(5*60*time.Minute), cached.Cache(NewCustomCache()), ), ) if err != nil { panic(err) } }
Output:
Example (DisableEmptyValueHit) ¶
package main import ( "context" "time" "github.com/gojekfarm/xtools/xload" "github.com/gojekfarm/xtools/xload/providers/cached" ) func main() { // By default, the cached loader caches empty values. // This example shows how to disable caching of empty values // with the cached loader. ctx := context.Background() cfg := struct { Title string `env:"TITLE"` Link string `env:"LINK"` ButtonLabel string `env:"BUTTON_LABEL"` }{} remoteLoader := xload.LoaderFunc(func(ctx context.Context, key string) (string, error) { // Load the value from a remote source. return "", nil }) err := xload.Load( ctx, &cfg, cached.NewLoader( remoteLoader, cached.TTL(5*60*time.Minute), cached.DisableEmptyValueHit, ), ) if err != nil { panic(err) } }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewLoader ¶
func NewLoader(l xload.Loader, opts ...Option) xload.LoaderFunc
NewLoader returns a new cached loader.
The cached loader uses these defaults: - TTL: 5 minutes. Configurable via `TTL` option. - Cache: A simple unbounded map cache. Configurable via `Cache` option. - Empty value hit: Enabled. Configurable via `DisableEmptyValueHit` option.
Types ¶
type Cacher ¶
type Cacher interface { Get(key string) (*string, error) Set(key, value string, ttl time.Duration) error }
Cacher is the interface for custom cache implementations. Implementations must distinguish between key not found and key found with empty value. `Get` must return nil for key not found. `Set` must cache empty values.
type MapCache ¶
type MapCache struct {
// contains filtered or unexported fields
}
MapCache is a simple cache implementation using a map.