cacheman

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2020 License: MIT Imports: 10 Imported by: 6

README

CacheMan

CacheMan was designed to be middleware for Echo for caching response from GET request for a period of time.

Usage (echo 3)

	server := echo.New()

	store, e := cacheman.NewBigCache(&cfg.Cache)
	if e == nil {
		server.Use(cacheman.Middleware(&cfg.Cache, store))
	}

Usage (echo 4)

	server := echo.New()

	store, e := cacheman.NewBigCache(&cfg.Cache)
	if e == nil {
		server.Use(cacheman.MiddlewareV4(&cfg.Cache, store))
	}

Cache support

  • BigCache

Custom cache

Just implement this interface and pass it into cacheman.Middleware.

type CacheInterface interface {
	Get(key string) ([]byte, error)
	Set(key string, value []byte) error
	Delete(key string) error
	Reset() error
}

Configuration

Enabled

Set to true to enable cacheman or false to disable. Default is false,

Verbose

Set to true to print out cacheman log or falst to make cacheman quiet. Default is false,

TTL

Cache entry life span in duration format. For example, 5m for 5 minutes, 1d for 1 day. Default is 1d.

Paths

Paths to be cached. Path with embedded variables like /user/:id is supported. Regular expression string is also supported, /.* to cache every path. Default is []string{},

ExcludedPaths

Paths to be excluded from cache. ExcludedPaths has higher priority than Paths. Default is []string{},

AdditionalHeaders

Custom headers added into returned cache. Default is map[string]string{},

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Middleware

func Middleware(config *Config, cache CacheInterface) echo.MiddlewareFunc

Middleware creates a middleware to handle cache

func MiddlewareV4 added in v0.0.5

func MiddlewareV4(config *Config, cache CacheInterface) echo4.MiddlewareFunc

MiddlewareV4 creates a middleware to handle cache for echo V4

func NewBigCache

func NewBigCache(config *Config) (*bigcache.BigCache, error)

NewBigCache creates big cache

Types

type CacheInterface

type CacheInterface interface {
	Get(key string) ([]byte, error)
	Set(key string, value []byte) error
	Delete(key string) error
	Reset() error
}

CacheInterface defines interface for cache

type Config

type Config struct {
	// Enabled to enable/disable cacheman
	Enabled bool
	// Verbose allow activities of cacheman to be display on console
	Verbose bool
	// TTL is age of cache entry in duration format, e.g. 1d for one day
	TTL string
	// Paths that will be cached
	Paths []string
	// ExcludedPaths are paths to be excluded from cache
	ExcludedPaths []string
	// AdditionalHeaders are injected in return cache
	AdditionalHeaders map[string]string
}

Config for cacheman

type Content

type Content struct {
	Status  int         `json:"status"`
	Headers http.Header `json:"headers"`
	Content string      `json:"content"`
}

Content is cached content

type Interceptor

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

Interceptor is response interceptor

func NewInterceptor

func NewInterceptor(writer http.ResponseWriter) *Interceptor

NewInterceptor creates a new response interceptor

func (*Interceptor) Content

func (c *Interceptor) Content() []byte

Content returns the captured content

func (*Interceptor) Header

func (c *Interceptor) Header() http.Header

Header returns response header

func (*Interceptor) Status

func (c *Interceptor) Status() int

Status returns the captured status

func (*Interceptor) Write

func (c *Interceptor) Write(b []byte) (int, error)

Write writes out the content. Automatically writes out the header if it has not been written out.

func (*Interceptor) WriteHeader

func (c *Interceptor) WriteHeader(statusCode int)

WriteHeader writes out the header with given status code

type Manager

type Manager struct {
	Enabled                  bool
	Verbose                  bool
	Cache                    CacheInterface
	Routes                   []string
	ExcludedRoutes           []string
	RouteCount               int
	ExcludedRouteCount       int
	ComparableRoutes         []*regexp.Regexp
	ComparableExcludedRoutes []*regexp.Regexp
	AdditionalHeaders        map[string]string
}

Manager is cache manager

func NewCacheManager

func NewCacheManager(conf *Config, cache CacheInterface) *Manager

NewCacheManager creates a cache manager

func (*Manager) Get

func (c *Manager) Get(path string) ([]byte, bool)

Get gets byte content from path key

func (*Manager) Log

func (c *Manager) Log(msg string)

Log prints log message

func (*Manager) Set

func (c *Manager) Set(path string, b []byte) error

Set sets byte content to path key

func (*Manager) TestPath

func (c *Manager) TestPath(path string) bool

TestPath return true if path matches a route, otherwise returns false

func (*Manager) TryWrite

func (c *Manager) TryWrite(ctx echo.Context) bool

TryWrite tries to write cached content if hit and return true, return false if miss

func (*Manager) TryWriteV4 added in v0.0.5

func (c *Manager) TryWriteV4(ctx echo4.Context) bool

TryWriteV4 tries to write cached content if hit and return true, return false if miss

Jump to

Keyboard shortcuts

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