cache

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2021 License: MIT Imports: 7 Imported by: 54

README

gin-cache

Release doc goreportcard for gin-cache

English | 🇨🇳中文

A high performance gin middleware to cache http response. Compared to gin-contrib/cache. It has a huge performance improvement.

Feature

  • Has a huge performance improvement compared to gin-contrib/cache.
  • Support cache response in local memory and redis.
  • Offer a way to custom the cache key of request.
  • Use singleflight to avoid hotspot invalid.

How To Use

Install

go get -u github.com/chenyahui/gin-cache

Example

Cache In Local Memory
package main

import (
	"time"

	"github.com/chenyahui/gin-cache"
	"github.com/chenyahui/gin-cache/persist"
	"github.com/gin-gonic/gin"
)

func main() {
	app := gin.New()

	memoryStore := persist.NewMemoryStore(1 * time.Minute)

	app.GET("/hello",
		cache.CacheByRequestURI(memoryStore, 2*time.Second),
		func(c *gin.Context) {
			c.String(200, "hello world")
		},
	)

	if err := app.Run(":8080"); err != nil {
		panic(err)
	}
}
Cache In Redis
package main

import (
	"time"

	"github.com/chenyahui/gin-cache"
	"github.com/chenyahui/gin-cache/persist"
	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis/v8"
)

func main() {
	app := gin.New()

	redisStore := persist.NewRedisStore(redis.NewClient(&redis.Options{
		Network: "tcp",
		Addr:    "127.0.0.1:6379",
	}))

	app.GET("/hello",
		cache.CacheByRequestURI(redisStore, 2*time.Second),
		func(c *gin.Context) {
			c.String(200, "hello world")
		},
	)
	if err := app.Run(":8080"); err != nil {
		panic(err)
	}
}

Benchmark

wrk -c 500 -d 1m -t 5 http://127.0.0.1:8080/hello

MemoryStore

MemoryStore QPS

RedisStore

RedisStore QPS

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 added in v1.0.0

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

CacheByRequestPath a shortcut function for caching response with url path, discard the query params

func CacheByRequestURI added in v1.0.0

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

CacheByRequestURI a shortcut function for caching response with uri

Types

type Config added in v1.0.0

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

type Discard added in v1.0.0

type Discard struct {
}

func (Discard) Errorf added in v1.0.0

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

type GetCacheStrategyByRequest added in v1.0.0

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

type Logger

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

type Option added in v1.0.0

type Option func(c *Config)

func WithCacheStrategyByRequest added in v1.0.0

func WithCacheStrategyByRequest(getGetCacheStrategyByRequest GetCacheStrategyByRequest) Option

func WithLogger added in v1.0.0

func WithLogger(l Logger) Option

type Strategy added in v1.0.0

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
examples
memory command
redis command

Jump to

Keyboard shortcuts

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