cached

package module
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 4 Imported by: 0

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.

func NewMapCache

func NewMapCache() *MapCache

NewMapCache returns a new MapCache.

func (*MapCache) Get

func (c *MapCache) Get(key string) (*string, error)

Get returns the value for the given key, if cached. If the value is not cached, it returns nil.

func (*MapCache) Set

func (c *MapCache) Set(key, value string, ttl time.Duration) error

Set sets the value for the given key.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option configures a cached loader.

var DisableEmptyValueHit Option = emptyValHit(false)

DisableEmptyValueHit disables caching of empty values.

func Cache

func Cache(c Cacher) Option

Cache sets the cache implementation for the loader.

type TTL

type TTL time.Duration

TTL sets the TTL for the cached keys.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL