cache

package
v2.0.0-...-1fe9aa2 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package cache: https://github.com/victorspringer/http-cache

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func KeyAsString

func KeyAsString(key uint64) string

KeyAsString can be used by adapters to convert the cache key from uint64 to string.

Types

type Adapter

type Adapter interface {
	// Get retrieves the cached response by a given key. It also
	// returns true or false, whether it exists or not.
	Get(ctx context.Context, key uint64) ([]byte, bool)

	// Set caches a response for a given key until an expiration date.
	Set(ctx context.Context, key uint64, response []byte, expiration time.Time)

	// Release frees cache for a given key.
	Release(ctx context.Context, key uint64)
}

Adapter interface for HTTP cache middleware client.

type Client

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

Client data structure for HTTP cache middleware.

func NewClient

func NewClient(opts ...ClientOption) (*Client, error)

NewClient initializes the cache HTTP middleware client with the given options.

func (*Client) Middleware

func (c *Client) Middleware(next http.Handler) http.Handler

Middleware is the HTTP cache middleware handler.

type ClientOption

type ClientOption func(c *Client) error

ClientOption is used to set Client settings.

func ClientWithAdapter

func ClientWithAdapter(a Adapter) ClientOption

ClientWithAdapter sets the adapter type for the HTTP cache middleware client.

func ClientWithExpiresHeader

func ClientWithExpiresHeader() ClientOption

ClientWithExpiresHeader enables middleware to add an Expires header to responses. Optional setting. If not set, default is false.

func ClientWithMethods

func ClientWithMethods(methods []string) ClientOption

ClientWithMethods sets the acceptable HTTP methods to be cached. Optional setting. If not set, default is "GET".

func ClientWithRefreshKey

func ClientWithRefreshKey(refreshKey string) ClientOption

ClientWithRefreshKey sets the parameter key used to free a request cached response. Optional setting.

func ClientWithTTL

func ClientWithTTL(ttl time.Duration) ClientOption

ClientWithTTL sets how long each response is going to be cached.

type Response

type Response struct {
	// Value is the cached response value.
	Value []byte

	// Header is the cached response header.
	Header http.Header

	// Expiration is the cached response expiration date.
	Expiration time.Time

	// LastAccess is the last date a cached response was accessed.
	// Used by LRU and MRU algorithms.
	LastAccess time.Time

	// Frequency is the count of times a cached response is accessed.
	// Used for LFU and MFU algorithms.
	Frequency int
}

Response is the cached response data structure.

func BytesToResponse

func BytesToResponse(b []byte) Response

BytesToResponse converts bytes array into Response data structure.

func (Response) Bytes

func (r Response) Bytes() []byte

Bytes converts Response data structure into bytes array.

Jump to

Keyboard shortcuts

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