cache

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 21, 2021 License: Apache-2.0 Imports: 8 Imported by: 1

README

go-echo-cache GoDoc Test

go-echo-cache, is a server-side HTTP cache middleware designed to work with Echo framework.

The in-memory cache is managed by freecache, a cache library with zero GC overhead and high concurrent performance.

Installation

The recommended way to install go-echo-cache is:

go get -u github.com/gitsight/go-echo-cache     

Examples

Basic example

A basic example that mimics the standard git clone command

package main

import (
	"net/http"
	"time"

	"github.com/coocood/freecache"
	cache "github.com/gitsight/go-echo-cache"
	"github.com/labstack/echo/v4"
)

func main() {
	c := freecache.NewCache(1024 * 1024) // Pre-allocated cache of 1Mb)

	e := echo.New()
	e.Use(cache.New(&cache.Config{}, c))
	e.GET("/", func(c echo.Context) error {
		c.String(http.StatusOK, time.Now().String())
		return nil
	})

	e.Start(":8080")
}

License

Apache License Version 2.0, see LICENSE

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(cfg *Config, cache *freecache.Cache) echo.MiddlewareFunc

Types

type CacheEntry added in v1.0.1

type CacheEntry struct {
	Header     http.Header
	StatusCode int
	Body       []byte
}

func (*CacheEntry) Decode added in v1.0.1

func (c *CacheEntry) Decode(b []byte) error

func (*CacheEntry) Encode added in v1.0.1

func (c *CacheEntry) Encode() ([]byte, error)

func (*CacheEntry) Replay added in v1.0.1

func (c *CacheEntry) Replay(w http.ResponseWriter) error

type CacheMiddleware

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

func (*CacheMiddleware) Handler

func (m *CacheMiddleware) Handler(next echo.HandlerFunc) echo.HandlerFunc

type Config

type Config struct {
	// TTL time to life of the cache.
	TTL time.Duration `default:"1m"`
	// Methods methods to be cached.
	Methods []string `default:"[GET]"`
	// StatusCode method to be cached.
	StatusCode []int `default:"[200,404]"`
	// IgnoreQuery if true the Query values from the requests are ignored on
	// the key generation.
	IgnoreQuery bool
	// Refresh fuction called before use the cache, if true, the cache is deleted.
	Refresh func(r *http.Request) bool
	// Cache fuction called before cache a request, if false, the request is not
	// cached. If set Method is ignored.
	Cache func(r *http.Request) bool
}

Config defiens the configuration for a cache middleware.

type ResponseRecorder

type ResponseRecorder struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

func NewResponseRecorder

func NewResponseRecorder(w http.ResponseWriter) *ResponseRecorder

func (*ResponseRecorder) Result

func (r *ResponseRecorder) Result() *CacheEntry

func (*ResponseRecorder) Write

func (w *ResponseRecorder) Write(b []byte) (int, error)

func (*ResponseRecorder) WriteHeader

func (w *ResponseRecorder) WriteHeader(statusCode int)

Directories

Path Synopsis
_example

Jump to

Keyboard shortcuts

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