cache

package module
Version: v0.0.0-...-f5e2442 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2020 License: Apache-2.0 Imports: 14 Imported by: 2

README

cache

Build Status

Middleware cache provides cache management for Tango. It can use many cache adapters, including memory, file, Redis, Memcache, PostgreSQL, MySQL, Ledis and Nodb.

Installation
go get gitea.com/tango/cache

Getting Help

More adapters

Default cache includes two adapters memory and file, you can find more adapters.

Credits

This package is a modified version of macaron/cache.

License

This project is under the Apache License, Version 2.0. See the LICENSE file for the full license text.

Documentation

Overview

Package cache is a middleware that provides the cache management of Tango.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeGob

func DecodeGob(data []byte, out *Item) error

func Decr

func Decr(val interface{}) (interface{}, error)

func EncodeGob

func EncodeGob(item *Item) ([]byte, error)

func Incr

func Incr(val interface{}) (interface{}, error)

func Register

func Register(name string, adapter Adapter)

Register registers a adapter.

Types

type Adapter

type Adapter interface {
	// Put puts value into cache with key and expire time.
	Put(key string, val interface{}, timeout int64) error
	// Get gets cached value by given key.
	Get(key string) interface{}
	// Delete deletes cached value by given key.
	Delete(key string) error
	// Incr increases cached int-type value by given key as a counter.
	Incr(key string) error
	// Decr decreases cached int-type value by given key as a counter.
	Decr(key string) error
	// IsExist returns true if cached value exists.
	IsExist(key string) bool
	// Flush deletes all cached data.
	Flush() error
	// StartAndGC starts GC routine based on config string settings.
	StartAndGC(opt Options) error
}

Adapter is the interface that operates the cache data.

func NewAdapter

func NewAdapter(name string, opt Options) (Adapter, error)

NewAdapter creates and returns a new cacheAdapter by given adapter name and configuration. It panics when given adapter isn't registered and starts GC automatically.

type Cache

type Cache struct {
	*Caches
}

Cache maintains cache adapter and tango handler interface

func (*Cache) SetCaches

func (c *Cache) SetCaches(cs *Caches)

set Caches

type Cacher

type Cacher interface {
	SetCaches(*Caches)
}

Cacher provides tango action handler to get proper handler

type Caches

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

Caches

func New

func New(options ...Options) *Caches

New is a middleware that maps a cache.Cache service into the tango handler chain. An single variadic cache.Options struct can be optionally provided to configure.

func (*Caches) Decr

func (c *Caches) Decr(key string) error

Decr decreases cached int-type value by given key as a counter.

func (*Caches) Delete

func (c *Caches) Delete(key string) error

Delete deletes cached value by given key.

func (*Caches) Flush

func (c *Caches) Flush() error

Flush deletes all cached data.

func (*Caches) Get

func (c *Caches) Get(key string) interface{}

Get gets cached value by given key.

func (*Caches) Handle

func (c *Caches) Handle(ctx *tango.Context)

Handle implement tango.Handle

func (*Caches) Incr

func (c *Caches) Incr(key string) error

Incr increases cached int-type value by given key as a counter.

func (*Caches) IsExist

func (c *Caches) IsExist(key string) bool

IsExist returns true if cached value exists.

func (*Caches) Option

func (c *Caches) Option() Options

Options return cache option.

func (*Caches) Put

func (c *Caches) Put(key string, val interface{}, timeout int64) error

Put puts value into cache with key and expire time.

type FileCacher

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

FileCacher represents a file cache adapter implementation.

func NewFileCacher

func NewFileCacher() *FileCacher

NewFileCacher creates and returns a new file cacher.

func (*FileCacher) Decr

func (c *FileCacher) Decr(key string) error

Decrease cached int value.

func (*FileCacher) Delete

func (c *FileCacher) Delete(key string) error

Delete deletes cached value by given key.

func (*FileCacher) Flush

func (c *FileCacher) Flush() error

Flush deletes all cached data.

func (*FileCacher) Get

func (c *FileCacher) Get(key string) interface{}

Get gets cached value by given key.

func (*FileCacher) Incr

func (c *FileCacher) Incr(key string) error

Incr increases cached int-type value by given key as a counter.

func (*FileCacher) IsExist

func (c *FileCacher) IsExist(key string) bool

IsExist returns true if cached value exists.

func (*FileCacher) Put

func (c *FileCacher) Put(key string, val interface{}, expire int64) error

Put puts value into cache with key and expire time. If expired is 0, it will be deleted by next GC operation.

func (*FileCacher) StartAndGC

func (c *FileCacher) StartAndGC(opt Options) error

StartAndGC starts GC routine based on config string settings.

type Item

type Item struct {
	Val     interface{}
	Created int64
	Expire  int64
}

Item represents a cache item.

type MemoryCacher

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

MemoryCacher represents a memory cache adapter implementation.

func NewMemoryCacher

func NewMemoryCacher() *MemoryCacher

NewMemoryCacher creates and returns a new memory cacher.

func (*MemoryCacher) Decr

func (c *MemoryCacher) Decr(key string) (err error)

Decr decreases cached int-type value by given key as a counter.

func (*MemoryCacher) Delete

func (c *MemoryCacher) Delete(key string) error

Delete deletes cached value by given key.

func (*MemoryCacher) Flush

func (c *MemoryCacher) Flush() error

Flush deletes all cached data.

func (*MemoryCacher) Get

func (c *MemoryCacher) Get(key string) interface{}

Get gets cached value by given key.

func (*MemoryCacher) Incr

func (c *MemoryCacher) Incr(key string) (err error)

Incr increases cached int-type value by given key as a counter.

func (*MemoryCacher) IsExist

func (c *MemoryCacher) IsExist(key string) bool

IsExist returns true if cached value exists.

func (*MemoryCacher) Put

func (c *MemoryCacher) Put(key string, val interface{}, expire int64) error

Put puts value into cache with key and expire time. If expired is 0, it will be deleted by next GC operation.

func (*MemoryCacher) StartAndGC

func (c *MemoryCacher) StartAndGC(opt Options) error

StartAndGC starts GC routine based on config string settings.

type MemoryItem

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

MemoryItem represents a memory cache item.

type Options

type Options struct {
	// Name of adapter. Default is "memory".
	Adapter string
	// Adapter configuration, it's corresponding to adapter.
	AdapterConfig string
	// GC interval time in seconds. Default is 60.
	Interval int
	// Occupy entire database. Default is false.
	OccupyMode bool
	// Configuration section name. Default is "cache".
	Section string
}

Options represents a struct for specifying configuration options for the cache middleware.

Jump to

Keyboard shortcuts

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