cache

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2024 License: MIT Imports: 14 Imported by: 0

README

echo-cache

Build Status PkgGoDev

A simple echo Cache middleware.

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
}

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: DefaultCanCacheResponseSkipper,
		CachePrefix:      DefaultCachePrefix,
		CacheDuration:    DefaultCacheDuration,
		CacheKey:         DefaultCacheKey,
	}
)

Functions

func Cache

func Cache() echo.MiddlewareFunc

func CacheWithConfig

func CacheWithConfig(config CacheConfig) echo.MiddlewareFunc

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`

func DefaultCanCacheResponseSkipper

func DefaultCanCacheResponseSkipper(c echo.Context) bool

Default canCacheResponse skipper will skip response cache if: - response status code not in (200, 301, 308) - response headers not contains `set-cookie`

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
}

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