cachekit

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2020 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	// DefaultMaxAge is default max-age value
	DefaultMaxAge = 30 * time.Second

	// HeaderCacheControl as in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
	HeaderCacheControl = "Cache-Control"

	// HeaderExpires as in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expires
	HeaderExpires = "Expires"

	// HeaderLastModified as in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified
	HeaderLastModified = "Last-Modified"

	// HeaderIfModifiedSince as in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since
	HeaderIfModifiedSince = "If-Modified-Since"
)

Variables

View Source
var (
	// ErrNotModified happen when conditional request apply
	ErrNotModified = errors.New("Cache: not modified")
)

Functions

func NotModifiedError

func NotModifiedError(err error) bool

NotModifiedError return true if error is not modified error

func SetHeader

func SetHeader(resp *echo.Response, pragma *Pragma)

SetHeader to set cache-related http header

Types

type Cache

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

Cache data

Example
var (
	data   string
	server *miniredis.Miniredis
	err    error
)

// run redis server
if server, err = miniredis.Run(); err != nil {
	log.Fatal(err.Error())
}
defer server.Close()

// create redis client
client := redis.NewClient(&redis.Options{Addr: server.Addr()})

// define key and refresh function for your cache
cache := cachekit.New("some-key", func() (interface{}, error) {
	return "fresh-data", nil
})

// execute cache to get the data
if err = cache.Execute(client, &data, pragmaWithCacheControl("")); err != nil {
	log.Fatal(err.Error())
}

fmt.Println(data)
Output:

fresh-data

func New

func New(key string, refreshFn RefreshFn) *Cache

New cache

func (*Cache) Execute

func (c *Cache) Execute(client *redis.Client, target interface{}, pragma *Pragma) (err error)

Execute cache to retreive data and save to target variable

type Pragma

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

Pragma handle pragmatic information/directives for caching

func CreatePragma

func CreatePragma(req *http.Request) *Pragma

CreatePragma to create new instance of CacheControl from request

func (*Pragma) IfModifiedSince

func (c *Pragma) IfModifiedSince() time.Time

IfModifiedSince return if-modified-since value

func (*Pragma) MaxAge

func (c *Pragma) MaxAge() time.Duration

MaxAge return max-age cache (in seconds)

func (*Pragma) NoCache

func (c *Pragma) NoCache() bool

NoCache return true if no cache is set

func (*Pragma) ResponseHeaders

func (c *Pragma) ResponseHeaders() map[string]string

ResponseHeaders return map that contain response header

func (*Pragma) SetExpiresByTTL

func (c *Pragma) SetExpiresByTTL(ttl time.Duration)

SetExpiresByTTL to set expires to current time to TTL

func (*Pragma) SetLastModified

func (c *Pragma) SetLastModified(lastModified time.Time)

SetLastModified to set last-modified

type RefreshFn

type RefreshFn func() (interface{}, error)

RefreshFn is function that retrieve refresh data

Jump to

Keyboard shortcuts

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