cache

package module
v0.0.0-...-6d49512 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2024 License: MIT Imports: 19 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
    Adapter          CacheAdapter
    Encoder          Encoder
}

LICENSE

MIT

Documentation

Index

Constants

View Source
const (
	SizeKB int64 = 1024
	SizeMB int64 = 1024 * SizeKB
)
View Source
const (
	SQLite dbName = iota
	PostgreSQL
	MySQL
)

Variables

View Source
var (
	DefaultCachePrefix   = "cache"
	DefaultCacheDuration = time.Duration(0)
	DefaultCacheConfig   = CacheConfig{
		Skipper:          DefaultCacheSkipper,
		CanCacheResponse: DefaultCanCacheResponseSkipper,
		CachePrefix:      DefaultCachePrefix,
		CacheDuration:    DefaultCacheDuration,
		CacheKey:         DefaultCacheKey,
	}
)
View Source
var DefaultSQLAdapterOption = SQLAdapterOption{
	Ctx:       context.Background(),
	TableName: "echo_cache",
	DBName:    SQLite,
}

Functions

func Cache

func Cache() echo.MiddlewareFunc

func CacheWithConfig

func CacheWithConfig(config CacheConfig) echo.MiddlewareFunc

func CopyMap

func CopyMap[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2)

Copy from std lib "maps"

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 BoltAdapter

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

func NewBoltAdapter

func NewBoltAdapter(ctx context.Context, path string) *BoltAdapter

func (*BoltAdapter) Get

func (ba *BoltAdapter) Get(key string) ([]byte, error)

func (*BoltAdapter) Set

func (ba *BoltAdapter) Set(key string, val []byte, ttl time.Duration) error

type CacheAdapter

type CacheAdapter interface {
	Get(key string) ([]byte, error)
	Set(key string, val []byte, ttl time.Duration) error
}

func NewMemoryAdapter

func NewMemoryAdapter(size int, memoryType MemoryType) CacheAdapter

func NewRedisAdapter

func NewRedisAdapter(opt *redis.Options) CacheAdapter

func NewSQLAdapter

func NewSQLAdapter(option SQLAdapterOption) CacheAdapter

type CacheConfig

type CacheConfig struct {
	Skipper          middleware.Skipper
	CanCacheResponse middleware.Skipper
	CachePrefix      string
	CacheKey         CacheKeyFunc
	CacheDuration    time.Duration
	Adapter          CacheAdapter
	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 MemoryAdapter

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

func (*MemoryAdapter) Get

func (ma *MemoryAdapter) Get(key string) ([]byte, error)

func (*MemoryAdapter) Set

func (ma *MemoryAdapter) Set(key string, val []byte, ttl time.Duration) error

type MemoryType

type MemoryType int
const (
	TYPE_SIMPLE MemoryType = iota + 1
	TYPE_LRU
	TYPE_LFU
	TYPE_ARC
)

func (MemoryType) String

func (m MemoryType) String() string

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 RedisAdapter

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

func (*RedisAdapter) Get

func (ra *RedisAdapter) Get(key string) ([]byte, error)

func (*RedisAdapter) Set

func (ra *RedisAdapter) Set(key string, val []byte, ttl time.Duration) 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 SQLAdapter

type SQLAdapter struct {
	SQLAdapterOption
	// contains filtered or unexported fields
}

func (*SQLAdapter) Get

func (sa *SQLAdapter) Get(key string) ([]byte, error)

func (*SQLAdapter) Set

func (sa *SQLAdapter) Set(key string, val []byte, ttl time.Duration) error

type SQLAdapterOption

type SQLAdapterOption struct {
	DB        *sql.DB
	Ctx       context.Context
	TableName string
	DBName    dbName
}

type Unmarshaler

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

Jump to

Keyboard shortcuts

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