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

package cache

import "github.com/astaxie/beego/client/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 (
    // Timer for how often to recycle the expired cache items in memory (in seconds)
    DefaultEvery = 60 // 1 minute
)

func FileGetContents Uses

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

FileGetContents Reads bytes from a file. if non-existent, create this file.

func FilePutContents Uses

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

FilePutContents puts bytes into a file. if non-existent, create this file.

func GetBool Uses

func GetBool(v interface{}) bool

GetBool converts interface to bool.

func GetFloat64 Uses

func GetFloat64(v interface{}) float64

GetFloat64 converts interface to float64.

func GetInt Uses

func GetInt(v interface{}) int

GetInt converts interface to int.

func GetInt64 Uses

func GetInt64(v interface{}) int64

GetInt64 converts interface to int64.

func GetString Uses

func GetString(v interface{}) string

GetString converts interface to string.

func GobDecode Uses

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

GobDecode Gob decodes a file cache item.

func GobEncode Uses

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

GobEncode Gob encodes a 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 a cached value by key.
    Get(ctx context.Context, key string) (interface{}, error)
    // GetMulti is a batch version of Get.
    GetMulti(ctx context.Context, keys []string) ([]interface{}, error)
    // Set a cached value with key and expire time.
    Put(ctx context.Context, key string, val interface{}, timeout time.Duration) error
    // Delete cached value by key.
    Delete(ctx context.Context, key string) error
    // Increment a cached int value by key, as a counter.
    Incr(ctx context.Context, key string) error
    // Decrement a cached int value by key, as a counter.
    Decr(ctx context.Context, key string) error
    // Check if a cached value exists or not.
    IsExist(ctx context.Context, key string) (bool, error)
    // Clear all cache.
    ClearAll(ctx context.Context) 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 creates a new cache driver by adapter name and config string. config: must be in JSON format such as {"interval":360}. Starts gc automatically.

func NewFileCache Uses

func NewFileCache() Cache

NewFileCache creates a new file cache with no config. The level and expiry need to be set in the 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(context.Context) error

ClearAll cleans cached files (not implemented)

func (*FileCache) Decr Uses

func (fc *FileCache) Decr(ctx context.Context, key string) error

Decr decreases cached int value.

func (*FileCache) Delete Uses

func (fc *FileCache) Delete(ctx context.Context, key string) error

Delete file cache value.

func (*FileCache) Get Uses

func (fc *FileCache) Get(ctx context.Context, key string) (interface{}, error)

Get value from file cache. if nonexistent or expired return an empty string.

func (*FileCache) GetMulti Uses

func (fc *FileCache) GetMulti(ctx context.Context, keys []string) ([]interface{}, error)

GetMulti gets values from file cache. if nonexistent or expired return an empty string.

func (*FileCache) Incr Uses

func (fc *FileCache) Incr(ctx context.Context, key string) error

Incr increases cached int value. fc value is saved forever unless deleted.

func (*FileCache) Init Uses

func (fc *FileCache) Init()

Init makes new a dir for file cache if it does not already exist

func (*FileCache) IsExist Uses

func (fc *FileCache) IsExist(ctx context.Context, key string) (bool, error)

IsExist checks if value exists.

func (*FileCache) Put Uses

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

Put value into file cache. timeout: how long this file should be kept in 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 starts gc for file cache. config must be in the format {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 which 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 a memory cache adapter. Contains a RW locker for safe map storage.

func (*MemoryCache) ClearAll Uses

func (bc *MemoryCache) ClearAll(context.Context) error

ClearAll deletes all cache in memory.

func (*MemoryCache) Decr Uses

func (bc *MemoryCache) Decr(ctx context.Context, key string) error

Decr decreases counter in memory.

func (*MemoryCache) Delete Uses

func (bc *MemoryCache) Delete(ctx context.Context, key string) error

Delete cache in memory.

func (*MemoryCache) Get Uses

func (bc *MemoryCache) Get(ctx context.Context, key string) (interface{}, error)

Get returns cache from memory. If non-existent or expired, return nil.

func (*MemoryCache) GetMulti Uses

func (bc *MemoryCache) GetMulti(ctx context.Context, keys []string) ([]interface{}, error)

GetMulti gets caches from memory. If non-existent or expired, return nil.

func (*MemoryCache) Incr Uses

func (bc *MemoryCache) Incr(ctx context.Context, key string) error

Incr increases cache counter in memory. Supports int,int32,int64,uint,uint32,uint64.

func (*MemoryCache) IsExist Uses

func (bc *MemoryCache) IsExist(ctx context.Context, key string) (bool, error)

IsExist checks if cache exists in memory.

func (*MemoryCache) Put Uses

func (bc *MemoryCache) Put(ctx context.Context, key string, val interface{}, timeout time.Duration) error

Put puts cache into memory. If lifespan is 0, it will never overwrite this value unless restarted

func (*MemoryCache) StartAndGC Uses

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

StartAndGC starts memory cache. Checks expiration in every clock time.

type MemoryItem Uses

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

MemoryItem stores memory cache item.

Directories

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

Package cache imports 18 packages (graph) and is imported by 4 packages. Updated 2020-12-01. Refresh now. Tools for package owners.