cache

package module
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 15 Imported by: 4

README

cache

GitHub Workflow Status Codecov GoDoc Sourcegraph

Package cache is a middleware that provides the cache management for Flamego.

Installation

The minimum requirement of Go is 1.18.

go get github.com/flamego/cache

Getting started

package main

import (
	"net/http"
	"time"

	"github.com/flamego/cache"
	"github.com/flamego/flamego"
)

func main() {
	f := flamego.Classic()
	f.Use(cache.Cacher())
	f.Get("/set", func(r *http.Request, cache cache.Cache) error {
		return cache.Set(r.Context(), "cooldown", true, time.Minute)
	})
	f.Get("/get", func(r *http.Request, cache cache.Cache) string {
		v, err := cache.Get(r.Context(), "cooldown")
		if err != nil && err != os.ErrNotExist {
			return err.Error()
		}

		cooldown, ok := v.(bool)
		if !ok || !cooldown {
			return "It has been cooled"
		}
		return "Still hot"
	})
	f.Run()
}

Getting help

License

This project is under the MIT License. See the LICENSE file for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cacher

func Cacher(opts ...Options) flamego.Handler

Cacher returns a middleware handler that injects cache.Cache into the request context, which is used for manipulating cache data.

func GobEncoder

func GobEncoder(v interface{}) ([]byte, error)

GobEncoder is a cache data encoder using Gob.

Types

type Cache

type Cache interface {
	// Get returns the value of given key in the cache. It returns os.ErrNotExist if
	// no such key exists or the key has expired.
	Get(ctx context.Context, key string) (interface{}, error)
	// Set sets the value of the key with given lifetime in the cache.
	Set(ctx context.Context, key string, value interface{}, lifetime time.Duration) error
	// Delete deletes a key from the cache.
	Delete(ctx context.Context, key string) error
	// Flush wipes out all existing data in the cache.
	Flush(ctx context.Context) error
	// GC performs a GC operation on the cache store.
	GC(ctx context.Context) error
}

Cache is a cache store with capabilities of setting, reading, deleting and GC cache data.

type Decoder

type Decoder func([]byte) (interface{}, error)

Decoder is a decoder to decode binary to cache data.

type Encoder

type Encoder func(interface{}) ([]byte, error)

Encoder is an encoder to encode cache data to binary.

type FileConfig

type FileConfig struct {

	// RootDir is the root directory of file cache items stored on the local file
	// system. Default is "cache".
	RootDir string
	// Encoder is the encoder to encode cache data. Default is a Gob encoder.
	Encoder Encoder
	// Decoder is the decoder to decode cache data. Default is a Gob decoder.
	Decoder Decoder
	// contains filtered or unexported fields
}

FileConfig contains options for the file cache store.

type Initer

type Initer func(ctx context.Context, args ...interface{}) (Cache, error)

Initer takes arbitrary number of arguments needed for initialization and returns an initialized cache store.

func FileIniter

func FileIniter() Initer

FileIniter returns the Initer for the file cache store.

func MemoryIniter

func MemoryIniter() Initer

MemoryIniter returns the Initer for the memory cache store.

type MemoryConfig

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

MemoryConfig contains options for the memory cache store.

type Options

type Options struct {
	// Initer is the initialization function of the cache store. Default is
	// cache.MemoryIniter.
	Initer Initer
	// Config is the configuration object to be passed to the Initer for the cache
	// store.
	Config interface{}
	// GCInterval is the time interval for GC operations. Default is 5 minutes.
	GCInterval time.Duration
	// ErrorFunc is the function used to print errors when something went wrong on
	// the background. Default is to drop errors silently.
	ErrorFunc func(err error)
}

Options contains options for the cache.Cacher middleware.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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