cache

package
v0.0.0-...-a966c5d Latest Latest
Warning

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

Go to latest
Published: May 9, 2019 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DBInterval int = 30
)
View Source
var (
	// clock time of recycling the expired cache items in memory.
	DefaultEvery int = 60 // 1 minute
)

Functions

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{}
	//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 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 if cached value exists or not
	IsExist(key string) bool
	//clear all cache
	ClearAll() error
	//star gc routine based on config string settings.
	StartAndGC(config string) error

	//others init
	Init(val interface{}) error
}

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

var GLCache Cache

func NewCache

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

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.

type DBCache

type DBCache struct {
	Every int //run an expiratin check Every clock time
	DB    gorm.DB
	// contains filtered or unexported fields
}

func NewDBCache

func NewDBCache() *DBCache

func (*DBCache) ClearAll

func (dc *DBCache) ClearAll() error

func (*DBCache) Decr

func (dc *DBCache) Decr(key string) error

decrease cached int value by key, as a counter.

func (*DBCache) Delete

func (dc *DBCache) Delete(name string) error

func (*DBCache) Get

func (dc *DBCache) Get(name string) interface{}

func (*DBCache) GetMulti

func (dc *DBCache) GetMulti(keys []string) []interface{}

func (*DBCache) Incr

func (dc *DBCache) Incr(key string) error

func (*DBCache) Init

func (dc *DBCache) Init(val interface{}) error

func (*DBCache) IsExist

func (dc *DBCache) IsExist(name string) bool

func (*DBCache) Put

func (dc *DBCache) Put(name string, value interface{}, expired int64) error

func (*DBCache) StartAndGC

func (dc *DBCache) StartAndGC(config string) error

type DBItem

type DBItem struct {
	Id           int64     `gorm:"column:id;primary_key" json:"id"`
	UserId       int64     `gorm:"column:user_id" json:"user_id"`
	Tel          string    `gorm:"column:tel" json:"tel"`
	AccessToken  string    `gorm:"column:access_token" json:"access_token"`
	LastAccess   time.Time `gorm:"column:last_access" json:"last_access"`
	Expired      int64     `gorm:"column:expired" json:"expired"`
	LastAccessIp string    `gorm:"column:last_access_ip" json:"last_access_ip"`
	AppId        string    `gorm:"column:appid" json:"appid"`
	ChannelId    string    `gorm:column:"channel_id" json:"channel_id"`
}

func (DBItem) TableName

func (item DBItem) TableName() string

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) GetMulti

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

GetMulti gets caches 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) Init

func (bc *MemoryCache) Init(val interface{}) error

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.

Jump to

Keyboard shortcuts

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