package module
Version: v1.1.0 Latest Latest

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

Go to latest
Published: Dec 7, 2021 License: MIT Imports: 14 Imported by: 0



GitHub Workflow Status Codecov GoDoc Sourcegraph

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


The minimum requirement of Go is 1.16.

go get

Getting started

package main

import (


func main() {
	f := flamego.Classic()
	f.Get("/", func(cache cache.Cache) {
		_ = cache.Set("cooldown", true, time.Minute)
		cooldown, ok := cache.Get("cooldown").(bool)
		// ...

Getting help


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




This section is empty.


This section is empty.


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.


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.


Path Synopsis

Jump to

Keyboard shortcuts

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