cache

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2014 License: Apache-2.0 Imports: 14 Imported by: 0

README

cache

cache is a Go cache manager. It can use many cache adapters. The repo is inspired by database/sql .

How to install?

go get github.com/astaxie/beego/cache

What adapters are supported?

As of now this cache support memory, Memcache and Redis.

How to use it?

First you must import it

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

Then init a Cache (example with memory adapter)

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

Use it like this:

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

Memory adapter

Configure memory adapter like this:

{"interval":60}

interval means the gc time. The cache will check at each time interval, whether item has expired.

Memcache adapter

Memcache adapter use the vitess's Memcache client.

Configure like this:

{"conn":"127.0.0.1:11211"}

Redis adapter

Redis adapter use the redigo client.

Configure like this:

{"conn":":6039"}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	FileCachePath           string = "cache" // cache directory
	FileCacheFileSuffix     string = ".bin"  // cache file suffix
	FileCacheDirectoryLevel int    = 2       // cache file deep level if auto generated cache files.
	FileCacheEmbedExpiry    int64  = 0       // cache expire time, default is no expire forever.
)
View Source
var (
	// clock time of recycling the expired cache items in memory.
	DefaultEvery int = 60 // 1 minute
)

Functions

func File_get_contents

func File_get_contents(filename string) ([]byte, error)

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

func File_put_contents

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

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

func GetBool

func GetBool(v interface{}) bool

convert interface to bool.

func GetFloat64

func GetFloat64(v interface{}) float64

convert interface to float64.

func GetInt

func GetInt(v interface{}) int

convert interface to int.

func GetInt64

func GetInt64(v interface{}) int64

convert interface to int64.

func GetString

func GetString(v interface{}) string

convert interface to string.

func Gob_decode

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

Gob decodes file cache item.

func Gob_encode

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

Gob encodes file cache item.

func Register

func Register(name string, adapter Cache)

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.

Types

type Cache

type Cache interface {
	// get cached value by key.
	Get(key string) interface{}
	// set cached value with key and expire time.
	Put(key string, val interface{}, timeout int64) 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 cached value is existed or not.
	IsExist(key string) bool
	// clear all cache.
	ClearAll() error
	// start gc routine via config string setting.
	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 := cache.NewCache("file","{....}")
c.Put("key",value,3600)
v := c.Get("key")

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

func NewCache

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

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

type FileCache

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

FileCache is cache adapter for file storage.

func NewFileCache

func NewFileCache() *FileCache

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

func (*FileCache) ClearAll

func (this *FileCache) ClearAll() error

Clean cached files. not implemented.

func (*FileCache) Decr

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

Decrease cached int value.

func (*FileCache) Delete

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

Delete file cache value.

func (*FileCache) Get

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

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

func (*FileCache) Incr

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

Increase cached int value. this value is saving forever unless Delete.

func (*FileCache) Init

func (this *FileCache) Init()

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

func (*FileCache) IsExist

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

Check value is exist.

func (*FileCache) Put

func (this *FileCache) Put(key string, val interface{}, timeout int64) error

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

func (*FileCache) StartAndGC

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

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

type FileCacheItem

type FileCacheItem struct {
	Data       interface{}
	Lastaccess int64
	Expired    int64
}

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

type MemoryCache

type MemoryCache struct {
	Every int // run an expiration check Every clock time
	// contains filtered or unexported fields
}

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

func NewMemoryCache

func NewMemoryCache() *MemoryCache

NewMemoryCache returns a new MemoryCache.

func (*MemoryCache) ClearAll

func (bc *MemoryCache) ClearAll() error

delete all cache in memory.

func (*MemoryCache) Decr

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

Decrease counter in memory.

func (*MemoryCache) Delete

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

/ Delete cache in memory.

func (*MemoryCache) Get

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

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

func (*MemoryCache) Incr

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

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

func (*MemoryCache) IsExist

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

check cache exist in memory.

func (*MemoryCache) Put

func (bc *MemoryCache) Put(name string, value interface{}, expired int64) error

Put cache to memory. if expired is 0, it will be cleaned by next gc operation ( default gc clock is 1 minute).

func (*MemoryCache) StartAndGC

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

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

type MemoryItem

type MemoryItem struct {
	Lastaccess time.Time
	// contains filtered or unexported fields
}

Memory cache item.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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