cacheman

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2020 License: MIT Imports: 9 Imported by: 6

README

CacheMan

Cache Middleware for Echo

Usage

	server := echo.New()

	store, e := cacheman.NewBigCache(&cfg.Cache)
	if e == nil {
		server.Use(cacheman.Middleware(&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
}

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 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
	// 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
	RouteCount        int
	ComparableRoutes  []*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

Jump to

Keyboard shortcuts

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