cachecontrol-lite

module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2021 License: MIT

README

cachecontrol-lite

Simple middleware for Cache-Control headers on responses for gingonic and chi routers

Basic Usage
Import package
import "github.com/lggomez/cachecontrol-lite/middleware"
import "github.com/lggomez/cachecontrol-lite/middleware/cacheobject"

For chi and gingonic respectively there are specific packages:

import "github.com/lggomez/cachecontrol-lite/middleware/gin"
import "github.com/lggomez/cachecontrol-lite/middleware/chi"
gingonic - Adding the middleware on your router
func mapUrlsToControllers(router *gin.Engine) {
    /* ... */
    defaultCacheControlConfig := &cacheobject.ResponseCacheDirectives{MaxAge: cacheobject.DeltaSeconds((time.Hour * 24).Seconds())}

    router.GET("/foo/:id",
        controller.Get,
        middleware.AddCacheControl(defaultCacheControlConfig),
    )
    /* ... */
}
chi - Adding the middleware on your router

The middleware wraps an http.HandlerFunc, so you can use your controller (or driver, or whatever conforms to its interface) in the following way:

func mapUrlsToControllers(router *gin.Engine) {
    /* ... */
	r := chi.NewRouter()
	
    r.Get("/", WithCacheControl(controller.Handle, &cacheobject.ResponseCacheDirectives{
        MaxAge: cacheobject.DeltaSeconds((time.Hour * 24).Seconds()),
    }))
    /* ... */
}

Directories

Path Synopsis
chi
gin

Jump to

Keyboard shortcuts

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