Coder Social home page Coder Social logo

dynamodb-cache's Introduction

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)

Related Projects

dynamodb-cache's People

Contributors

johanneskaufmann avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.