beego: github.com/astaxie/beego/cache Index | Files | Directories

package cache

import "github.com/astaxie/beego/cache"

Package cache provide a Cache interface and some implement engine Usage:

import(

"github.com/astaxie/beego/cache"

)

bm, err := cache.NewCache("memory", `{"interval":60}`)

Use it like this:

	bm.Put("astaxie", 1, 10 * time.Second)
	bm.Get("astaxie")
	bm.IsExist("astaxie")
	bm.Delete("astaxie")

 more docs http://beego.me/docs/module/cache.md

Index

Package Files

cache.go conv.go file.go memory.go

Variables

var (
    FileCachePath           = "cache"     // cache directory
    FileCacheFileSuffix     = ".bin"      // cache file suffix
    FileCacheDirectoryLevel = 2           // cache file deep level if auto generated cache files.
    FileCacheEmbedExpiry    time.Duration // cache expire time, default is no expire forever.
)

FileCache Config

var (
    // DefaultEvery means the clock time of recycling the expired cache items in memory.
    DefaultEvery = 60 // 1 minute
)

func FileGetContents Uses

func FileGetContents(filename string) (data []byte, e error)

FileGetContents Get bytes to file. if non-exist, create this file.

func FilePutContents Uses

func FilePutContents(filename string, content []byte) error

FilePutContents Put bytes to file. if non-exist, create this file.

func GetBool Uses

func GetBool(v interface{}) bool

GetBool convert interface to bool.

func GetFloat64 Uses

func GetFloat64(v interface{}) float64

GetFloat64 convert interface to float64.

func GetInt Uses

func GetInt(v interface{}) int

GetInt convert interface to int.

func GetInt64 Uses

func GetInt64(v interface{}) int64

GetInt64 convert interface to int64.

func GetString Uses

func GetString(v interface{}) string

GetString convert interface to string.

func GobDecode Uses

func GobDecode(data []byte, to *FileCacheItem) error

GobDecode Gob decodes file cache item.

func GobEncode Uses

func GobEncode(data interface{}) ([]byte, error)

GobEncode Gob encodes file cache item.

func Register Uses

func Register(name string, adapter Instance)

Register makes a cache adapter available by the adapter name. If Register is called twice with the same name or if driver is nil, it panics.

type Cache Uses

type Cache interface {
    // get cached value by key.
    Get(key string) interface{}
    // GetMulti is a batch version of Get.
    GetMulti(keys []string) []interface{}
    // set cached value with key and expire time.
    Put(key string, val interface{}, timeout time.Duration) error
    // delete cached value by key.
    Delete(key string) error
    // increase cached int value by key, as a counter.
    Incr(key string) error
    // decrease cached int value by key, as a counter.
    Decr(key string) error
    // check if cached value exists or not.
    IsExist(key string) bool
    // clear all cache.
    ClearAll() error
    // start gc routine based on config string settings.
    StartAndGC(config string) error
}

Cache interface contains all behaviors for cache adapter. usage:

cache.Register("file",cache.NewFileCache) // this operation is run in init method of file.go.
c,err := cache.NewCache("file","{....}")
c.Put("key",value, 3600 * time.Second)
v := c.Get("key")

c.Incr("counter")  // now is 1
c.Incr("counter")  // now is 2
count := c.Get("counter").(int)

func NewCache Uses

func NewCache(adapterName, config string) (adapter Cache, err error)

NewCache Create a new cache driver by adapter name and config string. config need to be correct JSON as string: {"interval":360}. it will start gc automatically.

func NewFileCache Uses

func NewFileCache() Cache

NewFileCache Create new file cache with no config. the level and expiry need set in method StartAndGC as config string.

func NewMemoryCache Uses

func NewMemoryCache() Cache

NewMemoryCache returns a new MemoryCache.

type FileCache Uses

type FileCache struct {
    CachePath      string
    FileSuffix     string
    DirectoryLevel int
    EmbedExpiry    int
}

FileCache is cache adapter for file storage.

func (*FileCache) ClearAll Uses

func (fc *FileCache) ClearAll() error

ClearAll will clean cached files. not implemented.

func (*FileCache) Decr Uses

func (fc *FileCache) Decr(key string) error

Decr will decrease cached int value.

func (*FileCache) Delete Uses

func (fc *FileCache) Delete(key string) error

Delete file cache value.

func (*FileCache) Get Uses

func (fc *FileCache) Get(key string) interface{}

Get value from file cache. if non-exist or expired, return empty string.

func (*FileCache) GetMulti Uses

func (fc *FileCache) GetMulti(keys []string) []interface{}

GetMulti gets values from file cache. if non-exist or expired, return empty string.

func (*FileCache) Incr Uses

func (fc *FileCache) Incr(key string) error

Incr will increase cached int value. fc value is saving forever unless Delete.

func (*FileCache) Init Uses

func (fc *FileCache) Init()

Init will make new dir for file cache if not exist.

func (*FileCache) IsExist Uses

func (fc *FileCache) IsExist(key string) bool

IsExist check value is exist.

func (*FileCache) Put Uses

func (fc *FileCache) Put(key string, val interface{}, timeout time.Duration) error

Put value into file cache. timeout means how long to keep this file, unit of ms. if timeout equals fc.EmbedExpiry(default is 0), cache this item forever.

func (*FileCache) StartAndGC Uses

func (fc *FileCache) StartAndGC(config string) error

StartAndGC will start and begin gc for file cache. the config need to be like {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}

type FileCacheItem Uses

type FileCacheItem struct {
    Data       interface{}
    Lastaccess time.Time
    Expired    time.Time
}

FileCacheItem is basic unit of file cache adapter. it contains data and expire time.

type Instance Uses

type Instance func() Cache

Instance is a function create a new Cache Instance

type MemoryCache Uses

type MemoryCache struct {
    sync.RWMutex

    Every int // run an expiration check Every clock time
    // contains filtered or unexported fields
}

MemoryCache is Memory cache adapter. it contains a RW locker for safe map storage.

func (*MemoryCache) ClearAll Uses

func (bc *MemoryCache) ClearAll() error

ClearAll will delete all cache in memory.

func (*MemoryCache) Decr Uses

func (bc *MemoryCache) Decr(key string) error

Decr decrease counter in memory.

func (*MemoryCache) Delete Uses

func (bc *MemoryCache) Delete(name string) error

Delete cache in memory.

func (*MemoryCache) Get Uses

func (bc *MemoryCache) Get(name string) interface{}

Get cache from memory. if non-existed or expired, return nil.

func (*MemoryCache) GetMulti Uses

func (bc *MemoryCache) GetMulti(names []string) []interface{}

GetMulti gets caches from memory. if non-existed or expired, return nil.

func (*MemoryCache) Incr Uses

func (bc *MemoryCache) Incr(key string) error

Incr increase cache counter in memory. it supports int,int32,int64,uint,uint32,uint64.

func (*MemoryCache) IsExist Uses

func (bc *MemoryCache) IsExist(name string) bool

IsExist check cache exist in memory.

func (*MemoryCache) Put Uses

func (bc *MemoryCache) Put(name string, value interface{}, lifespan time.Duration) error

Put cache to memory. if lifespan is 0, it will be forever till restart.

func (*MemoryCache) StartAndGC Uses

func (bc *MemoryCache) StartAndGC(config string) error

StartAndGC start memory cache. it will check expiration in every clock time.

type MemoryItem Uses

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

MemoryItem store memory cache item.

Directories

PathSynopsis
memcachePackage memcache for cache provider
redisPackage redis for cache provider
ssdb

Package cache imports 15 packages (graph) and is imported by 903 packages. Updated 2019-09-25. Refresh now. Tools for package owners.