cache

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var PageCachePrefix = "gincache.page.cache:"

PageCachePrefix default page cache key prefix

Functions

func Cache

func Cache(store persist.Store, expire time.Duration, opts ...Option) gin.HandlerFunc

Cache user must pass store and store expiration time to cache and with custom option. default caching response with uri, which use PageCachePrefix

func GenerateKeyWithPrefix

func GenerateKeyWithPrefix(prefix, key string) string

GenerateKeyWithPrefix generate key with GenerateKeyWithPrefix and u, if key is larger than 200,it will use sha1.Sum key like: prefix+u or prefix+sha1(u)

func GenerateRequestPath

func GenerateRequestPath(c *gin.Context) (string, bool)

GenerateRequestPath generate key with PageCachePrefix and request Path

func GenerateRequestUri

func GenerateRequestUri(c *gin.Context) (string, bool)

GenerateRequestUri generate key with PageCachePrefix and request uri

Types

type BodyCache

type BodyCache struct {
	Status int
	Header http.Header
	Data   []byte
	// contains filtered or unexported fields
}

BodyCache body cache store

func (*BodyCache) MarshalBinary

func (b *BodyCache) MarshalBinary() ([]byte, error)

func (*BodyCache) UnmarshalBinary

func (b *BodyCache) UnmarshalBinary(data []byte) error

type BodyWriter

type BodyWriter struct {
	gin.ResponseWriter
	// contains filtered or unexported fields
}

BodyWriter dup response writer body

func (*BodyWriter) Write

func (w *BodyWriter) Write(b []byte) (int, error)

Write writes the data to the connection as part of an HTTP reply.

func (*BodyWriter) WriteString

func (w *BodyWriter) WriteString(s string) (int, error)

WriteString the string into the response body.

type Config

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

Config for cache

type Discard

type Discard struct{}

Discard is an logger on which all Write calls succeed without doing anything.

func NewDiscard

func NewDiscard() Discard

NewDiscard a discard logger on which always succeed without doing anything

func (Discard) Errorf

func (d Discard) Errorf(context.Context, string, ...any)

Errorf implement Logger interface.

type Encoding

type Encoding interface {
	Marshal(v any) ([]byte, error)
	Unmarshal(data []byte, v any) error
}

Encoding interface

type JSONEncoding

type JSONEncoding struct{}

func (JSONEncoding) Marshal

func (JSONEncoding) Marshal(v any) ([]byte, error)

func (JSONEncoding) Unmarshal

func (JSONEncoding) Unmarshal(data []byte, v any) error

type JSONGzipEncoding

type JSONGzipEncoding struct{}

func (JSONGzipEncoding) Marshal

func (JSONGzipEncoding) Marshal(v any) ([]byte, error)

func (JSONGzipEncoding) Unmarshal

func (JSONGzipEncoding) Unmarshal(data []byte, v any) error

type Logger

type Logger interface {
	Errorf(ctx context.Context, format string, args ...any)
}

Logger logger interface

type Option

type Option func(c *Config)

Option custom option

func WithEncoding

func WithEncoding(encode Encoding) Option

WithEncoding custom Encoding, default is JSONEncoding.

func WithGenerateKey

func WithGenerateKey(f func(c *gin.Context) (string, bool)) Option

WithGenerateKey custom generate key ,default is GenerateRequestURIKey.

func WithLogger

func WithLogger(l Logger) Option

WithLogger custom logger, default is Discard.

func WithRandDuration

func WithRandDuration(rand func() time.Duration) Option

WithRandDuration custom rand duration for expire, default return zero expiration time always expire + rand()

func WithSingleflight

func WithSingleflight(group *singleflight.Group) Option

WithSingleflight custom single flight group, default is private single flight group.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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