cache

package
v0.1.78 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2020 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Example (InMemory)
package main

import (
	"context"
	"errors"
	"fmt"
	"time"

	"github.com/pace/bricks/pkg/cache"
)

func main() {
	ctx := context.Background()

	// init cache
	var c cache.Cache = cache.InMemory()

	// write to cache
	if err := c.Put(ctx, "foo", []byte("bar"), time.Hour); err != nil {
		panic(err)
	}

	// get from cache and print
	v, _, err := c.Get(ctx, "foo")
	if err != nil {
		panic(err)
	}
	fmt.Println(string(v))

	// forget
	if err := c.Forget(ctx, "foo"); err != nil {
		panic(err)
	}

	// get from cache and print
	_, _, err = c.Get(ctx, "foo")
	if errors.Is(err, cache.ErrNotFound) {
		fmt.Println(err)
	} else {
		panic("expected error not found")
	}

}
Output:

bar
key "foo": not found

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// The value under the given key was not found.
	ErrNotFound = errors.New("not found")

	// The caching backend produced an error that is not reflected by any other
	// error.
	ErrBackend = errors.New("cache backend error")
)

Package errors.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Put stores the value under the key. Any existing value is overwritten. If
	// ttl is given, the cache automatically forgets the value after the
	// duration. If ttl is zero then it is never automatically forgotten.
	Put(ctx context.Context, key string, value []byte, ttl time.Duration) error

	// Get returns the value stored under the key and its remaining ttl. If
	// there is no value stored, ErrNotFound is returned. If the ttl is zero,
	// the value does not automatically expire. Unless an error is returned, the
	// value is always non-nil.
	Get(ctx context.Context, key string) (value []byte, ttl time.Duration, _ error)

	// Forget removes the value stored under the key. No error is returned if
	// there is no value stored.
	Forget(ctx context.Context, key string) error
}

Cache is a common abstraction to cache bytes. It is safe for concurrent use.

type Memory

type Memory struct {
	// contains filtered or unexported fields
}

Memory is the cache that stores everything in memory. It is safe for concurrent use.

func InMemory

func InMemory() *Memory

InMemory returns a new in-memory cache.

func (*Memory) Forget

func (c *Memory) Forget(_ context.Context, key string) error

Forget removes the value stored under the key. No error is returned if there is no value stored.

func (*Memory) Get

func (c *Memory) Get(ctx context.Context, key string) ([]byte, time.Duration, error)

Get returns the value stored under the key and its remaining ttl. If there is no value stored, ErrNotFound is returned. If the ttl is zero, the value does not automatically expire. Unless an error is returned, the value is always non-nil.

func (*Memory) Put

func (c *Memory) Put(_ context.Context, key string, value []byte, ttl time.Duration) error

Put stores the value under the key. Any existing value is overwritten. If ttl is given, the cache automatically forgets the value after the duration. If ttl is zero then it is never automatically forgotten.

type Redis

type Redis struct {
	// contains filtered or unexported fields
}

Redis is the cache that uses a redis backend. It is safe for concurrent use.

func InRedis

func InRedis(client *redis.Client, prefix string) *Redis

InRedis returns a new cache that connects to redis using the given client. The prefix is used for every key that is stored.

func (*Redis) Forget

func (c *Redis) Forget(ctx context.Context, key string) error

Forget removes the value stored under the key. No error is returned if there is no value stored.

func (*Redis) Get

func (c *Redis) Get(ctx context.Context, key string) ([]byte, time.Duration, error)

Get returns the value stored under the key and its remaining ttl. If there is no value stored, ErrNotFound is returned. If the ttl is zero, the value does not automatically expire. Unless an error is returned, the value is always non-nil.

func (*Redis) Put

func (c *Redis) Put(ctx context.Context, key string, value []byte, ttl time.Duration) error

Put stores the value under the key. Any existing value is overwritten. If ttl is given, the cache automatically forgets the value after the duration. If ttl is zero then it is never automatically forgotten.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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