cache

package module
v0.0.0-...-d243502 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2018 License: MIT Imports: 7 Imported by: 5

README

dynamodb-cache

Cache with Memory and DynamoDB Adapters.

gopher with a hat that has wires going to a dynamodb database on the floor

Installation

go get github.com/JohannesKaufmann/dynamodb-cache

Usage

Create a DynamoDB table (for example called Cache) and pass the name to dynadapter.New as the second element.

type person struct {
  Name string
  Age  int
}
// - - - initialize - - - //
c, err := cache.New(
  memadapter.New(time.Hour, false),
  dynadapter.New(db, "Cache", time.Hour*24*7),
  // the order matters: items are saved (Set) and deleted (Del)
  // from to both but retrieved (Get) from the first adapter
  // (memadapter) first. If not found it tries the next
  // adapter (dynadapter).
)
if err != nil {
  log.Fatal(err)
}

// - - - set - - - //
john := person{
  Name: "John",
  Age:  19,
}
// set can be called with strings, ints, structs, maps, slices, ...
err = c.Set("1234", john)
if err != nil {
  log.Fatal(err)
}
fmt.Println("set person")

// - - - get - - - //
var p person
// remember to pass in a pointer
err = c.Get("1234", &p)
if err != nil {
  log.Fatal(err)
}
fmt.Printf("get person: %+v \n", p)

// - - - del - - - //
err = c.Del("123")
if err != nil {
  log.Fatal(err)
}

Get returns an error that is one of the following:

  • cache.ErrNotFound if the item was not found in ANY of the adapters.
  • cache.ErrExpired if the item was found but already expired (expired but not yet deleted). Remember that for DynamoDB it can take up to 48h for the deletion to happen.
  • other error (typically network error)

Middleware

r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(c.Middleware()) // cache middleware

r.Get("/", func(w http.ResponseWriter, r *http.Request) {
  w.Write([]byte("welcome"))
})
r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
  var name = "World"

  w.Write([]byte("Hello " + name))
})
http.ListenAndServe(":3000", r)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = errors.New("item not found")
	ErrExpired  = errors.New("item found but expired")
)

common errors

Cacheable contains the HTTP status codes are defined as cacheable. -> https://stackoverflow.com/a/39406969

Functions

This section is empty.

Types

type Adapter

type Adapter interface {
	Set(key string, value []byte) error
	Get(key string) ([]byte, error)
	Del(key string) error
}

type Cache

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

func New

func New(adapters ...InitAdapter) (*Cache, error)

New initializes a new cache with the adapters that are passed in.

func (*Cache) Del

func (c *Cache) Del(key string) error

Del deletes the item from the cache. The item is deleted from every adapter.

func (*Cache) Get

func (c *Cache) Get(key string, target interface{}) error

Get gets the item from the cache. It tries every adapter until it finds it.

func (*Cache) Middleware

func (c *Cache) Middleware() func(http.Handler) http.Handler

func (*Cache) Set

func (c *Cache) Set(key string, value interface{}) error

Set sets the value for that key in the cache.

type InitAdapter

type InitAdapter func() (Adapter, error)

Directories

Path Synopsis
examples
api

Jump to

Keyboard shortcuts

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