cacheman

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2022 License: MIT Imports: 13 Imported by: 0

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))
	}

Working example

cacheman-example

Cache support

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
	Type() string
}

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{},

CacheInfoPath

URI to request cacheman information. Send GET request to this path to see cacheman information. Make it empty to disable it. Default is <empty>.

PurgePath

URI to purge cache. Send PURGE request to this path to empty cache. Make it empty to disable it. Default is <empty>.

License

MIT

Documentation

Index

Constants

View Source
const (
	// HealthCheckUntested tells that operation has not been tested
	HealthCheckUntested string = "untested"
	// HealthCheckFailed tells that operation has failed the test
	HealthCheckFailed string = "failed"
	// HealthCheckPassed tells that operation has passed the test
	HealthCheckPassed string = "passed"
)

Variables

This section is empty.

Functions

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

Types

type BigCacheClient added in v0.0.6

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

func NewBigCache

func NewBigCache(config *Config) (*BigCacheClient, error)

NewBigCache creates big cache client

func (*BigCacheClient) Delete added in v0.0.6

func (c *BigCacheClient) Delete(key string) error

func (*BigCacheClient) Get added in v0.0.6

func (c *BigCacheClient) Get(key string) ([]byte, error)

func (*BigCacheClient) Reset added in v0.0.6

func (c *BigCacheClient) Reset() error

func (*BigCacheClient) Set added in v0.0.6

func (c *BigCacheClient) Set(key string, value []byte) error

func (*BigCacheClient) Type added in v0.0.8

func (c *BigCacheClient) Type() string

type CacheInterface

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

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
	// Server is cache server in host:port format
	Server string
	// Password is credential for accessing cache service
	Password string
	// Database is database name or index
	Database interface{}
	// CacheInfoPath is URI to request cache information
	CacheInfoPath string
	// PurgePath is URI to purge all content in cache
	PurgePath string
	// Namespace to be automatically added into cache key
	Namespace 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
	Namespace                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) Purge added in v0.0.10

func (c *Manager) Purge() error

Purge all content in cache

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) 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

func (*Manager) WriteInfoV4 added in v0.0.9

func (c *Manager) WriteInfoV4(ctx echo4.Context)

WriteInfoV4 print cacheman information out to client

type MemcachedClient added in v0.0.7

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

func NewMemcached added in v0.0.7

func NewMemcached(config *Config) (*MemcachedClient, error)

NewMemcached creates big cache client

func (*MemcachedClient) Delete added in v0.0.7

func (c *MemcachedClient) Delete(key string) error

func (*MemcachedClient) Get added in v0.0.7

func (c *MemcachedClient) Get(key string) ([]byte, error)

func (*MemcachedClient) Reset added in v0.0.7

func (c *MemcachedClient) Reset() error

func (*MemcachedClient) Set added in v0.0.7

func (c *MemcachedClient) Set(key string, value []byte) error

func (*MemcachedClient) Type added in v0.0.8

func (c *MemcachedClient) Type() string

type RedisClient added in v0.0.6

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

func NewRedis added in v0.0.6

func NewRedis(config *Config) (*RedisClient, error)

NewRedis creates big cache client

func (*RedisClient) Delete added in v0.0.6

func (c *RedisClient) Delete(key string) error

func (*RedisClient) Get added in v0.0.6

func (c *RedisClient) Get(key string) ([]byte, error)

func (*RedisClient) Reset added in v0.0.6

func (c *RedisClient) Reset() error

func (*RedisClient) Set added in v0.0.6

func (c *RedisClient) Set(key string, value []byte) error

func (*RedisClient) Type added in v0.0.8

func (c *RedisClient) Type() string

Jump to

Keyboard shortcuts

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