Categorygithub.com/hertz-contrib/cache
modulepackage
0.0.1
Repository: https://github.com/hertz-contrib/cache.git
Documentation: pkg.go.dev

# README

cache (This is a community driven project)

English | 中文

This is a middleware for hertz.

Hertz middleware for cache response management with multi-backend support:

This repo is forked from gin-cache and adapted for hertz.

Usage

Start using it

Download and install it:

go get github.com/hertz-contrib/cache

Import it in your code:

import "github.com/hertz-contrib/cache"

Basic Examples

memory

package main

import (
    "context"
    "fmt"
    "net/http"
    "sync/atomic"
    "time"

    "github.com/cloudwego/hertz/pkg/app"
    "github.com/cloudwego/hertz/pkg/app/server"
    "github.com/hertz-contrib/cache"
    "github.com/hertz-contrib/cache/persist"
)

func main() {
    h := server.New()

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

    var cacheHitCount, cacheMissCount int32

    h.Use(cache.NewCacheByRequestURI(
        memoryStore,
        2*time.Second,
        cache.WithOnHitCache(func(ctx context.Context, c *app.RequestContext) {
            atomic.AddInt32(&cacheHitCount, 1)
        }),
        cache.WithOnMissCache(func(ctx context.Context, c *app.RequestContext) {
            atomic.AddInt32(&cacheMissCount, 1)
        }),
    ))
    h.GET("/hello", func(ctx context.Context, c *app.RequestContext) {
        c.String(http.StatusOK, "hello world")
    })
    h.GET("/get_hit_count", func(ctx context.Context, c *app.RequestContext) {
        c.String(200, fmt.Sprintf("total hit count: %d", cacheHitCount))
    })
    h.GET("/get_miss_count", func(ctx context.Context, c *app.RequestContext) {
        c.String(200, fmt.Sprintf("total miss count: %d", cacheMissCount))
    })

    h.Spin()
}

redis

package main

import (
    "context"
    "net/http"
    "time"

    "github.com/cloudwego/hertz/pkg/app"
    "github.com/cloudwego/hertz/pkg/app/server"
    "github.com/go-redis/redis/v8"
    "github.com/hertz-contrib/cache"
    "github.com/hertz-contrib/cache/persist"
)

func main() {
    h := server.New()

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

    h.Use(cache.NewCacheByRequestURI(redisStore, 2*time.Second))
    h.GET("/hello", func(ctx context.Context, c *app.RequestContext) {
        c.String(http.StatusOK, "hello world")
    })
    h.Spin()
}

License

This project is under Apache License. See the LICENSE-APACHE file for the full license text.

# Packages

No description provided by the author
No description provided by the author

# Functions

NewCache user must pass getCacheKey to describe the way to generate cache key.
NewCacheByRequestPath a shortcut function for caching response by url path, means will discard the query params.
NewCacheByRequestURI a shortcut function for caching response by uri.
NewCacheByRequestURIWithIgnoreQueryOrder a shortcut function for caching response by uri and ignore query param order.
WithBeforeReplyWithCache will be called before replying with cache.
WithCacheStrategyByRequest set up the custom strategy by per request.
WithOnHitCache will be called when cache hit.
WithOnMissCache will be called when cache miss.
WithOnShareSingleFlight will be called when share the singleflight result.
No description provided by the author
WithPrefixKey will prefix the key.
WithSingleFlightForgetTimeout to reduce the impact of long tail requests.

# Structs

Option represents the optional function.
Options contains all options.
ResponseCache record the http response cache.
Strategy the cache strategy.

# Type aliases

No description provided by the author
GetCacheStrategyByRequest User can use this function to design custom cache strategy by request.
OnHitCacheCallback define the callback when use cache.
OnMissCacheCallback define the callback when use cache.
OnShareSingleFlightCallback define the callback when share the singleFlight result.