gin_cache

package
v0.0.0-...-265ede9 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cache

func Cache(
	defaultCacheStore persist.CacheStore,
	defaultExpire time.Duration,
	opts ...Option,
) gin.HandlerFunc

Cache user must pass getCacheKey to describe the way to generate cache key

func CacheByRequestPath

func CacheByRequestPath(defaultCacheStore persist.CacheStore, defaultExpire time.Duration, opts ...Option) gin.HandlerFunc

CacheByRequestPath a shortcut function for caching response by url path, means will discard the query params

func CacheByRequestURI

func CacheByRequestURI(defaultCacheStore persist.CacheStore, defaultExpire time.Duration, opts ...Option) gin.HandlerFunc

CacheByRequestURI a shortcut function for caching response by uri

func CorsHeaders

func CorsHeaders() []string

Types

type BeforeReplyWithCacheCallback

type BeforeReplyWithCacheCallback func(c *gin.Context, cache *ResponseCache)

type Config

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

Config contains all options

type Discard

type Discard struct {
}

Discard the default logger that will discard all logs of gin-cache

func (Discard) Errorf

func (l Discard) Errorf(string, ...interface{})

Errorf will output the log at error level

type GetCacheStrategyByRequest

type GetCacheStrategyByRequest func(c *gin.Context) (bool, Strategy)

GetCacheStrategyByRequest User can this function to design custom cache strategy by request. The first return value bool means whether this request should be cached. The second return value Strategy determine the special strategy by this request.

type Logger

type Logger interface {
	Errorf(string, ...interface{})
}

Logger define the logger interface

type OnHitCacheCallback

type OnHitCacheCallback func(c *gin.Context)

OnHitCacheCallback define the callback when use cache

type OnMissCacheCallback

type OnMissCacheCallback func(c *gin.Context)

OnMissCacheCallback define the callback when use cache

type OnShareSingleFlightCallback

type OnShareSingleFlightCallback func(c *gin.Context)

OnShareSingleFlightCallback define the callback when share the singleflight result

type Option

type Option func(c *Config)

Option represents the optional function.

func IgnoreQueryOrder

func IgnoreQueryOrder() Option

IgnoreQueryOrder will ignore the queries order in url when generate cache key . This option only takes effect in CacheByRequestURI function

func WithBeforeReplyWithCache

func WithBeforeReplyWithCache(cb BeforeReplyWithCacheCallback) Option

WithBeforeReplyWithCache will be called before replying with cache.

func WithCacheStrategyByRequest

func WithCacheStrategyByRequest(getGetCacheStrategyByRequest GetCacheStrategyByRequest) Option

WithCacheStrategyByRequest set up the custom strategy by per request

func WithDiscardHeaders

func WithDiscardHeaders(headers []string) Option

func WithLogger

func WithLogger(l Logger) Option

WithLogger set the custom logger

func WithOnHitCache

func WithOnHitCache(cb OnHitCacheCallback) Option

WithOnHitCache will be called when cache hit.

func WithOnMissCache

func WithOnMissCache(cb OnMissCacheCallback) Option

WithOnMissCache will be called when cache miss.

func WithOnShareSingleFlight

func WithOnShareSingleFlight(cb OnShareSingleFlightCallback) Option

WithOnShareSingleFlight will be called when share the singleflight result

func WithPrefixKey

func WithPrefixKey(prefix string) Option

WithPrefixKey will prefix the key

func WithSingleFlightForgetTimeout

func WithSingleFlightForgetTimeout(forgetTimeout time.Duration) Option

WithSingleFlightForgetTimeout to reduce the impact of long tail requests. singleflight.Forget will be called after the timeout has reached for each backend request when timeout is greater than zero.

func WithoutHeader

func WithoutHeader() Option

type ResponseCache

type ResponseCache struct {
	Status int
	Header http.Header
	Data   []byte
}

ResponseCache record the http response cache

type Strategy

type Strategy struct {
	CacheKey string

	// CacheStore if nil, use default cache store instead
	CacheStore persist.CacheStore

	// CacheDuration
	CacheDuration time.Duration
}

Strategy the cache strategy

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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