cache

package module
v0.0.0-...-bf1a16a Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2023 License: Apache-2.0 Imports: 10 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-echo-cache GoDoc Test

go-echo-cache, is a server-side HTTP cache middleware designed to work with Echo framework.

The in-memory cache is managed by freecache, a cache library with zero GC overhead and high concurrent performance.

Installation

The recommended way to install go-echo-cache is:

go get -u github.com/fraidev/go-echo-cache     

Examples

Basic example

A basic example that mimics the standard git clone command

package main

import (
	"net/http"
	"time"

	"github.com/coocood/freecache"
	cache "github.com/fraidev/go-echo-cache"
	"github.com/labstack/echo/v4"
)

func main() {
	c := freecache.NewCache(1024 * 1024) // Pre-allocated cache of 1Mb)

	e := echo.New()
	e.Use(cache.New(&cache.Config{}, c))
	e.GET("/", func(c echo.Context) error {
		c.String(http.StatusOK, time.Now().String())
		return nil
	})

	e.Start(":8080")
}

License

Apache License Version 2.0, see LICENSE

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(cfg *Config, cache Cache) echo.MiddlewareFunc

Types

type Cache

type Cache interface {
	Get(ctx context.Context, key []byte) (value []byte, err error)
	Set(ctx context.Context, key, value []byte, expireSeconds int) (err error)
}

Cache defines the interface for a cache.

type CacheEntry

type CacheEntry struct {
	Header     http.Header
	StatusCode int
	Body       []byte
}

func (*CacheEntry) Decode

func (c *CacheEntry) Decode(b []byte) error

func (*CacheEntry) Encode

func (c *CacheEntry) Encode() ([]byte, error)

func (*CacheEntry) Replay

func (c *CacheEntry) Replay(w http.ResponseWriter) error

type CacheMiddleware

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

func (*CacheMiddleware) Handler

func (m *CacheMiddleware) Handler(next echo.HandlerFunc) echo.HandlerFunc

type Config

type Config struct {
	// TTL time to life of the cache.
	TTL time.Duration `default:"1m"`
	// Methods methods to be cached.
	Methods []string `default:"[GET]"`
	// StatusCode method to be cached.
	StatusCode []int `default:"[200,404]"`
	// IgnoreQuery if true the Query values from the requests are ignored on
	// the key generation.
	IgnoreQuery bool
	// Refresh fuction called before use the cache, if true, the cache is deleted.
	Refresh func(r *http.Request) bool
	// Cache fuction called before cache a request, if false, the request is not
	// cached. If set Method is ignored.
	Cache func(r *http.Request) bool
	// GetKey function called to generate the key for the cache.
	GetKey func(r *http.Request) []byte
}

Config defiens the configuration for a cache middleware.

type MemoryCache

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

func NewMemoryCache

func NewMemoryCache(cache *freecache.Cache) MemoryCache

func (*MemoryCache) Get

func (c *MemoryCache) Get(ctx context.Context, key []byte) (value []byte, err error)

func (*MemoryCache) Set

func (c *MemoryCache) Set(ctx context.Context, key, value []byte, expireSeconds int) (err error)

type RedisCache

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

func NewRedisCache

func NewRedisCache(cache *redis.Client) RedisCache

func (*RedisCache) Get

func (c *RedisCache) Get(ctx context.Context, key []byte) (value []byte, err error)

func (*RedisCache) Set

func (c *RedisCache) Set(ctx context.Context, key, value []byte, expireSeconds int) (err error)

type ResponseRecorder

type ResponseRecorder struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

func NewResponseRecorder

func NewResponseRecorder(w http.ResponseWriter) *ResponseRecorder

func (*ResponseRecorder) Flush

func (w *ResponseRecorder) Flush()

func (*ResponseRecorder) Result

func (r *ResponseRecorder) Result() *CacheEntry

func (*ResponseRecorder) Write

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

func (*ResponseRecorder) WriteHeader

func (w *ResponseRecorder) WriteHeader(statusCode int)

Directories

Path Synopsis
_example
basic command

Jump to

Keyboard shortcuts

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