cache

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2021 License: MIT Imports: 14 Imported by: 1

README

nikel-cache

GoDoc Go Report Card

A simple and performant cache middleware for Gin based on olebedev's gin-cache.

Go version +1.13 required.

Available Database Backends
  • In-Memory
  • LevelDB (currently used by Nikel API)
  • BadgerDB
Usage
package main

import (
	"github.com/gin-gonic/gin"
	"github.com/nikel-api/nikel-cache"
)

func main() {
	r := gin.New()

	r.Use(cache.New(cache.Options{
		// set zero to make cache never expire
		Expire: 0,

		// set store
		Store: func() *cache.LevelDB {
			store, err := cache.NewLevelDB("cache")
			if err != nil {
				panic(err)
			}
			return store
		}(),

		// uses the header fields to calculate key
		Headers: []string{},

		// strips header fields
		StripHeaders: []string{},

		// bypass cache by response code
		BypassCodes: map[int]bool{},

		// *gin.Context.Abort() will be invoked immediately after cache has been served
		DoNotUseAbort: false,
	}))

	r.Run()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(o ...Options) gin.HandlerFunc

New cache

Types

type BadgerDB added in v1.1.0

type BadgerDB struct {
	DB *badger.DB
}

BadgerDB struct

func NewBadgerDB added in v1.1.0

func NewBadgerDB(path string) (*BadgerDB, error)

NewBadgerDB initializes BadgerDB database

func (*BadgerDB) Get added in v1.1.0

func (bdb *BadgerDB) Get(key string) ([]byte, error)

Get cache value

func (*BadgerDB) Remove added in v1.1.0

func (bdb *BadgerDB) Remove(key string) error

Remove cache value

func (*BadgerDB) Set added in v1.1.0

func (bdb *BadgerDB) Set(key string, value []byte) error

Set cache value

type Cache

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

Cache struct implements Store interface

func (*Cache) Get

func (c *Cache) Get(key string) (*Cached, error)

Get cache value

func (*Cache) Set

func (c *Cache) Set(key string, cch *Cached) error

Set cache value

type Cached

type Cached struct {
	Status   int
	Body     []byte
	Header   http.Header
	ExpireAt time.Time
}

Cached is a cached item

type InMemory

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

InMemory struct

func NewInMemory

func NewInMemory() *InMemory

NewInMemory initializes in-memory structure

func (*InMemory) Get

func (im *InMemory) Get(key string) ([]byte, error)

Get cache value

func (*InMemory) Remove

func (im *InMemory) Remove(key string) error

Remove cache value

func (*InMemory) Set

func (im *InMemory) Set(key string, value []byte) error

Set cache value

type LevelDB

type LevelDB struct {
	DB *leveldb.DB
}

LevelDB struct

func NewLevelDB

func NewLevelDB(path string) (*LevelDB, error)

NewLevelDB initializes LevelDB database

func (*LevelDB) Get

func (ldb *LevelDB) Get(key string) ([]byte, error)

Get cache value

func (*LevelDB) Remove

func (ldb *LevelDB) Remove(key string) error

Remove cache value

func (*LevelDB) Set

func (ldb *LevelDB) Set(key string, value []byte) error

Set cache value

type Options

type Options struct {
	Store         Store
	Expire        time.Duration
	Headers       []string
	StripHeaders  []string
	BypassCodes   map[int]bool
	DoNotUseAbort bool
}

Options for cache

type Store

type Store interface {
	Get(string) ([]byte, error)
	Set(string, []byte) error
	Remove(string) error
}

Store interface for filesystems to implement

Jump to

Keyboard shortcuts

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