httpcache

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2021 License: MIT Imports: 9 Imported by: 3

README

http-cache

Yet another useless caching middleware for Go.

Why?

First, this was part of the job test assessment. But I think that it shouldn't go to the trash bin and might be useful for someone. So, generally speaking - Just for fun. I got the job, BTW 😉

Getting Started

Installation

go get -u github.com/vokinneberg/http-cache

Usage
Generic Go middleware
    mux := http.NewServeMux()
    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "application/json")
        w.Write([]byte("{\"hello\": \"world\"}"))
    })

    handler := http_cache.NewDefault().Handler(mux)
    http.ListenAndServe(":8080", handler)
Negroni middleware
    mux := http.NewServeMux()

    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "application/json")
        w.Write([]byte("{\"hello\": \"world\"}"))
    })

    n := negroni.Classic()

    n.Use(http_cache.NewDefault())
    n.UseHandler(mux)
    n.Run(":8080")

Roadmap

  • Add Unit tests.
  • Add benchmarks - I really interested in how efficient this implementation is?
  • Make middleware RFC7234 complaint.
  • Add more data store adapters such as: Redis, memcached, DynamoDB, etc.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HttpCache

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

A HttpCache is a caching middleware.

func New

func New(options *Options) *HttpCache

New creates a new instance of `httpcache` middleware.

func NewDefault

func NewDefault() *HttpCache

NewDefault creates a new instance of `http-cache` middleware with default Options.

func (*HttpCache) Handler

func (c *HttpCache) Handler(h http.Handler) http.Handler

Handler adds caching on the request if its HTTP verb is supported.

func (*HttpCache) ServeHTTP

func (c *HttpCache) ServeHTTP(rw http.ResponseWriter, req *http.Request, next http.HandlerFunc)

ServeHTTP is a Negroni middleware compatible interface.

type Options

type Options struct {
	// AllowedVerbs is the list of HTTP Verbs that allowed for caching.
	// Supported cachable verbs: GET, HEAD, OPTIONS.
	// All supported verbs are allowed by http.
	AllowedVerbs []string

	// MaxAge is the maximum age in milliseconds response entry remains in cache.
	// Default value is 60000 milliseconds.
	MaxAge int64

	// Size is the initial capacity of cache.
	// Default value is 1000.
	Size int
}

Options a configuration container for `httpcache` middleware.

type ResponseRecorder

type ResponseRecorder interface {
	http.ResponseWriter
	Body() *bytes.Buffer
	Code() int
	Result() *http.Response
}

func NewResponseRecorder

func NewResponseRecorder() ResponseRecorder

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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