cache

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 17 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

echo-cache

Build Status PkgGoDev

A simple echo Cache middleware.

Requirements

  • Go 1.26 or newer
  • Echo v5

Install

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

Usage

Basic Usage
import (
    "github.com/sdvcrx/echo-cache"
)

// ...

e.Use(cache.Cache())
Custom Configuration
e.Use(cache.CacheWithConfig(cache.CacheConfig{
  // ...
}))

Configuration:

type CacheConfig struct {
    Skipper          middleware.Skipper
    CanCacheResponse middleware.Skipper
    CachePrefix      string
    CacheKey         CacheKeyFunc
    CacheDuration    time.Duration
    Store            store.Store
    Encoder          Encoder
    Metrics          Metrics
}

Echo v5 uses a concrete context type, so custom callbacks receive *echo.Context:

import "github.com/labstack/echo/v5"

e.Use(cache.CacheWithConfig(cache.CacheConfig{
  Skipper: func(c *echo.Context) bool {
    return c.Request().Header.Get("X-Skip-Cache") != ""
  },
}))

Migrating from Echo v4

This version supports Echo v5 only. Applications upgrading from Echo v4 must update their Echo imports to github.com/labstack/echo/v5 and change custom cache callback parameters from echo.Context to *echo.Context.

LICENSE

MIT

Documentation

Index

Constants

View Source
const (
	SizeKB int64 = 1024
	SizeMB int64 = 1024 * SizeKB
)

Variables

View Source
var (
	DefaultCachePrefix   = "cache"
	DefaultCacheDuration = time.Duration(0)
	DefaultCacheConfig   = CacheConfig{
		Skipper:          DefaultCacheSkipper,
		CanCacheResponse: DefaultCanCacheResponse,
		CachePrefix:      DefaultCachePrefix,
		CacheDuration:    DefaultCacheDuration,
		CacheKey:         DefaultCacheKey,
	}
)

Functions

func Cache

func Cache() echo.MiddlewareFunc

func CacheControlNoCache added in v1.0.0

func CacheControlNoCache(cacheControl string) bool

Retrurn true if cacheControl contain the `private`, `no-cache` or `no-store` directives.

func CacheWithConfig

func CacheWithConfig(config CacheConfig) echo.MiddlewareFunc

CacheWithConfig returns cache middleware with the given configuration.

func DefaultCacheKey

func DefaultCacheKey(prefix string, req *http.Request) string

func DefaultCacheSkipper

func DefaultCacheSkipper(c *echo.Context) bool

Cache default skipper only cache GET/HEAD method and headers not contain `Range` Based on https://vercel.com/docs/edge-cache#cacheable-response-criteria

func DefaultCanCacheResponse added in v1.0.0

func DefaultCanCacheResponse(c *echo.Context) bool

Default canCacheResponse will skip response cache if: - Response status code not in (200, 301, 308) - Response headers not contains `set-cookie` - Response does not exceed 10MB in content length - Response does not contain the `private`, `no-cache` or `no-store` directives in the Cache-Control header. Based on https://vercel.com/docs/edge-cache#cacheable-response-criteria

Types

type CacheConfig

type CacheConfig struct {
	Skipper          middleware.Skipper
	CanCacheResponse middleware.Skipper
	CachePrefix      string
	CacheKey         CacheKeyFunc
	CacheDuration    time.Duration
	Store            store.Store
	Encoder          Encoder
	Metrics          Metrics
}

func (CacheConfig) ToMiddleware added in v1.0.0

func (config CacheConfig) ToMiddleware() (echo.MiddlewareFunc, error)

ToMiddleware converts CacheConfig to middleware or returns an error for invalid configuration.

type CacheKeyFunc

type CacheKeyFunc func(prefix string, req *http.Request) string

type Encoder

type Encoder interface {
	Marshaler
	Unmarshaler
}

Interface that marshal/unmarshal `Response`

type JSONEncoder

type JSONEncoder struct{}

func (*JSONEncoder) Marshal

func (e *JSONEncoder) Marshal(r *Response) ([]byte, error)

func (*JSONEncoder) Unmarshal

func (e *JSONEncoder) Unmarshal(b []byte, v *Response) error

type Marshaler

type Marshaler interface {
	Marshal(r *Response) ([]byte, error)
}

type Metrics

type Metrics interface {
	// The total number of cache hits
	CacheHits()
	// The total number of cache misses
	CacheMisses()
	// The current size of the cache in bytes
	CacheSize(size float64)
	// The time it takes for the middleware to retrieve data from the cache
	CacheLatency(latency float64)
	// The total number of errors encountered while interacting with the cache
	CacheError()
}

type MsgpackEncoder

type MsgpackEncoder struct{}

func (*MsgpackEncoder) Marshal

func (m *MsgpackEncoder) Marshal(r *Response) ([]byte, error)

func (*MsgpackEncoder) Unmarshal

func (m *MsgpackEncoder) Unmarshal(b []byte, v *Response) error

type Response

type Response struct {
	StatusCode int         `msgpack:"status_code"`
	Headers    http.Header `msgpack:"headers,omitempty"`
	Body       []byte      `msgpack:"body,omitempty"`
}

func NewResponse

func NewResponse(code int, header http.Header, body []byte) *Response

type Unmarshaler

type Unmarshaler interface {
	Unmarshal(b []byte, v *Response) error
}

Directories

Path Synopsis
store module
bolt module
memory module
redis module
sql module

Jump to

Keyboard shortcuts

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