cache

package module
v0.0.0-...-b17b16b Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2015 License: MIT Imports: 14 Imported by: 0

README

gin-cache

Tiny and simple cache middleware for gin framework

Usage

package main

import (
  "time"

  "github.com/gin-gonic/gin"
  "github.com/olebedev/gin-cache"
)

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

  r.Use(cache.New(cache.Options{
    // set expire duration
    // by default zero, it means that cached content won't drop
    Expire: 5 * time.Minute,

    // store interface, see cache.go
    // by default it uses cache.InMemory
    Store: func() *cache.LevelDB {
      store, err := cache.NewLevelDB("cache")
      panicIf(err)
      return store
    }(),

    // it uses slice listed below as default to calculate
    // key, if `Header` slice is not specified
    Header: []string{
      "User-Agent",
      "Accept",
      "Accept-Encoding",
      "Accept-Language",
      "Cookie",
      "User-Agent",
    },

    // *gin.Context.Abort() will be invoked immediately after cache has been served
    // so, you can change this, but you should manage c.Writer.Written() flag by self
    // example:
    // func config(c *gin.Context) {
    //   if c.Writer.Written() {
    //      return
    //   }
    //   // else serve content
    //   ...
    // }
    DoNotUseAbort: false,
  }))

  r.Run(":3000")
}
TODO
  • inmemory store
  • leveldb store
  • cache_test.go
  • leveldb_test.go
  • redis store
  • memcache store
  • add CI tool

Documentation

Index

Constants

View Source
const KEY_PREFIX = "gin:cache:"

Variables

View Source
var (
	ErrNotFound      = errors.New("not found")
	ErrAlreadyExists = errors.New("already exists")
)

Functions

func New

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

Types

type Cache

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

func (*Cache) Get

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

func (*Cache) Set

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

func (*Cache) Update

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

type Cached

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

type InMemory

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

func NewInMemory

func NewInMemory() *InMemory

func (*InMemory) Get

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

func (*InMemory) Keys

func (im *InMemory) Keys() []string

func (*InMemory) Remove

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

func (*InMemory) Set

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

func (*InMemory) Update

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

type LevelDB

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

func NewLevelDB

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

func (*LevelDB) Get

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

func (*LevelDB) Keys

func (ldb *LevelDB) Keys() []string

func (*LevelDB) Remove

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

func (*LevelDB) Set

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

func (*LevelDB) Update

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

type Options

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

type Store

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

Jump to

Keyboard shortcuts

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